1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum Variant {
None = 0,
IfExists = 1,
Negated = 2,
IfExistsNegated = 3,
}
impl Variant {
#[inline]
pub(super) fn as_usize(self) -> usize {
self as usize
}
#[inline]
pub(super) fn if_exists(self) -> bool {
matches!(self, Self::IfExists | Self::IfExistsNegated)
}
#[inline]
pub(super) fn negated(self) -> bool {
matches!(self, Self::Negated | Self::IfExistsNegated)
}
}
impl From<u8> for Variant {
fn from(value: u8) -> Self {
match value {
0 => Self::None,
1 => Self::IfExists,
2 => Self::Negated,
3 => Self::IfExistsNegated,
_ => panic!("Invalid variant value: {}", value),
}
}
}
#[cfg(test)]
mod tests {
use {super::Variant, pretty_assertions::assert_eq, std::panic::catch_unwind};
#[test_log::test]
fn test_clone() {
assert_eq!(Variant::None.clone(), Variant::None);
assert_eq!(Variant::IfExists.clone(), Variant::IfExists);
assert_eq!(Variant::Negated.clone(), Variant::Negated);
assert_eq!(Variant::IfExistsNegated.clone(), Variant::IfExistsNegated);
}
#[test_log::test]
fn test_variant_values() {
assert_eq!(Variant::None, Variant::from(0));
assert_eq!(Variant::IfExists, Variant::from(1));
assert_eq!(Variant::Negated, Variant::from(2));
assert_eq!(Variant::IfExistsNegated, Variant::from(3));
let e = catch_unwind(|| Variant::from(4)).unwrap_err();
assert_eq!(e.downcast_ref::<String>().unwrap(), "Invalid variant value: 4");
}
}