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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/// A Haystack number.
#[derive(Clone, Debug, PartialEq)]
pub enum Number {
    Basic(BasicNumber),
    Scientific(ScientificNumber),
}

impl std::fmt::Display for Number {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Basic(num) => write!(f, "{}", num),
            Self::Scientific(ex) => write!(f, "{}", ex),
        }
    }
}

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 {
        Number::Basic(BasicNumber::new(value, unit))
    }

    /// Create a new `Number` and no unit.
    pub fn new_unitless(value: f64) -> Self {
        Self::new(value, None)
    }

    /// Create a new scientific notation `Number`. If present, the unit should
    /// be a valid unit string from Project Haystack's
    /// unit database.
    pub fn new_scientific(
        significand: f64,
        exponent: i32,
        unit: Option<String>,
    ) -> Option<Self> {
        Some(Number::Scientific(ScientificNumber::new(
            significand,
            exponent,
            unit,
        )?))
    }

    /// Create a new scientific notation `Number` and no unit.
    pub fn new_scientific_unitless(
        significand: f64,
        exponent: i32,
    ) -> Option<Self> {
        Self::new_scientific(significand, exponent, None)
    }

    /// If this represents a non-scientific notation number, return the number.
    pub fn as_number(&self) -> Option<&BasicNumber> {
        match self {
            Self::Basic(number) => Some(number),
            _ => None,
        }
    }

    /// If this represents a number in scientific notation,
    /// return the scientific notation number.
    pub fn as_scientific_number(&self) -> Option<&ScientificNumber> {
        match self {
            Self::Scientific(ex) => Some(ex),
            _ => None,
        }
    }

    /// Return the unit component of this `Number`, if present.
    pub fn unit(&self) -> Option<&str> {
        match self {
            Self::Basic(num) => num.unit(),
            Self::Scientific(ex) => ex.unit(),
        }
    }

    /// Return a string containing Axon code representing this number.
    pub fn to_axon_code(&self) -> String {
        match self {
            Self::Basic(num) => num.to_axon_code(),
            Self::Scientific(ex) => ex.to_axon_code(),
        }
    }
}

/// A Haystack Number, encapsulating a scalar value and
/// an optional unit value. The unit is represented as a
/// string. This does not represent Haystack scientific notation numbers.
#[derive(Clone, Debug, PartialEq)]
pub struct BasicNumber {
    value: f64,
    unit: Option<String>,
}

impl BasicNumber {
    /// Create a new `BasicNumber`. 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 }
    }

    /// Create a new `BasicNumber` with no unit.
    pub fn new_unitless(value: f64) -> Self {
        Self::new(value, None)
    }

    /// 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())
    }

    /// Return a string containing Axon code representing this number.
    pub fn to_axon_code(&self) -> String {
        let value = self.value();
        if let Some(unit) = self.unit() {
            if value.is_nan() {
                format!("nan().as(\"{}\")", unit)
            } else if value.is_infinite() && value.is_sign_positive() {
                format!("posInf().as(\"{}\")", unit)
            } else if value.is_infinite() && value.is_sign_negative() {
                format!("negInf().as(\"{}\")", unit)
            } else {
                format!("{}{}", value, unit)
            }
        } else {
            if value.is_nan() {
                "nan()".to_owned()
            } else if value.is_infinite() && value.is_sign_positive() {
                "posInf()".to_owned()
            } else if value.is_infinite() && value.is_sign_negative() {
                "negInf()".to_owned()
            } else {
                format!("{}", value)
            }
        }
    }
}

impl std::fmt::Display for BasicNumber {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = self.value();
        if value.is_nan() {
            if let Some(unit) = self.unit() {
                write!(f, "NaN {}", unit)
            } else {
                write!(f, "NaN")
            }
        } else if value.is_infinite() && value.is_sign_positive() {
            if let Some(unit) = self.unit() {
                write!(f, "INF {}", unit)
            } else {
                write!(f, "INF")
            }
        } else if value.is_infinite() && value.is_sign_negative() {
            if let Some(unit) = self.unit() {
                write!(f, "-INF {}", unit)
            } else {
                write!(f, "-INF")
            }
        } else if let Some(unit) = self.unit() {
            write!(f, "{} {}", value, unit)
        } else {
            write!(f, "{}", value)
        }
    }
}

/// A Haystack scientific notation Number, encapsulating a value and
/// an optional unit value. The unit is represented as a
/// string.
#[derive(Clone, Debug, PartialEq)]
pub struct ScientificNumber {
    significand: f64,
    exponent: i32,
    unit: Option<String>,
}

impl ScientificNumber {
    /// Create a new `ScientificNumber`. If present, the unit should
    /// be a valid unit string from Project Haystack's
    /// unit database. The significand must be a finite number which is
    /// not NaN.
    pub fn new(
        significand: f64,
        exponent: i32,
        unit: Option<String>,
    ) -> Option<Self> {
        if significand.is_nan() || significand.is_infinite() {
            None
        } else {
            Some(Self {
                significand,
                exponent,
                unit,
            })
        }
    }

    /// Create a new `ScientificNumber` with no unit. The significand must
    /// be a finite number which is not NaN.
    pub fn new_unitless(significand: f64, exponent: i32) -> Option<Self> {
        Self::new(significand, exponent, None)
    }

    /// Return the numeric significand component of this number.
    pub fn significand(&self) -> f64 {
        self.significand
    }

    /// Return the numeric exponent component of this number.
    pub fn exponent(&self) -> i32 {
        self.exponent
    }

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

    /// Return a string containing Axon code representing this number.
    pub fn to_axon_code(&self) -> String {
        let exp = self.exponent();
        let sig = self.significand();
        if let Some(unit) = self.unit() {
            format!("{}e{}{}", sig, exp, unit)
        } else {
            format!("{}e{}", sig, exp)
        }
    }
}

impl std::fmt::Display for ScientificNumber {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let exp = self.exponent();
        let sig = self.significand();
        if let Some(unit) = self.unit() {
            write!(f, "{}e{} {}", sig, exp, unit)
        } else {
            write!(f, "{}e{}", sig, exp)
        }
    }
}