cock_tier/modules/
cock_handler.rs

1use crate::{CockStruct, Score, Tier, ID};
2
3/// This struct encapsulates the result of a score calculation, [CockResult].
4/// It includes the raw score and the percentage score.
5#[derive(Debug, PartialEq, Clone)]
6pub struct CockResult {
7    pub score: f32,
8    pub percentage: f32,
9}
10
11/// This struct handles all operations related to the [CockStruct] entity.
12/// It keeps track of an identifier and the [CockStruct] itself.
13#[derive(Debug, Clone)]
14pub struct CockHandler {
15    pub id: ID,
16    pub cock: CockStruct,
17}
18
19impl CockHandler {
20    /// Constructor for a new [CockHandler]    
21    pub fn new(id: ID, cock: CockStruct) -> CockHandler {
22        CockHandler { id, cock }
23    }
24
25    /// This method calculates the total score for the current [CockStruct].
26    /// The calculation takes into account several attributes of the [CockStruct].
27    /// It returns a [CockResult] containing the raw score and the percentage score.
28    pub fn total_score(&self) -> CockResult {
29        let actual_score = (
30            self.cock.aesthetic.score()         // 10
31            + self.cock.balls.score()           // 5
32            + self.cock.veininess.score()       // 5
33            + self.cock.abnormalities.score()   // 5
34            + self.cock.size.score()) as f32    // 10
35            * 2.0;
36
37        let total_possible_score = 70.0;
38
39        let percentage_score = actual_score / total_possible_score * 100.0;
40
41        CockResult {
42            score: actual_score,
43            percentage: percentage_score,
44        }
45    }
46
47    /// This method determines the grade for the current [CockStruct].
48    /// The grade is based on the percentage score.
49    pub fn grade(&self) -> Tier {
50        let percentage_score = self.total_score().percentage;
51
52        let score = percentage_score as i32;
53
54        match score {
55            91..=100 => Tier::S,
56            81..=90 => Tier::A,
57            71..=80 => Tier::B,
58            61..=70 => Tier::C,
59            51..=60 => Tier::D,
60            41..=50 => Tier::E,
61            _ => Tier::F,
62        }
63    }
64}
65
66/// This implementation of [std::fmt::Display] allows a [CockHandler] to be converted to a string for easy display.
67impl std::fmt::Display for CockHandler {
68    /// Returns a string representation of the [CockHandler] variant.
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        let total_score = self.total_score();
71        let grade = self.grade();
72
73        writeln!(f, "--- ID ---\n{}\n", self.id)?;
74        writeln!(f, "--- Cock Info ---\n{}\n", self.cock)?;
75        writeln!(f, "Score: {}", total_score.score)?;
76        writeln!(f, "Percentage: {:.2}%", total_score.percentage)?;
77        write!(f, "Grade: {:?}", grade)
78    }
79}
80
81/// add testing
82#[cfg(test)]
83mod tests {}