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
/// A Haystack Number, encapsulating a scalar value and
/// an optional unit value. The unit is represented as a
/// string.
#[derive(Clone, Debug, PartialEq)]
pub struct Number {
    value: f64,
    unit: Option<String>,
}

impl Number {
    /// Create a new `Number`. If present, the unit should
    /// be a valid unit string from Project Haystack's
    /// unit database.
    pub fn new(value: f64, unit: Option<String>) -> Self {
        Self { value, unit }
    }

    /// Return the numeric component of this `Number`.
    pub fn value(&self) -> f64 {
        self.value
    }

    /// Return the unit component of this `Number`, if present.
    pub fn unit(&self) -> Option<&str> {
        self.unit.as_ref().map(|unit| unit.as_ref())
    }

    /// Parse a `Number` from a number encoded in a JSON string.
    /// # Example
    /// ```rust
    /// use raystack::Number;
    ///
    /// let n = Number::new(1.0, Some("pH".to_owned()));
    /// assert_eq!(Number::from_encoded_json_string("n:1.0 pH").unwrap(), n);
    /// ```
    pub fn from_encoded_json_string(
        json_string: &str,
    ) -> Result<Self, ParseNumberError> {
        let json_string = json_string.replacen("n:", "", 1);
        let mut split = json_string.trim().split(' ');
        let number_str = split.next();
        let unit_str = split.next();

        if let Some(number_str) = number_str {
            let number = if number_str == "INF" {
                std::f64::INFINITY
            } else if number_str == "-INF" {
                std::f64::NEG_INFINITY
            } else {
                number_str
                    .parse()
                    .map_err(|_| ParseNumberError::from_str(&json_string))?
            };
            let unit = unit_str.map(|unit_str| unit_str.trim().to_string());
            Ok(Number::new(number, unit))
        } else {
            Err(ParseNumberError::from_str(&json_string))
        }
    }
}

/// An error indicating that a `Number` could not be parsed.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParseNumberError {
    unparsable_number: String,
}

impl ParseNumberError {
    pub(crate) fn from_str(s: &str) -> Self {
        Self {
            unparsable_number: s.to_string(),
        }
    }
}

impl std::fmt::Display for ParseNumberError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Could not parse Number from string {}",
            self.unparsable_number
        )
    }
}

impl std::error::Error for ParseNumberError {}

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

    #[test]
    fn from_encoded_json_string() {
        let unitless = "n:45.5";
        assert_eq!(
            Number::from_encoded_json_string(unitless).unwrap().value(),
            45.5
        );

        let unit = "n:73.2 °F";
        let number_with_unit = Number::from_encoded_json_string(unit).unwrap();
        assert_eq!(number_with_unit.value(), 73.2);
        assert_eq!(number_with_unit.unit(), Some("°F"))
    }

    #[test]
    fn from_encoded_json_string_infinity() {
        let unitless = "n:INF";
        assert_eq!(
            Number::from_encoded_json_string(unitless).unwrap().value(),
            std::f64::INFINITY,
        );

        let unit = "n:INF °F";
        let number_with_unit = Number::from_encoded_json_string(unit).unwrap();
        assert_eq!(number_with_unit.value(), std::f64::INFINITY);
        assert_eq!(number_with_unit.unit(), Some("°F"))
    }

    #[test]
    fn from_encoded_json_string_neg_infinity() {
        let unitless = "n:-INF";
        assert_eq!(
            Number::from_encoded_json_string(unitless).unwrap().value(),
            std::f64::NEG_INFINITY,
        );

        let unit = "n:-INF °F";
        let number_with_unit = Number::from_encoded_json_string(unit).unwrap();
        assert_eq!(number_with_unit.value(), std::f64::NEG_INFINITY);
        assert_eq!(number_with_unit.unit(), Some("°F"))
    }

    #[test]
    fn from_encoded_json_string_signless_nan() {
        let unitless = "n:NaN";
        assert!(Number::from_encoded_json_string(unitless)
            .unwrap()
            .value()
            .is_nan());

        let unit = "n:NaN °F";
        let number_with_unit = Number::from_encoded_json_string(unit).unwrap();
        assert!(number_with_unit.value().is_nan());
        assert_eq!(number_with_unit.unit(), Some("°F"))
    }

    #[test]
    fn from_encoded_json_string_signed_nan() {
        let unitless = "n:-NaN";
        assert!(Number::from_encoded_json_string(unitless)
            .unwrap()
            .value()
            .is_nan());

        let unit = "n:+NaN °F";
        let number_with_unit = Number::from_encoded_json_string(unit).unwrap();
        assert!(number_with_unit.value().is_nan());
        assert_eq!(number_with_unit.unit(), Some("°F"))
    }
}