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
use super::measurement::*;

/// The `Temperature` struct can be used to deal with 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 {
    kelvin: f64
}

impl Temperature {
    pub fn from_kelvin(kelvin: f64) -> Self {
        Temperature { kelvin: kelvin }
    }
    
    pub fn from_celsius(celsius: f64) -> Self {
        Self::from_kelvin(celsius + 273.15)
    }
    
    pub fn from_fahrenheit(fahrenheit: f64) -> Self {
        Self::from_kelvin((fahrenheit - 32.0) / 1.8 + 273.15)
    }
    
    pub fn from_rankine(rankine: f64) -> Self {
        Self::from_kelvin((rankine - 491.67) / 1.8 + 273.15)
    }
    
    pub fn as_kelvin(&self) -> f64 {
        self.kelvin
    }
    
    pub fn as_celsius(&self) -> f64 {
        self.kelvin - 273.15
    }
    
    pub fn as_fahrenheit(&self) -> f64 {
        (self.kelvin - 273.15) * 1.8 + 32.0
    }
    
    pub fn as_rankine(&self) -> f64 {
        (self.kelvin - 273.15) * 1.8 + 491.67
    }
}

impl Measurement for Temperature {
    fn get_base_units(&self) -> f64 {
        self.kelvin
    }
    
    fn from_base_units(units: f64) -> Self {
        Self::from_kelvin(units)
    }
}

implement_measurement! { Temperature }