cock_lib/cock_parts/
curvature.rs

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