dd_sds/match_validation/
match_status.rs

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