Skip to main content

ps_uuid/methods/
fmt_simple.rs

1//! Simple (non-hyphenated) formatting for UUID.
2
3use core::fmt;
4
5use crate::UUID;
6
7/// A UUID formatted as a simple, unadorned sequence of 32 hexadecimal digits.
8///
9/// Created by calling [`UUID::simple()`], this wrapper implements [`Display`]
10/// to render the UUID without hyphens or any other separators.
11///
12/// ```text
13/// 550e8400e29b41d4a716446655440000
14/// ```
15///
16/// [`Display`]: core::fmt::Display
17#[derive(Clone, Copy, Debug)]
18pub struct Simple(UUID);
19
20impl fmt::Display for Simple {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        let b = &self.0.bytes;
23        write!(
24            f,
25            "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
26            b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
27            b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]
28        )
29    }
30}
31
32impl From<Simple> for UUID {
33    #[inline]
34    fn from(simple: Simple) -> Self {
35        simple.0
36    }
37}
38
39impl UUID {
40    /// Returns a formatter for the simple (non-hyphenated) format.
41    ///
42    /// # Example
43    ///
44    /// ```
45    /// use ps_uuid::UUID;
46    ///
47    /// let uuid = UUID::nil();
48    /// assert_eq!(uuid.simple().to_string(), "00000000000000000000000000000000");
49    /// ```
50    #[inline]
51    #[must_use]
52    pub const fn simple(self) -> Simple {
53        Simple(self)
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    #![allow(clippy::expect_used)]
60    use crate::UUID;
61
62    #[test]
63    fn nil_formats_as_32_zeros() {
64        assert_eq!(
65            UUID::nil().simple().to_string(),
66            "00000000000000000000000000000000"
67        );
68    }
69
70    #[test]
71    fn max_formats_as_32_fs() {
72        assert_eq!(
73            UUID::max().simple().to_string(),
74            "ffffffffffffffffffffffffffffffff"
75        );
76    }
77
78    #[test]
79    fn simple_has_no_hyphens() {
80        let uuid = UUID::gen_v4();
81        let simple = uuid.simple().to_string();
82        assert_eq!(simple.len(), 32);
83        assert!(!simple.contains('-'));
84    }
85
86    #[test]
87    fn simple_matches_display_without_hyphens() {
88        let uuid = UUID::gen_v4();
89        let display = uuid.to_string();
90        let simple = uuid.simple().to_string();
91        assert_eq!(simple, display.replace('-', ""));
92    }
93
94    #[test]
95    fn simple_is_lowercase_hex() {
96        let uuid = UUID::from_bytes([
97            0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56,
98            0x78, 0x9A,
99        ]);
100        let simple = uuid.simple().to_string();
101        assert_eq!(simple, "abcdef123456789abcdef0123456789a");
102    }
103
104    #[test]
105    fn round_trip_parse() {
106        let uuid = UUID::gen_v4();
107        let simple = uuid.simple().to_string();
108        let parsed: UUID = simple.parse().expect("simple format should parse");
109        assert_eq!(parsed, uuid);
110    }
111}