cock_lib/cock_parts/
abnormalities.rs

1use crate::{FromString, GetVariants, Score};
2
3/// The [Abnormalities] enum represents the different types of abnormalities of the cock.
4#[derive(Debug, PartialEq, Clone, serde::Deserialize)]
5pub enum Abnormalities {
6    None,
7    Minor(String),
8    Major(String),
9}
10
11/// The [Score] trait implementation for [Abnormalities] provides a score value based on the type of abnormality.
12impl Score for Abnormalities {
13    fn score(&self) -> u32 {
14        match self {
15            Abnormalities::None => 5,
16            Abnormalities::Minor(_) => 3,
17            Abnormalities::Major(_) => 1,
18        }
19    }
20}
21
22/// The [GetVariants] trait implementation for [Abnormalities] returns a vector of the possible variants of [Abnormalities].
23impl GetVariants for Abnormalities {
24    fn get_variants() -> Vec<String> {
25        vec![
26            String::from("None"),
27            String::from("Minor"),
28            String::from("Major"),
29        ]
30    }
31}
32
33/// Additional methods for the [Abnormalities] struct.
34impl Abnormalities {
35    /// Returns a string representation of the abnormality.
36    pub fn get_abnormality(&self) -> &str {
37        match self {
38            Abnormalities::None => "None",
39            Abnormalities::Minor(minor) => minor,
40            Abnormalities::Major(major) => major,
41        }
42    }
43}
44
45/// The [FromString] trait implementation for [Abnormalities] returns an [Abnormalities] variant based on the string provided.
46impl FromString for Abnormalities {
47    fn from_string(abnormality: &str) -> Abnormalities {
48        match abnormality {
49            "None" => Abnormalities::None,
50            "Minor" => Abnormalities::Minor("".to_string()),
51            "Major" => Abnormalities::Major("".to_string()),
52            _ => panic!("Invalid abnormality"),
53        }
54    }
55}
56
57/// The [std::fmt::Display] trait implementation for [Abnormalities] returns a string representation of the abnormality.
58impl std::fmt::Display for Abnormalities {
59    /// Returns a string representation of the [Abnormalities] variant.
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        match self {
62            Abnormalities::None => write!(f, "None"),
63            Abnormalities::Minor(minor) => write!(f, "Minor: {}", minor),
64            Abnormalities::Major(major) => write!(f, "Major: {}", major),
65        }
66    }
67}
68
69/// Unit tests for the [Abnormalities] struct.
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn abnormalities_test() {
76        let major_abnormality = Abnormalities::Major(String::from("major"));
77        let minor_abnormality = Abnormalities::Minor(String::from("minor"));
78        let no_abnormality = Abnormalities::None;
79
80        assert_eq!(major_abnormality.get_abnormality(), "major");
81        assert_eq!(minor_abnormality.get_abnormality(), "minor");
82        assert_eq!(no_abnormality.get_abnormality(), "None");
83
84        assert_eq!(major_abnormality.score(), 1);
85        assert_eq!(minor_abnormality.score(), 3);
86        assert_eq!(no_abnormality.score(), 5);
87    }
88}