Skip to main content

pebble_cms/models/
user.rs

1use serde::{Deserialize, Serialize};
2use std::str::FromStr;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "lowercase")]
6pub enum UserRole {
7    Admin,
8    Author,
9    Viewer,
10}
11
12impl FromStr for UserRole {
13    type Err = ();
14
15    fn from_str(s: &str) -> Result<Self, Self::Err> {
16        match s.to_lowercase().as_str() {
17            "admin" => Ok(Self::Admin),
18            "author" => Ok(Self::Author),
19            "viewer" => Ok(Self::Viewer),
20            _ => Err(()),
21        }
22    }
23}
24
25impl std::fmt::Display for UserRole {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            Self::Admin => write!(f, "admin"),
29            Self::Author => write!(f, "author"),
30            Self::Viewer => write!(f, "viewer"),
31        }
32    }
33}
34
35#[derive(Debug, Clone, Serialize)]
36pub struct User {
37    pub id: i64,
38    pub username: String,
39    pub email: String,
40    #[serde(skip_serializing)]
41    pub password_hash: String,
42    pub role: UserRole,
43    pub created_at: String,
44    pub updated_at: String,
45}
46
47#[derive(Debug, Clone, Serialize)]
48pub struct UserSummary {
49    pub id: i64,
50    pub username: String,
51}
52
53impl From<&User> for UserSummary {
54    fn from(user: &User) -> Self {
55        Self {
56            id: user.id,
57            username: user.username.clone(),
58        }
59    }
60}