yfunc_rust/
yuid.rs

1use std::str::FromStr;
2
3use crate::prelude::*;
4use uuid::Uuid;
5
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct YUid(Uuid);
8
9impl YUid {
10
11    pub fn origin(&self) -> Uuid {
12        self.0
13    }
14
15    pub fn new() -> Self {
16        Self(Uuid::new_v4())
17    }
18
19    pub fn from_str(uuid_str: &str) -> YRes<YUid> {
20        let uid = Uuid::from_str(uuid_str).map_err(|e| {
21            err!("build YUid from uuid string failed").trace(
22                ctx!("build YUid from uuid string: Uuid::from_str() failed", uuid_str, e)
23            )
24        })?;
25        Ok(Self(uid))
26    }
27
28    pub fn to_str(&self) -> String {
29        self.0.to_string()
30    }
31
32}