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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::{borrow::Borrow, fmt, str::FromStr};
use uuid::Uuid;
#[derive(Default, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Id(String);
impl Id {
pub fn new() -> Self {
Uuid::new_v4().into()
}
pub fn is_valid(&self) -> bool {
!self.0.is_empty()
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl AsRef<String> for Id {
fn as_ref(&self) -> &String {
&self.0
}
}
impl AsRef<str> for Id {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl From<String> for Id {
fn from(from: String) -> Self {
Self(from)
}
}
impl From<&str> for Id {
fn from(from: &str) -> Self {
from.to_owned().into()
}
}
impl From<Uuid> for Id {
fn from(from: Uuid) -> Self {
from.as_simple().to_string().into()
}
}
impl From<Id> for String {
fn from(from: Id) -> Self {
from.0
}
}
impl FromStr for Id {
type Err = ();
fn from_str(s: &str) -> Result<Id, Self::Err> {
Ok(s.into())
}
}
impl Borrow<str> for Id {
fn borrow(&self) -> &str {
self.as_ref()
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(self.as_ref())
}
}