ps_uuid/methods/
fmt_braced.rs1use core::fmt;
4
5use crate::UUID;
6
7#[derive(Clone, Copy, Debug)]
19pub struct Braced(UUID);
20
21impl fmt::Display for Braced {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 let b = &self.0.bytes;
24 write!(
25 f,
26 "{{{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}}}",
27 b[0], b[1], b[2], b[3],
28 b[4], b[5],
29 b[6], b[7],
30 b[8], b[9],
31 b[10], b[11], b[12], b[13], b[14], b[15]
32 )
33 }
34}
35
36impl From<Braced> for UUID {
37 #[inline]
38 fn from(braced: Braced) -> Self {
39 braced.0
40 }
41}
42
43impl UUID {
44 #[inline]
55 #[must_use]
56 pub const fn braced(self) -> Braced {
57 Braced(self)
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 #![allow(clippy::expect_used)]
64 use crate::UUID;
65
66 #[test]
67 fn nil_formats_correctly() {
68 assert_eq!(
69 UUID::nil().braced().to_string(),
70 "{00000000-0000-0000-0000-000000000000}"
71 );
72 }
73
74 #[test]
75 fn max_formats_correctly() {
76 assert_eq!(
77 UUID::max().braced().to_string(),
78 "{ffffffff-ffff-ffff-ffff-ffffffffffff}"
79 );
80 }
81
82 #[test]
83 fn braced_has_correct_length() {
84 let uuid = UUID::gen_v4();
85 let braced = uuid.braced().to_string();
86 assert_eq!(braced.len(), 38); }
88
89 #[test]
90 fn braced_starts_and_ends_with_braces() {
91 let uuid = UUID::gen_v4();
92 let braced = uuid.braced().to_string();
93 assert!(braced.starts_with('{'));
94 assert!(braced.ends_with('}'));
95 }
96
97 #[test]
98 fn braced_contains_hyphenated_uuid() {
99 let uuid = UUID::gen_v4();
100 let braced = uuid.braced().to_string();
101 let hyphenated = uuid.hyphenated().to_string();
102 assert_eq!(&braced[1..37], hyphenated);
103 }
104
105 #[test]
106 fn braced_is_lowercase() {
107 let uuid = UUID::from_bytes([
108 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56,
109 0x78, 0x9A,
110 ]);
111 let braced = uuid.braced().to_string();
112 assert_eq!(braced, "{abcdef12-3456-789a-bcde-f0123456789a}");
113 }
114
115 #[test]
116 fn round_trip_parse() {
117 let uuid = UUID::gen_v4();
118 let braced = uuid.braced().to_string();
119 let parsed: UUID = braced.parse().expect("braced format should parse");
120 assert_eq!(parsed, uuid);
121 }
122}