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
use c3p0::{C3p0Error, JsonCodec, Model};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::borrow::Cow;
use typescript_definitions::TypeScriptify;

pub type AuthAccountModel = Model<AuthAccountData>;

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthAccountData {
    pub username: String,
    pub email: String,
    pub password: String,
    pub roles: Vec<String>,
    pub created_date_epoch_seconds: i64,
    pub status: AuthAccountStatus,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, TypeScriptify)]
#[allow(non_camel_case_types)]
pub enum AuthAccountStatus {
    ACTIVE,
    PENDING_ACTIVATION,
    DISABLED,
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(tag = "_json_tag")]
enum AuthAccountDataVersioning<'a> {
    V1(Cow<'a, AuthAccountData>),
}

#[derive(Clone)]
pub struct AuthAccountDataCodec {}

impl JsonCodec<AuthAccountData> for AuthAccountDataCodec {
    fn from_value(&self, value: Value) -> Result<AuthAccountData, C3p0Error> {
        let versioning = serde_json::from_value(value)?;
        let data = match versioning {
            AuthAccountDataVersioning::V1(data_v1) => data_v1.into_owned(),
        };
        Ok(data)
    }

    fn to_value(&self, data: &AuthAccountData) -> Result<Value, C3p0Error> {
        serde_json::to_value(AuthAccountDataVersioning::V1(Cow::Borrowed(data)))
            .map_err(C3p0Error::from)
    }
}