summaryrefslogtreecommitdiff
path: root/src/values/rotation.rs
blob: da291b2cb9966596be3445e2f973d98098ae4fdc (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::num::ParseFloatError;
use std::str::FromStr;

use iced::{Radians, Rotation};

use super::Value;

#[derive(Debug, thiserror::Error, Clone, PartialEq)]
pub enum ParseRotationError {
    #[error("float parsing error: {0}")]
    ParseFloatError(ParseFloatError),
    #[error("invalid prefix")]
    InvalidPrefix,
    #[error("cannot parse rotation from empty string")]
    Empty,
}

impl From<ParseFloatError> for ParseRotationError {
    fn from(value: ParseFloatError) -> Self {
        Self::ParseFloatError(value)
    }
}

impl Value for Rotation {
    type Err = ParseRotationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            return Err(ParseRotationError::Empty);
        }

        if s.starts_with(|c: char| !c.is_ascii_digit()) {
            let (prefix, value) = s.split_at(1);
            match prefix.to_lowercase().as_str() {
                "s" => Ok(Rotation::Solid(Radians(f32::from_str(value)?))),
                "f" => Ok(Rotation::Floating(Radians(f32::from_str(value)?))),
                _ => Err(ParseRotationError::InvalidPrefix),
            }
        } else {
            Ok(Rotation::Floating(Radians(f32::from_str(s)?)))
        }
    }

    fn to_string(&self) -> String {
        match self {
            Self::Floating(value) => format!("f{}", value),
            Self::Solid(value) => format!("s{}", value),
        }
    }
}

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

    #[test]
    fn can_parse_without_prefix() {
        assert_eq!(
            Rotation::from_str("10.5"),
            Ok(Rotation::Floating(Radians(10.5)))
        )
    }

    #[test]
    fn can_parse_with_s_prefix() {
        assert_eq!(
            Rotation::from_str("s12.3"),
            Ok(Rotation::Solid(Radians(12.3)))
        )
    }

    #[test]
    fn can_parse_with_f_prefix() {
        assert_eq!(
            Rotation::from_str("f16.9"),
            Ok(Rotation::Floating(Radians(16.9)))
        )
    }

    #[test]
    fn can_parse_with_uppercase_prefix() {
        assert_eq!(
            Rotation::from_str("S9.4"),
            Ok(Rotation::Solid(Radians(9.4)))
        )
    }

    #[test]
    fn cant_parse_invalid_prefix() {
        assert_eq!(
            Rotation::from_str("a6.0"),
            Err(ParseRotationError::InvalidPrefix)
        )
    }

    #[test]
    fn cant_parse_invalid_float() {
        assert_eq!(
            Rotation::from_str("3.a"),
            Err(ParseRotationError::ParseFloatError(
                f32::from_str("3.a").expect_err("")
            ))
        )
    }

    #[test]
    fn cant_parse_empty_string() {
        assert_eq!(Rotation::from_str(""), Err(ParseRotationError::Empty))
    }
}