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