ndisapi/netlib/ip_helper/
guid_wrapper.rs1use std::fmt;
2use uuid::Uuid;
3
4#[derive(Default, Debug, Clone)]
6pub struct GuidWrapper(Uuid);
7
8impl GuidWrapper {
9 pub fn new() -> Self {
15 GuidWrapper(Uuid::nil())
16 }
17
18 pub fn from_uuid(uuid: Uuid) -> Self {
28 GuidWrapper(uuid)
29 }
30
31 pub fn to_hyphenated_upper_string(&self) -> String {
37 self.0.hyphenated().to_string().to_uppercase()
38 }
39}
40
41impl From<Uuid> for GuidWrapper {
43 fn from(uuid: Uuid) -> Self {
53 GuidWrapper::from_uuid(uuid)
54 }
55}
56
57impl fmt::Display for GuidWrapper {
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 write!(f, "{}", self.to_hyphenated_upper_string())
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::GuidWrapper;
76 use uuid::Uuid;
77
78 #[test]
79 fn test_new() {
80 let guid_wrapper = GuidWrapper::new();
81 assert_eq!(guid_wrapper.0, Uuid::nil());
82 }
83
84 #[test]
85 fn test_from_uuid() {
86 let uuid = Uuid::new_v4();
87 let guid_wrapper = GuidWrapper::from_uuid(uuid);
88 assert_eq!(guid_wrapper.0, uuid);
89 }
90
91 #[test]
92 fn test_to_hyphenated_upper_string() {
93 let uuid = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
94 let guid_wrapper = GuidWrapper::from_uuid(uuid);
95 let guid_str = guid_wrapper.to_hyphenated_upper_string();
96 assert_eq!(guid_str, "550E8400-E29B-41D4-A716-446655440000");
97 }
98
99 #[test]
100 fn test_display() {
101 let uuid = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
102 let guid_wrapper = GuidWrapper::from_uuid(uuid);
103 let guid_str = guid_wrapper.to_string();
104 assert_eq!(guid_str, "550E8400-E29B-41D4-A716-446655440000");
105 }
106}