gedcomx/conclusion/
confidencelevel.rs

1use std::fmt;
2
3use quickcheck::{Arbitrary, Gen};
4use serde::{Deserialize, Serialize};
5
6use crate::{EnumAsString, Uri};
7
8/// Levels of confidence.
9#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
10#[non_exhaustive]
11#[serde(from = "EnumAsString", into = "EnumAsString")]
12pub enum ConfidenceLevel {
13    /// The contributor has a high degree of confidence that the assertion is
14    /// true.
15    High,
16    /// The contributor has a medium degree of confidence that the assertion is
17    /// true.
18    Medium,
19    /// The contributor has a low degree of confidence that the assertion is
20    /// true.
21    Low,
22    Custom(Uri),
23}
24
25impl_enumasstring_yaserialize_yadeserialize!(ConfidenceLevel, "ConfidenceLevel");
26
27impl From<EnumAsString> for ConfidenceLevel {
28    fn from(f: EnumAsString) -> Self {
29        match f.0.as_ref() {
30            "http://gedcomx.org/High" => Self::High,
31            "http://gedcomx.org/Medium" => Self::Medium,
32            "http://gedcomx.org/Low" => Self::Low,
33            _ => Self::Custom(f.0.into()),
34        }
35    }
36}
37
38impl fmt::Display for ConfidenceLevel {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
40        match self {
41            Self::High => write!(f, "http://gedcomx.org/High"),
42            Self::Medium => write!(f, "http://gedcomx.org/Medium"),
43            Self::Low => write!(f, "http://gedcomx.org/Low"),
44            Self::Custom(c) => write!(f, "{c}"),
45        }
46    }
47}
48
49impl Default for ConfidenceLevel {
50    fn default() -> Self {
51        Self::Custom(Uri::default())
52    }
53}
54
55impl Arbitrary for ConfidenceLevel {
56    fn arbitrary(g: &mut Gen) -> Self {
57        let options = vec![
58            Self::High,
59            Self::Medium,
60            Self::Low,
61            Self::Custom(Uri::arbitrary(g)),
62        ];
63
64        g.choose(&options).unwrap().clone()
65    }
66}
67
68#[cfg(test)]
69mod test {
70    use yaserde::ser::Config;
71
72    use super::*;
73
74    #[test]
75    fn roundtrip_to_string() {
76        let variant = ConfidenceLevel::High;
77        let s = variant.to_string();
78        let roundtripped: ConfidenceLevel = EnumAsString::from(s).into();
79        assert_eq!(variant, roundtripped);
80    }
81
82    #[test]
83    fn roundtrip_to_string_custom() {
84        let variant = ConfidenceLevel::Custom("custom uri".into());
85        let s = variant.to_string();
86        let roundtripped: ConfidenceLevel = EnumAsString::from(s).into();
87        assert_eq!(variant, roundtripped);
88    }
89
90    #[test]
91    fn deserialize() {
92        let xml = "<ConfidenceLevel>http://gedcomx.org/High</ConfidenceLevel>";
93        let cl: ConfidenceLevel = yaserde::de::from_str(xml).unwrap();
94        assert_eq!(cl, ConfidenceLevel::High);
95    }
96
97    #[test]
98    fn deserialize_custom() {
99        let xml = "<ConfidenceLevel>this is a test</ConfidenceLevel>";
100        let cl: ConfidenceLevel = yaserde::de::from_str(xml).unwrap();
101        assert_eq!(cl, ConfidenceLevel::Custom("this is a test".into()));
102    }
103
104    #[test]
105    fn serialize() {
106        let config = Config {
107            write_document_declaration: false,
108            ..Default::default()
109        };
110        let actual = yaserde::ser::to_string_with_config(&ConfidenceLevel::High, &config).unwrap();
111        let expected = "http://gedcomx.org/High";
112
113        assert_eq!(actual, expected);
114    }
115
116    #[test]
117    fn serialize_custom() {
118        let config = Config {
119            write_document_declaration: false,
120            ..Default::default()
121        };
122        let actual = yaserde::ser::to_string_with_config(
123            &ConfidenceLevel::Custom("this is a test".into()),
124            &config,
125        )
126        .unwrap();
127        let expected = "this is a test";
128
129        assert_eq!(actual, expected);
130    }
131
132    #[quickcheck_macros::quickcheck]
133    fn roundtrip_json(input: ConfidenceLevel) -> bool {
134        let json = serde_json::to_string(&input).unwrap();
135        let from_json: ConfidenceLevel = serde_json::from_str(&json).unwrap();
136        input == from_json
137    }
138}