1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::{FromString, GetVariants};

/// The [Tier] enum represents the grade given to a cock.
#[derive(Debug, PartialEq, Clone)]
pub enum Tier {
    S,
    A,
    B,
    C,
    D,
    E,
    F,
}

/// The [GetVariants] trait implementation for [Tier] returns a vector of the possible variants of [Tier].
impl GetVariants for Tier {
    fn get_variants() -> Vec<String> {
        vec![
            String::from("S"),
            String::from("A"),
            String::from("B"),
            String::from("C"),
            String::from("D"),
            String::from("E"),
            String::from("F"),
        ]
    }
}

/// The [FromString] trait implementation for [Tier] returns a variant of [Tier].
impl FromString for Tier {
    fn from_string(tier: &str) -> Tier {
        match tier {
            "S" => Tier::S,
            "A" => Tier::A,
            "B" => Tier::B,
            "C" => Tier::C,
            "D" => Tier::D,
            "E" => Tier::E,
            "F" => Tier::F,
            _ => panic!("Invalid tier"),
        }
    }
}

/// The [std::fmt::Display] trait implementation for [Tier] returns a string representation of the [Tier] variant.
impl std::fmt::Display for Tier {
    /// Returns a string representation of the [Tier] variant.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Tier::S => write!(f, "S"),
            Tier::A => write!(f, "A"),
            Tier::B => write!(f, "B"),
            Tier::C => write!(f, "C"),
            Tier::D => write!(f, "D"),
            Tier::E => write!(f, "E"),
            Tier::F => write!(f, "F"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tier() {
        let s = Tier::S;
        let a = Tier::A;
        let b = Tier::B;
        let c = Tier::C;
        let d = Tier::D;
        let e = Tier::E;
        let f = Tier::F;

        assert_eq!(s, Tier::S);
        assert_eq!(a, Tier::A);
        assert_eq!(b, Tier::B);
        assert_eq!(c, Tier::C);
        assert_eq!(d, Tier::D);
        assert_eq!(e, Tier::E);
        assert_eq!(f, Tier::F);
    }
}