use uuid::Uuid;
use crate::errors::errors::UsersError;
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct UserId {
id: String
}
impl UserId {
pub fn id(&self) -> &str {
&self.id
}
pub (crate) fn generate() -> Self {
Self {
id: Uuid::new_v4().to_string()
}
}
}
impl TryFrom<&str> for UserId {
type Error = UsersError;
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);
}
}