dd_sds/match_validation/
match_status.rs

1#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone)]
2pub enum MatchStatus {
3    // The ordering here is important, values further down the list have a higher priority when merging.
4    NotChecked,
5    NotAvailable,
6    Invalid,
7    Error(String),
8    Valid,
9}
10
11impl std::fmt::Display for MatchStatus {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        match self {
14            MatchStatus::NotChecked => write!(f, "NotChecked"),
15            MatchStatus::NotAvailable => write!(f, "NotAvailable"),
16            MatchStatus::Invalid => write!(f, "Invalid"),
17            MatchStatus::Error(msg) => write!(f, "Error({})", msg),
18            MatchStatus::Valid => write!(f, "Valid"),
19        }
20    }
21}
22
23impl MatchStatus {
24    // Order matters as we want to update the match_status only if the new match_status has higher priority.
25    // (in case of split key where we try different combinations of id and secret (aws use-case))
26    pub fn merge(&mut self, new_status: MatchStatus) {
27        if new_status > *self {
28            *self = new_status;
29        }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn test_merge() {
39        let mut status = MatchStatus::NotChecked;
40        status.merge(MatchStatus::NotAvailable);
41        assert_eq!(status, MatchStatus::NotAvailable);
42
43        status.merge(MatchStatus::Invalid);
44        assert_eq!(status, MatchStatus::Invalid);
45
46        status.merge(MatchStatus::Error("error".to_string()));
47        assert_eq!(status, MatchStatus::Error("error".to_string()));
48
49        status.merge(MatchStatus::Valid);
50        assert_eq!(status, MatchStatus::Valid);
51    }
52    #[test]
53    fn test_merge_lower_prio() {
54        let mut status = MatchStatus::Valid;
55        status.merge(MatchStatus::NotChecked);
56        assert_eq!(status, MatchStatus::Valid);
57
58        status.merge(MatchStatus::NotAvailable);
59        assert_eq!(status, MatchStatus::Valid);
60
61        status.merge(MatchStatus::Invalid);
62        assert_eq!(status, MatchStatus::Valid);
63
64        status.merge(MatchStatus::Error("error".to_string()));
65        assert_eq!(status, MatchStatus::Valid);
66
67        status = MatchStatus::Error("error".to_string());
68        status.merge(MatchStatus::NotChecked);
69
70        assert_eq!(status, MatchStatus::Error("error".to_string()));
71
72        status.merge(MatchStatus::NotAvailable);
73        assert_eq!(status, MatchStatus::Error("error".to_string()));
74
75        status.merge(MatchStatus::Invalid);
76        assert_eq!(status, MatchStatus::Error("error".to_string()));
77
78        status = MatchStatus::Invalid;
79        status.merge(MatchStatus::NotChecked);
80        assert_eq!(status, MatchStatus::Invalid);
81
82        status.merge(MatchStatus::NotAvailable);
83        assert_eq!(status, MatchStatus::Invalid);
84
85        status = MatchStatus::NotAvailable;
86        status.merge(MatchStatus::NotChecked);
87        assert_eq!(status, MatchStatus::NotAvailable);
88    }
89}