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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::f64::consts::{PI, TAU};
use std::fmt;

/// A radian confined to the range of [0.0; 2π)
#[derive(Debug, PartialEq, Copy, Clone, Default, Serialize, Deserialize)]
pub struct Radians {
    value: f64,
}

impl Radians {
    /// Creates a new instance of [`Radians`].
    ///
    /// ### Errors
    /// Returns a [`RadiansError`] if the given value is outside the range [0.0; 2π)
    ///
    /// ### Examples
    /// ```
    /// use myelin_geometry::Radians;
    /// use std::f64::consts::PI;
    ///
    /// let rotation = Radians::try_new(PI).expect("Value was outside the range [0.0; 2π)");
    /// ```
    pub fn try_new(value: f64) -> Result<Self, RadiansError> {
        const VALID_VALUES_RANGE: std::ops::Range<f64> = 0.0..TAU;
        if VALID_VALUES_RANGE.contains(&value) {
            Ok(Radians { value })
        } else {
            Err(RadiansError::OutOfRange)
        }
    }

    /// Returns the underlying value
    pub fn value(self) -> f64 {
        self.value
    }

    /// Convert degrees to radians
    ///
    /// ### Errors
    /// Returns a [`RadiansError`] if the given value is outside the range [0.0°; 360°)
    ///
    /// ### Examples
    /// ```
    /// use myelin_geometry::Radians;
    /// use std::f64::consts::FRAC_PI_2;
    /// use std::f64::consts::PI;
    ///
    /// use nearly_eq::assert_nearly_eq;
    ///
    /// assert_nearly_eq!(FRAC_PI_2, Radians::try_from_degrees(90.0).unwrap().value());
    /// ```
    pub fn try_from_degrees(degrees: f64) -> Result<Self, RadiansError> {
        const MAX_DEGREES: f64 = 360.0;
        const MAX_RADIANS: f64 = 2.0 * PI;

        Radians::try_new(degrees / MAX_DEGREES * MAX_RADIANS)
    }
}

/// The reason why a [`Radians`] instance could not be created
#[derive(Debug)]
pub enum RadiansError {
    /// The given value was not in the range [0.0; 2π)
    OutOfRange,
}

impl fmt::Display for RadiansError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Given value is not in range [0.0; 2π)")
    }
}

impl Error for RadiansError {}

#[cfg(test)]
mod tests {
    use super::*;
    use nearly_eq::assert_nearly_eq;
    use std::f64::consts::PI;

    #[test]
    fn radians_new_with_negative_0_point_1_is_none() {
        let radians = Radians::try_new(-0.1);
        assert!(radians.is_err())
    }

    #[test]
    fn radians_new_with_0_is_some() {
        let radians = Radians::try_new(0.0);
        assert!(radians.is_ok())
    }

    #[test]
    fn radians_new_with_1_point_9_pi_is_some() {
        let radians = Radians::try_new(1.9 * PI);
        assert!(radians.is_ok())
    }

    #[test]
    fn radians_new_with_2_pi_is_none() {
        let radians = Radians::try_new(2.0 * PI);
        assert!(radians.is_err())
    }

    #[test]
    fn radians_value_returns_1_when_given_1() {
        let value = 1.0;
        let radians = Radians::try_new(value).unwrap();
        assert_nearly_eq!(value, radians.value())
    }

    #[test]
    fn try_from_degrees_works_with_0_as_input() {
        let degrees = 0.0;
        let radians = Radians::try_from_degrees(degrees).unwrap();
        let expected = 0.0;
        assert_nearly_eq!(expected, radians.value())
    }

    #[test]
    fn try_from_degrees_returns_none_with_359_as_input() {
        let degrees = 359.0;
        let radians = Radians::try_from_degrees(degrees).unwrap();
        let expected = (2.0 * PI) - (PI / 180.0);
        assert_nearly_eq!(expected, radians.value())
    }

    #[test]
    fn try_from_degrees_works_with_180_as_input() {
        let degrees = 180.0;
        let radians = Radians::try_from_degrees(degrees).unwrap();
        let expected = PI;
        assert_nearly_eq!(expected, radians.value())
    }

    #[test]
    fn try_from_degrees_works_with_1_as_input() {
        let degrees = 1.0;
        let radians = Radians::try_from_degrees(degrees).unwrap();
        let expected = PI / 180.0;
        assert_nearly_eq!(expected, radians.value())
    }

    #[test]
    fn try_from_degrees_works_with_360_as_input() {
        let degrees = 360.0;
        let radians = Radians::try_from_degrees(degrees);
        assert!(radians.is_err());
    }

    #[test]
    fn try_from_degrees_returns_none_with_negative_1_as_input() {
        let degrees = -1.0;
        let radians = Radians::try_from_degrees(degrees);
        assert!(radians.is_err());
    }

    #[test]
    fn try_from_degrees_returns_none_with_361_as_input() {
        let degrees = 361.0;
        let radians = Radians::try_from_degrees(degrees);
        assert!(radians.is_err());
    }
}