ndisapi/netlib/ip_helper/
guid_wrapper.rs

1use std::fmt;
2use uuid::Uuid;
3
4/// A wrapper around `Uuid` to represent a GUID.
5#[derive(Default, Debug, Clone)]
6pub struct GuidWrapper(Uuid);
7
8impl GuidWrapper {
9    /// Creates a new `GuidWrapper` with a nil `Uuid`.
10    ///
11    /// # Returns
12    ///
13    /// A new `GuidWrapper` instance with a nil `Uuid`.
14    pub fn new() -> Self {
15        GuidWrapper(Uuid::nil())
16    }
17
18    /// Creates a new `GuidWrapper` from the provided `Uuid`.
19    ///
20    /// # Arguments
21    ///
22    /// * `uuid` - The `Uuid` to wrap.
23    ///
24    /// # Returns
25    ///
26    /// A new `GuidWrapper` instance containing the provided `Uuid`.
27    pub fn from_uuid(uuid: Uuid) -> Self {
28        GuidWrapper(uuid)
29    }
30
31    /// Converts the wrapped `Uuid` to a hyphenated uppercase string.
32    ///
33    /// # Returns
34    ///
35    /// A `String` representation of the hyphenated uppercase `Uuid`.
36    pub fn to_hyphenated_upper_string(&self) -> String {
37        self.0.hyphenated().to_string().to_uppercase()
38    }
39}
40
41/// Implements the `From<Uuid>` trait for `GuidWrapper`.
42impl From<Uuid> for GuidWrapper {
43    /// Creates a new `GuidWrapper` from the provided `Uuid`.
44    ///
45    /// # Arguments
46    ///
47    /// * `uuid` - The `Uuid` to wrap.
48    ///
49    /// # Returns
50    ///
51    /// A new `GuidWrapper` instance containing the provided `Uuid`.
52    fn from(uuid: Uuid) -> Self {
53        GuidWrapper::from_uuid(uuid)
54    }
55}
56
57/// Implements the `Display` trait for `GuidWrapper`.
58impl fmt::Display for GuidWrapper {
59    /// Formats the wrapped `Uuid` as a hyphenated uppercase string.
60    ///
61    /// # Arguments
62    ///
63    /// * `f` - The mutable `Formatter` reference.
64    ///
65    /// # Returns
66    ///
67    /// A `fmt::Result` containing the result of the formatting operation.
68    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}