summaryrefslogtreecommitdiff
path: root/crates/env/src/member.rs
blob: 34abe0db940ef6b59d70ffedc0901bbf7a508503 (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
use cfg_file::ConfigFile;
use serde::{Deserialize, Serialize};
use string_proc::camel_case;
use uuid::Uuid;

#[derive(Debug, Eq, Clone, ConfigFile, Serialize, Deserialize)]
pub struct Member {
    id: String,
    uuid: Uuid,
}

impl Default for Member {
    fn default() -> Self {
        Self::new("default_user")
    }
}

impl PartialEq for Member {
    fn eq(&self, other: &Self) -> bool {
        self.uuid == other.uuid
    }
}

impl std::fmt::Display for Member {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.id)
    }
}

impl std::convert::AsRef<str> for Member {
    fn as_ref(&self) -> &str {
        &self.id
    }
}

impl Member {
    /// Create member struct by id
    pub fn new(new_id: impl Into<String>) -> Self {
        let uuid = Uuid::new_v4();
        Self {
            id: camel_case!(new_id.into()),
            uuid,
        }
    }

    /// Get member id
    pub fn id(&self) -> String {
        self.id.clone()
    }

    /// Get member uuid
    pub fn uuid(&self) -> Uuid {
        self.uuid
    }
}