Skip to main content

ps_uuid/methods/
get_variant.rs

1use crate::{Variant, UUID};
2
3impl UUID {
4    #[must_use]
5    pub const fn get_variant(&self) -> Variant {
6        match self.bytes[8] {
7            0x00..=0x7F => Variant::NCS,
8            0x80..=0xBF => Variant::OSF,
9            0xC0..=0xDF => Variant::DCOM,
10            0xE0..=0xFF => Variant::Reserved,
11        }
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use super::{Variant, UUID};
18
19    /// A helper function to create a `UUID` instance where only the 8th byte,
20    /// which determines the variant, is significant for our tests.
21    /// The other bytes are initialized to zero.
22    const fn create_uuid_with_variant_byte(variant_byte: u8) -> UUID {
23        let mut bytes = [0u8; 16];
24        bytes[8] = variant_byte;
25        UUID { bytes }
26    }
27
28    #[test]
29    fn test_variant_ncs() {
30        // The variant bits are 0xx...
31        // This corresponds to the byte range 0x00..=0x7F.
32
33        // Test lower boundary
34        let uuid_lower = create_uuid_with_variant_byte(0x00);
35        assert_eq!(uuid_lower.get_variant(), Variant::NCS);
36
37        // Test a value within the range
38        let uuid_middle = create_uuid_with_variant_byte(0x42);
39        assert_eq!(uuid_middle.get_variant(), Variant::NCS);
40
41        // Test upper boundary
42        let uuid_upper = create_uuid_with_variant_byte(0x7F);
43        assert_eq!(uuid_upper.get_variant(), Variant::NCS);
44    }
45
46    #[test]
47    fn test_variant_osf() {
48        // The variant bits are 10x...
49        // This corresponds to the byte range 0x80..=0xBF.
50        // This is the standard variant for RFC 4122 UUIDs.
51
52        // Test lower boundary
53        let uuid_lower = create_uuid_with_variant_byte(0x80);
54        assert_eq!(uuid_lower.get_variant(), Variant::OSF);
55
56        // Test a value within the range (e.g., from a v4 UUID)
57        let uuid_middle = create_uuid_with_variant_byte(0xA9);
58        assert_eq!(uuid_middle.get_variant(), Variant::OSF);
59
60        // Test upper boundary
61        let uuid_upper = create_uuid_with_variant_byte(0xBF);
62        assert_eq!(uuid_upper.get_variant(), Variant::OSF);
63    }
64
65    #[test]
66    fn test_variant_dcom() {
67        // The variant bits are 110...
68        // This corresponds to the byte range 0xC0..=0xDF.
69
70        // Test lower boundary
71        let uuid_lower = create_uuid_with_variant_byte(0xC0);
72        assert_eq!(uuid_lower.get_variant(), Variant::DCOM);
73
74        // Test a value within the range
75        let uuid_middle = create_uuid_with_variant_byte(0xCB);
76        assert_eq!(uuid_middle.get_variant(), Variant::DCOM);
77
78        // Test upper boundary
79        let uuid_upper = create_uuid_with_variant_byte(0xDF);
80        assert_eq!(uuid_upper.get_variant(), Variant::DCOM);
81    }
82
83    #[test]
84    fn test_variant_reserved() {
85        // The variant bits are 111...
86        // This corresponds to the byte range 0xE0..=0xFF.
87
88        // Test lower boundary
89        let uuid_lower = create_uuid_with_variant_byte(0xE0);
90        assert_eq!(uuid_lower.get_variant(), Variant::Reserved);
91
92        // Test a value within the range
93        let uuid_middle = create_uuid_with_variant_byte(0xF0);
94        assert_eq!(uuid_middle.get_variant(), Variant::Reserved);
95
96        // Test upper boundary
97        let uuid_upper = create_uuid_with_variant_byte(0xFF);
98        assert_eq!(uuid_upper.get_variant(), Variant::Reserved);
99    }
100}