Skip to main content

eml_nl/utils/
affiliation_type.rs

1use thiserror::Error;
2
3use crate::{EMLError, EMLValueResultExt as _, utils::StringValueData};
4
5/// Affiliation type used in the election.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum AffiliationType {
8    /// lijstengroep
9    GroupOfLists,
10    /// stel gelijkluidende lijsten
11    SetOfEqualLists,
12    /// op zichzelf staande lijst
13    StandAloneList,
14}
15
16impl AffiliationType {
17    /// Create a new AffiliationType from a string, validating its format
18    pub fn new(s: impl AsRef<str>) -> Result<Self, EMLError> {
19        Self::from_eml_value(s).wrap_value_error()
20    }
21
22    /// Create an [`AffiliationType`] from a `&str`, if possible.
23    pub fn from_eml_value(s: impl AsRef<str>) -> Result<Self, UnknownAffiliationTypeError> {
24        let data = s.as_ref();
25        match data {
26            "lijstengroep" => Ok(AffiliationType::GroupOfLists),
27            "stel gelijkluidende lijsten" => Ok(AffiliationType::SetOfEqualLists),
28            "op zichzelf staande lijst" => Ok(AffiliationType::StandAloneList),
29            _ => Err(UnknownAffiliationTypeError(data.to_string())),
30        }
31    }
32
33    /// Get the `&str` representation of this [`AffiliationType`].
34    pub fn to_eml_value(&self) -> &'static str {
35        match self {
36            AffiliationType::GroupOfLists => "lijstengroep",
37            AffiliationType::SetOfEqualLists => "stel gelijkluidende lijsten",
38            AffiliationType::StandAloneList => "op zichzelf staande lijst",
39        }
40    }
41}
42
43/// Error returned when an unknown affiliation type string is encountered.
44#[derive(Debug, Clone, Error, PartialEq, Eq)]
45#[error("Unknown affiliation type: {0}")]
46pub struct UnknownAffiliationTypeError(String);
47
48impl StringValueData for AffiliationType {
49    type Error = UnknownAffiliationTypeError;
50    fn parse_from_str(s: &str) -> Result<Self, Self::Error>
51    where
52        Self: Sized,
53    {
54        Self::from_eml_value(s)
55    }
56
57    fn to_raw_value(&self) -> String {
58        self.to_eml_value().to_string()
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_affiliation_type_from_str() {
68        assert_eq!(
69            AffiliationType::from_eml_value("lijstengroep"),
70            Ok(AffiliationType::GroupOfLists)
71        );
72        assert_eq!(
73            AffiliationType::from_eml_value("stel gelijkluidende lijsten"),
74            Ok(AffiliationType::SetOfEqualLists)
75        );
76        assert_eq!(
77            AffiliationType::from_eml_value("op zichzelf staande lijst"),
78            Ok(AffiliationType::StandAloneList)
79        );
80        assert_eq!(
81            AffiliationType::from_eml_value("UNKNOWN"),
82            Err(UnknownAffiliationTypeError("UNKNOWN".to_string()))
83        );
84    }
85
86    #[test]
87    fn test_affiliation_type_to_str() {
88        assert_eq!(AffiliationType::GroupOfLists.to_eml_value(), "lijstengroep");
89        assert_eq!(
90            AffiliationType::SetOfEqualLists.to_eml_value(),
91            "stel gelijkluidende lijsten"
92        );
93        assert_eq!(
94            AffiliationType::StandAloneList.to_eml_value(),
95            "op zichzelf staande lijst"
96        );
97    }
98}