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
//! The User ID
use uuid::Uuid;
use crate::errors::errors::UsersError;

/// User ID consists of an ID only that is UUID
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct UserId {
    id: String
}

impl UserId {

    pub fn id(&self) -> &str {
        &self.id
    }

    // Generate a new User ID.
    pub (crate) fn generate() -> Self {
        Self {
            id: Uuid::new_v4().to_string()
        }
    }
}

impl TryFrom<&str> for UserId {
    type Error = UsersError;
    /// The argument is user ID that must be UUID.
    /// If you don't provide a valid ID, this function returns errors.
    fn try_from(id: &str) -> Result<Self, UsersError> {
        match Uuid::try_parse(id) {
            Ok(id_from_string) => {
                Ok(Self {
                    id: id_from_string.to_string()
                })
            }
            Err(_) => Err(UsersError::InvalidUUID)
        }   
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_eq() {
        let user_id = UserId::generate();
        let clone_id = user_id.clone();
        assert_eq!(user_id, clone_id);
    }

}