ps_uuid/methods/
predicates.rs1use crate::{Variant, UUID};
4
5impl UUID {
6 #[inline]
8 #[must_use]
9 pub const fn is_nil(&self) -> bool {
10 matches!(self.bytes, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
11 }
12
13 #[inline]
15 #[must_use]
16 pub const fn is_max(&self) -> bool {
17 matches!(
18 self.bytes,
19 [
20 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
21 0xFF, 0xFF
22 ]
23 )
24 }
25
26 #[inline]
28 #[must_use]
29 pub const fn is_ncs(&self) -> bool {
30 matches!(self.get_variant(), Variant::NCS)
31 }
32
33 #[inline]
35 #[must_use]
36 pub const fn is_osf(&self) -> bool {
37 matches!(self.get_variant(), Variant::OSF)
38 }
39
40 #[inline]
42 #[must_use]
43 pub const fn is_dcom(&self) -> bool {
44 matches!(self.get_variant(), Variant::DCOM)
45 }
46
47 #[inline]
49 #[must_use]
50 pub const fn is_reserved(&self) -> bool {
51 matches!(self.get_variant(), Variant::Reserved)
52 }
53
54 #[inline]
56 #[must_use]
57 pub const fn is_v1(&self) -> bool {
58 matches!(self.get_version(), Some(1))
59 }
60
61 #[inline]
63 #[must_use]
64 pub const fn is_v2(&self) -> bool {
65 matches!(self.get_version(), Some(2))
66 }
67
68 #[inline]
70 #[must_use]
71 pub const fn is_v3(&self) -> bool {
72 matches!(self.get_version(), Some(3))
73 }
74
75 #[inline]
77 #[must_use]
78 pub const fn is_v4(&self) -> bool {
79 matches!(self.get_version(), Some(4))
80 }
81
82 #[inline]
84 #[must_use]
85 pub const fn is_v5(&self) -> bool {
86 matches!(self.get_version(), Some(5))
87 }
88
89 #[inline]
91 #[must_use]
92 pub const fn is_v6(&self) -> bool {
93 matches!(self.get_version(), Some(6))
94 }
95
96 #[inline]
98 #[must_use]
99 pub const fn is_v7(&self) -> bool {
100 matches!(self.get_version(), Some(7))
101 }
102
103 #[inline]
105 #[must_use]
106 pub const fn is_v8(&self) -> bool {
107 matches!(self.get_version(), Some(8))
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use crate::{Variant, UUID};
114
115 const fn uuid_with_variant_version(variant_byte: u8, version_nibble: u8) -> UUID {
116 let mut bytes = [0u8; 16];
117 bytes[6] = version_nibble << 4;
118 bytes[8] = variant_byte;
119 UUID { bytes }
120 }
121
122 #[test]
123 fn is_nil_returns_true_only_for_nil() {
124 assert!(UUID::nil().is_nil());
125 assert!(!UUID::max().is_nil());
126 assert!(!UUID::from(1u8).is_nil());
127 }
128
129 #[test]
130 fn is_max_returns_true_only_for_max() {
131 assert!(UUID::max().is_max());
132 assert!(!UUID::nil().is_max());
133 assert!(!UUID::from(1u8).is_max());
134 }
135
136 #[test]
137 fn variant_predicates_match_get_variant() {
138 let ncs = uuid_with_variant_version(0x00, 0);
139 let osf = uuid_with_variant_version(0x80, 4);
140 let dcom = uuid_with_variant_version(0xC0, 0);
141 let reserved = uuid_with_variant_version(0xE0, 0);
142
143 assert!(ncs.is_ncs());
144 assert!(!ncs.is_osf());
145 assert_eq!(ncs.get_variant(), Variant::NCS);
146
147 assert!(osf.is_osf());
148 assert!(!osf.is_ncs());
149 assert_eq!(osf.get_variant(), Variant::OSF);
150
151 assert!(dcom.is_dcom());
152 assert!(!dcom.is_osf());
153 assert_eq!(dcom.get_variant(), Variant::DCOM);
154
155 assert!(reserved.is_reserved());
156 assert!(!reserved.is_osf());
157 assert_eq!(reserved.get_variant(), Variant::Reserved);
158 }
159
160 #[test]
161 fn version_predicates_match_get_version() {
162 for version in 1..=8 {
163 let uuid = uuid_with_variant_version(0x80, version);
164 assert_eq!(uuid.get_version(), Some(version));
165
166 assert_eq!(uuid.is_v1(), version == 1);
167 assert_eq!(uuid.is_v2(), version == 2);
168 assert_eq!(uuid.is_v3(), version == 3);
169 assert_eq!(uuid.is_v4(), version == 4);
170 assert_eq!(uuid.is_v5(), version == 5);
171 assert_eq!(uuid.is_v6(), version == 6);
172 assert_eq!(uuid.is_v7(), version == 7);
173 assert_eq!(uuid.is_v8(), version == 8);
174 }
175 }
176
177 #[test]
178 fn version_predicates_return_false_for_non_osf() {
179 let ncs = uuid_with_variant_version(0x00, 4);
180 assert!(!ncs.is_v4());
181 assert_eq!(ncs.get_version(), None);
182 }
183
184 #[test]
185 fn generated_uuids_have_correct_predicates() {
186 let v4 = UUID::gen_v4();
187 assert!(v4.is_v4());
188 assert!(v4.is_osf());
189 assert!(!v4.is_nil());
190 assert!(!v4.is_max());
191 }
192}