You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
Rust
59 lines
1.4 KiB
Rust
//! User model and related types.
|
|
|
|
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
/// User role enum matching the Postgres `user_role` CHECK constraint.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum UserRole {
|
|
User,
|
|
Admin,
|
|
}
|
|
|
|
impl UserRole {
|
|
/// Convert from database string representation.
|
|
pub fn from_db(s: &str) -> Self {
|
|
match s {
|
|
"admin" => UserRole::Admin,
|
|
_ => UserRole::User,
|
|
}
|
|
}
|
|
|
|
/// Convert to database string representation.
|
|
pub fn as_str(&self) -> &'static str {
|
|
match self {
|
|
UserRole::User => "user",
|
|
UserRole::Admin => "admin",
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for UserRole {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.as_str())
|
|
}
|
|
}
|
|
|
|
/// A user record from the database.
|
|
#[derive(Debug, Clone, Serialize)]
|
|
pub struct User {
|
|
pub id: Uuid,
|
|
pub email: String,
|
|
pub display_name: Option<String>,
|
|
pub role: UserRole,
|
|
pub created_at: DateTime<Utc>,
|
|
pub updated_at: DateTime<Utc>,
|
|
}
|
|
|
|
/// Response shape for `GET /api/v1/auth/me`.
|
|
#[derive(Debug, Serialize)]
|
|
pub struct MeResponse {
|
|
pub id: Uuid,
|
|
pub email: String,
|
|
pub display_name: Option<String>,
|
|
pub role: String,
|
|
pub created_at: DateTime<Utc>,
|
|
}
|