summaryrefslogtreecommitdiffstats
path: root/src/model.rs
blob: 5f6111e604edc376cedb2d70ad6da293655d8055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
#[serde(rename_all = "camelCase")]
pub struct User {
    pub id: uuid::Uuid,
    pub name: String,
    pub email: String,
    #[serde(default, skip_serializing)]
    pub password: String,
    pub created_at: Option<OffsetDateTime>,
    pub updated_at: Option<OffsetDateTime>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TokenClaims {
    pub sub: String,
    pub iat: usize,
    pub exp: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RegisterSchema {
    pub name: String,
    pub email: String,
    #[serde(default, skip_serializing)]
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoginSchema {
    pub email: String,
    #[serde(default, skip_serializing)]
    pub password: String,
}

macro_rules! impl_from_superset {
    ($from:tt, $to:ty, $($field:tt)*) => {
        impl From<$from> for $to {
            fn from(value: $from) -> Self {
                let $from {
                    $($field)*,
                    ..
                } = value;

                Self {
                    $($field)*,
                }
            }
        }
    };
}

impl_from_superset!(User, RegisterSchema, name, email, password);
impl_from_superset!(User, LoginSchema, email, password);
impl_from_superset!(RegisterSchema, LoginSchema, email, password);