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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! Types and constants for handling temperature.

use super::measurement::*;

/// The `Temperature` struct can be used to deal with absolute temperatures in
/// a common way.
///
/// # Example
///
/// ```
/// use measurements::Temperature;
///
/// let boiling_water = Temperature::from_celsius(100.0);
/// let fahrenheit = boiling_water.as_fahrenheit();
/// println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
/// ```
#[derive(Copy, Clone, Debug)]
pub struct Temperature {
    degrees_kelvin: f64,
}

/// The `TemperatureDelta` struct can be used to deal with differences between
/// temperatures in a common way.
///
/// # Example
///
/// ```
/// use measurements::{Temperature, TemperatureDelta};
///
/// let boiling_water = Temperature::from_celsius(100.0);
/// let frozen_water = Temperature::from_celsius(0.0);
/// let difference: TemperatureDelta = boiling_water - frozen_water;
/// println!("Boiling water is {} above freezing.", difference);
/// ```
#[derive(Copy, Clone, Debug)]
pub struct TemperatureDelta {
    kelvin_degrees: f64,
}

impl TemperatureDelta {
    /// Create a new TemperatureDelta from a floating point value in Kelvin
    pub fn from_kelvin(kelvin_degrees: f64) -> Self {
        TemperatureDelta {
            kelvin_degrees: kelvin_degrees,
        }
    }

    /// Create a new TemperatureDelta from a floating point value in Celsius
    pub fn from_celsius(celsius_degrees: f64) -> Self {
        TemperatureDelta::from_kelvin(celsius_degrees)
    }

    /// Create a new TemperatureDelta from a floating point value in Fahrenheit
    pub fn from_fahrenheit(farenheit_degrees: f64) -> Self {
        TemperatureDelta {
            kelvin_degrees: farenheit_degrees / 1.8,
        }
    }

    /// Create a new TemperatureDelta from a floating point value in Rankine
    pub fn from_rankine(rankine_degrees: f64) -> Self {
        TemperatureDelta {
            kelvin_degrees: rankine_degrees / 1.8,
        }
    }

    /// Convert this TemperatureDelta to a floating point value in Kelvin
    pub fn as_kelvin(&self) -> f64 {
        self.kelvin_degrees
    }

    /// Convert this TemperatureDelta to a floating point value in Celsius
    pub fn as_celsius(&self) -> f64 {
        self.kelvin_degrees
    }

    /// Convert this TemperatureDelta to a floating point value in Fahrenheit
    pub fn as_fahrenheit(&self) -> f64 {
        self.kelvin_degrees * 1.8
    }

    /// Convert this TemperatureDelta to a floating point value in Rankine
    pub fn as_rankine(&self) -> f64 {
        self.kelvin_degrees * 1.8
    }
}

impl Temperature {
    /// Create a new Temperature from a floating point value in Kelvin
    pub fn from_kelvin(degrees_kelvin: f64) -> Self {
        Temperature {
            degrees_kelvin: degrees_kelvin,
        }
    }

    /// Create a new Temperature from a floating point value in Celsius
    pub fn from_celsius(degrees_celsius: f64) -> Self {
        Self::from_kelvin(degrees_celsius + 273.15)
    }

    /// Create a new Temperature from a floating point value in Fahrenheit
    pub fn from_fahrenheit(degrees_fahrenheit: f64) -> Self {
        Self::from_kelvin((degrees_fahrenheit - 32.0) / 1.8 + 273.15)
    }

    /// Create a new Temperature from a floating point value in Rankine
    pub fn from_rankine(degrees_rankine: f64) -> Self {
        Self::from_kelvin((degrees_rankine - 491.67) / 1.8 + 273.15)
    }

    /// Convert this absolute Temperature to a floating point value in Kelvin
    pub fn as_kelvin(&self) -> f64 {
        self.degrees_kelvin
    }

    /// Convert this absolute Temperature to a floating point value in Celsius
    pub fn as_celsius(&self) -> f64 {
        self.degrees_kelvin - 273.15
    }

    /// Convert this absolute Temperature to a floating point value in Fahrenheit
    pub fn as_fahrenheit(&self) -> f64 {
        (self.degrees_kelvin - 273.15) * 1.8 + 32.0
    }

    /// Convert this absolute Temperature to a floating point value in Rankine
    pub fn as_rankine(&self) -> f64 {
        (self.degrees_kelvin - 273.15) * 1.8 + 491.67
    }
}

impl Measurement for Temperature {
    fn as_base_units(&self) -> f64 {
        self.degrees_kelvin
    }

    fn from_base_units(degrees_kelvin: f64) -> Self {
        Self::from_kelvin(degrees_kelvin)
    }

    fn get_base_units_name(&self) -> &'static str {
        "K"
    }
}

impl Measurement for TemperatureDelta {
    fn as_base_units(&self) -> f64 {
        self.kelvin_degrees
    }

    fn from_base_units(kelvin_degrees: f64) -> Self {
        Self::from_kelvin(kelvin_degrees)
    }

    fn get_base_units_name(&self) -> &'static str {
        "K"
    }
}

impl ::std::ops::Add<TemperatureDelta> for Temperature {
    type Output = Temperature;

    fn add(self, other: TemperatureDelta) -> Temperature {
        Temperature::from_kelvin(self.degrees_kelvin + other.kelvin_degrees)
    }
}

impl ::std::ops::Add<Temperature> for TemperatureDelta {
    type Output = Temperature;

    fn add(self, other: Temperature) -> Temperature {
        other + self
    }
}

impl ::std::ops::Sub<TemperatureDelta> for Temperature {
    type Output = Temperature;

    fn sub(self, other: TemperatureDelta) -> Temperature {
        Temperature::from_kelvin(self.degrees_kelvin - other.kelvin_degrees)
    }
}

impl ::std::ops::Sub<Temperature> for Temperature {
    type Output = TemperatureDelta;

    fn sub(self, other: Temperature) -> TemperatureDelta {
        TemperatureDelta::from_kelvin(self.degrees_kelvin - other.degrees_kelvin)
    }
}

impl ::std::cmp::Eq for Temperature {}
impl ::std::cmp::PartialEq for Temperature {
    fn eq(&self, other: &Self) -> bool {
        self.as_base_units() == other.as_base_units()
    }
}

impl ::std::cmp::PartialOrd for Temperature {
    fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
        self.as_base_units().partial_cmp(&other.as_base_units())
    }
}

implement_display!(Temperature);
implement_measurement!(TemperatureDelta);

#[cfg(test)]
mod test {
    use temperature::*;
    use test_utils::assert_almost_eq;

    // Temperature Units
    #[test]
    fn kelvin() {
        let t = Temperature::from_kelvin(100.0);
        let o = t.as_kelvin();

        assert_almost_eq(o, 100.0);
    }

    #[test]
    fn celsius() {
        let t = Temperature::from_kelvin(100.0);
        let o = t.as_celsius();

        assert_almost_eq(o, -173.15);
    }

    #[test]
    fn fahrenheit() {
        let t = Temperature::from_kelvin(100.0);
        let o = t.as_fahrenheit();

        assert_almost_eq(o, -279.67);
    }

    #[test]
    fn rankine() {
        let t = Temperature::from_kelvin(100.0);
        let o = t.as_rankine();

        assert_almost_eq(o, 180.0);
    }

    // Traits
    #[test]
    fn add() {
        let a = Temperature::from_kelvin(2.0);
        let b = TemperatureDelta::from_kelvin(4.0);
        let c = a + b;
        let d = b + a;
        assert_almost_eq(c.as_kelvin(), 6.0);
        assert_eq!(c, d);
    }

    #[test]
    fn add2() {
        let a = TemperatureDelta::from_kelvin(2.0);
        let b = TemperatureDelta::from_kelvin(4.0);
        let c = a + b;
        let d = b + a;
        assert_almost_eq(c.as_kelvin(), 6.0);
        assert_eq!(c, d);
    }

    #[test]
    fn sub() {
        let a = Temperature::from_kelvin(4.0);
        let b = TemperatureDelta::from_kelvin(2.0);
        let c = a - b;
        assert_almost_eq(c.as_kelvin(), 2.0);
    }

    #[test]
    fn sub2() {
        let a = Temperature::from_fahrenheit(212.0);
        let b = Temperature::from_celsius(75.0);
        let c = a - b;
        assert_almost_eq(c.as_kelvin(), 25.0);
    }

    #[test]
    fn sub3() {
        let a = TemperatureDelta::from_fahrenheit(180.0);
        let b = TemperatureDelta::from_celsius(75.0);
        let c = a - b;
        assert_almost_eq(c.as_kelvin(), 25.0);
    }

    #[test]
    fn mul() {
        let a = TemperatureDelta::from_celsius(5.0);
        let b = a * 2.0;
        let c = 2.0 * a;
        assert_almost_eq(b.as_celsius(), 10.0);
        assert_eq!(b, c);
    }

    #[test]
    fn eq() {
        let a = Temperature::from_kelvin(2.0);
        let b = Temperature::from_kelvin(2.0);
        assert_eq!(a == b, true);
    }

    #[test]
    fn neq() {
        let a = Temperature::from_kelvin(2.0);
        let b = Temperature::from_kelvin(4.0);
        assert_eq!(a == b, false);
    }

    #[test]
    fn cmp() {
        let a = Temperature::from_kelvin(2.0);
        let b = Temperature::from_kelvin(4.0);
        assert_eq!(a < b, true);
        assert_eq!(a <= b, true);
        assert_eq!(a > b, false);
        assert_eq!(a >= b, false);
    }

}