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 MatchStatus {
12    // Order matters as we want to update the match_status only if the new match_status has higher priority.
13    // (in case of split key where we try different combinations of id and secret (aws use-case))
14    pub fn merge(&mut self, new_status: MatchStatus) {
15        if new_status > *self {
16            *self = new_status;
17        }
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn test_merge() {
27        let mut status = MatchStatus::NotChecked;
28        status.merge(MatchStatus::NotAvailable);
29        assert_eq!(status, MatchStatus::NotAvailable);
30
31        status.merge(MatchStatus::Invalid);
32        assert_eq!(status, MatchStatus::Invalid);
33
34        status.merge(MatchStatus::Error("error".to_string()));
35        assert_eq!(status, MatchStatus::Error("error".to_string()));
36
37        status.merge(MatchStatus::Valid);
38        assert_eq!(status, MatchStatus::Valid);
39    }
40    #[test]
41    fn test_merge_lower_prio() {
42        let mut status = MatchStatus::Valid;
43        status.merge(MatchStatus::NotChecked);
44        assert_eq!(status, MatchStatus::Valid);
45
46        status.merge(MatchStatus::NotAvailable);
47        assert_eq!(status, MatchStatus::Valid);
48
49        status.merge(MatchStatus::Invalid);
50        assert_eq!(status, MatchStatus::Valid);
51
52        status.merge(MatchStatus::Error("error".to_string()));
53        assert_eq!(status, MatchStatus::Valid);
54
55        status = MatchStatus::Error("error".to_string());
56        status.merge(MatchStatus::NotChecked);
57
58        assert_eq!(status, MatchStatus::Error("error".to_string()));
59
60        status.merge(MatchStatus::NotAvailable);
61        assert_eq!(status, MatchStatus::Error("error".to_string()));
62
63        status.merge(MatchStatus::Invalid);
64        assert_eq!(status, MatchStatus::Error("error".to_string()));
65
66        status = MatchStatus::Invalid;
67        status.merge(MatchStatus::NotChecked);
68        assert_eq!(status, MatchStatus::Invalid);
69
70        status.merge(MatchStatus::NotAvailable);
71        assert_eq!(status, MatchStatus::Invalid);
72
73        status = MatchStatus::NotAvailable;
74        status.merge(MatchStatus::NotChecked);
75        assert_eq!(status, MatchStatus::NotAvailable);
76    }
77}