cock_lib/cock_parts/
shape.rs

1use crate::{FromString, GetVariants};
2
3/// Enum representing the [Shape] for a cock.
4/// The shapes include [Shape::Cylindrical], [Shape::Tapered] and an [Shape::Other] variant that can store a custom [Shape] description as a string.
5#[derive(Debug, PartialEq, Clone, serde::Deserialize)]
6pub enum Shape {
7    Cylindrical,
8    Tapered,
9    Other(String),
10}
11
12impl Shape {
13    /// Returns the string description of a [Shape] instance.
14    pub fn get_shape(&self) -> &str {
15        match self {
16            Shape::Cylindrical => "Cylindrical",
17            Shape::Tapered => "Tapered",
18            Shape::Other(other) => other,
19        }
20    }
21}
22
23/// Implementation of the [FromString] trait for [Shape]. This allows a [Shape] instance to be created from a string value.
24/// The [Shape::Other] variant involves a user prompt for a custom shape description.
25impl FromString for Shape {
26    fn from_string(shape: &str) -> Shape {
27        match shape {
28            "Cylindrical" => Shape::Cylindrical,
29            "Tapered" => Shape::Tapered,
30            "Other" => Shape::Other("".to_string()),
31            _ => panic!("Invalid shape"),
32        }
33    }
34}
35
36/// Implementation of the [GetVariants] trait for [Shape]. This enables the creation of a vector containing all possible variants as string values.
37impl GetVariants for Shape {
38    fn get_variants() -> Vec<String> {
39        vec![
40            String::from("Cylindrical"),
41            String::from("Tapered"),
42            String::from("Other"),
43        ]
44    }
45}
46
47/// Implementation of the [std::fmt::Display] trait for [Shape]. This allows a [Shape] instance to be converted to a string for display purposes.
48/// For the [Shape::Other] variant, the custom shape description is displayed.
49impl std::fmt::Display for Shape {
50    /// Returns the string description of a [Shape] instance.
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        match self {
53            Shape::Cylindrical => write!(f, "Cylindrical"),
54            Shape::Tapered => write!(f, "Tapered"),
55            Shape::Other(other) => write!(f, "{}", other),
56        }
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_shape() {
66        let cylindrical = Shape::Cylindrical;
67        let tapered = Shape::Tapered;
68        let other = Shape::Other(String::from("test"));
69
70        assert_eq!(cylindrical, Shape::Cylindrical);
71        assert_eq!(tapered, Shape::Tapered);
72        assert_eq!(other, Shape::Other(String::from("test")));
73    }
74}