cock_lib/cock_parts/
balls.rs

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