wot_network/
trust_depth.rs1use std::cmp::Ordering;
2
3#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
7pub enum TrustDepth {
8 None,
10 Limited(u8),
12 Unlimited,
14}
15
16impl From<u8> for TrustDepth {
17 fn from(value: u8) -> Self {
18 match value {
19 0 => Self::None,
20 u8::MAX => Self::Unlimited,
21 i => Self::Limited(i),
22 }
23 }
24}
25
26impl PartialEq<u8> for TrustDepth {
27 fn eq(&self, other: &u8) -> bool {
28 let other = TrustDepth::from(*other);
29 *self == other
30 }
31}
32
33impl PartialOrd<u8> for TrustDepth {
34 fn partial_cmp(&self, other: &u8) -> Option<Ordering> {
35 let other = TrustDepth::from(*other);
36 Some(self.cmp(&other))
37 }
38}
39
40impl PartialOrd<Self> for TrustDepth {
41 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
42 Some(self.cmp(other))
43 }
44}
45
46impl Ord for TrustDepth {
47 fn cmp(&self, other: &TrustDepth) -> Ordering {
48 match (*self, other) {
49 (Self::Unlimited, Self::Unlimited) => Ordering::Equal,
50 (Self::Unlimited, Self::Limited(_) | Self::None) => Ordering::Greater,
51 (Self::Limited(_) | Self::None, Self::Unlimited) => Ordering::Less,
52 (Self::None, Self::None) => Ordering::Equal,
53 (Self::None, Self::Limited(_)) => Ordering::Less,
54 (Self::Limited(_), Self::None) => Ordering::Greater,
55 (Self::Limited(value), Self::Limited(other)) => value.cmp(other),
56 }
57 }
58}
59
60impl From<TrustDepth> for u8 {
61 fn from(value: TrustDepth) -> Self {
62 match value {
63 TrustDepth::None => 0,
64 TrustDepth::Limited(i) => i,
65 TrustDepth::Unlimited => u8::MAX,
66 }
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use crate::TrustDepth;
73
74 #[test]
75 fn trust_depth() {
76 let depth1 = TrustDepth::Limited(0);
77 let depth2 = TrustDepth::Limited(1);
78 let depth3 = TrustDepth::Unlimited;
79
80 assert!(depth1 < depth2);
81 assert!(depth1 < depth3);
82 assert!(depth2 < depth3);
83
84 assert_eq!(depth1, depth1);
85 assert_eq!(depth2, depth2);
86 assert_eq!(depth3, depth3);
87 }
88}