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
//! Types and constants for handling energy.

use super::measurement::*;

/// The `Energy` struct can be used to deal with energies in a common way.
/// Common metric and imperial units are supported.
///
/// # Example
///
/// ```
/// use measurements::Energy;
///
/// let energy = Energy::from_kcalories(2500.0);
/// println!("Some say a health adult male should consume {} per day", energy);
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug)]
pub struct Energy {
    joules: f64,
}

impl Energy {
    /// Create a new Energy from a floating point value in Joules (or watt-seconds)
    pub fn from_joules(joules: f64) -> Energy {
        Energy { joules: joules }
    }

    /// Create a new Energy from a floating point value in Kilocalories (often just called calories)
    pub fn from_kcalories(kcalories: f64) -> Energy {
        Self::from_joules(kcalories * 4186.8)
    }

    /// Create a new Energy from a floating point value in British Thermal Units
    pub fn from_btu(btu: f64) -> Energy {
        Self::from_joules(btu * 1055.056)
    }

    /// Create a new Energy from a floating point value in electron Volts (eV).
    pub fn from_e_v(e_v: f64) -> Energy {
        Self::from_joules(e_v / 6.241509479607718e+18)
    }

    /// Create a new Energy from a floating point value in Watt-hours (Wh)
    pub fn from_watt_hours(wh: f64) -> Energy {
        Self::from_joules(wh * 3600.0)
    }

    /// Create a new Energy from a floating point value in Kilowatt-Hours (kWh)
    pub fn from_kilowatt_hours(kwh: f64) -> Energy {
        Self::from_joules(kwh * 3600.0 * 1000.0)
    }

    /// Convert this Energy into a floating point value in Joules (or watt-seconds)
    pub fn as_joules(&self) -> f64 {
        self.joules
    }

    /// Convert this Energy into a floating point value in Kilocalories (often just called calories)
    pub fn as_kcalories(&self) -> f64 {
        self.joules / 4186.8
    }

    /// Convert this Energy into a floating point value in British Thermal Units
    pub fn as_btu(&self) -> f64 {
        self.joules / 1055.056
    }

    /// Convert this Energy into a floating point value in electron volts (eV)
    pub fn as_e_v(&self) -> f64 {
        self.joules * 6.241509479607718e+18
    }

    /// Convert this Energy into a floating point value in Watt-hours (Wh)
    pub fn as_watt_hours(&self) -> f64 {
        self.joules / 3600.0
    }

    /// Convert this Energy into a floating point value in kilowatt-hours (kWh)
    pub fn as_kilowatt_hours(&self) -> f64 {
        self.joules / (3600.0 * 1000.0)
    }
}

impl Measurement for Energy {
    fn as_base_units(&self) -> f64 {
        self.joules
    }

    fn from_base_units(units: f64) -> Self {
        Self::from_joules(units)
    }

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

    fn get_appropriate_units(&self) -> (&'static str, f64) {
        // Smallest to Largest
        let list = [
            ("fJ", 1e-15),
            ("pJ", 1e-12),
            ("nJ", 1e-9),
            ("\u{00B5}J", 1e-6),
            ("mJ", 1e-3),
            ("J", 1e0),
            ("kJ", 1e3),
            ("MJ", 1e6),
            ("GJ", 1e9),
            ("TJ", 1e12),
            ("PJ", 1e15),
            ("EJ", 1e18),
        ];
        self.pick_appropriate_units(&list)
    }
}

implement_measurement! { Energy }

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

    #[test]
    pub fn as_kcalories() {
        let i1 = Energy::from_kcalories(100.0);
        let r1 = i1.as_joules();

        let i2 = Energy::from_joules(100.0);
        let r2 = i2.as_kcalories();

        assert_almost_eq(r1, 418680.0);
        assert_almost_eq(r2, 0.0238845896627496);
    }

    #[test]
    pub fn as_btu() {
        let i1 = Energy::from_btu(100.0);
        let r1 = i1.as_joules();

        let i2 = Energy::from_joules(100.0);
        let r2 = i2.as_btu();

        assert_almost_eq(r1, 105505.6);
        assert_almost_eq(r2, 0.0947816987913438);
    }

    #[test]
    pub fn as_e_v() {
        let i1 = Energy::from_e_v(100.0);
        let r1 = i1.as_joules();

        let i2 = Energy::from_joules(100.0);
        let r2 = i2.as_e_v();

        assert_almost_eq(r1, 1.60217653e-17);
        assert_almost_eq(r2, 6.241509479607718e+20);
    }

    #[test]
    pub fn as_watt_hours() {
        let i1 = Energy::from_watt_hours(100.0);
        let r1 = i1.as_joules();

        let i2 = Energy::from_joules(100.0);
        let r2 = i2.as_watt_hours();

        assert_almost_eq(r1, 360000.0);
        assert_almost_eq(r2, 0.02777777777777777777777777777778);
    }

    #[test]
    pub fn as_kilowatt_hours() {
        let i1 = Energy::from_kilowatt_hours(100.0);
        let r1 = i1.as_joules();

        let i2 = Energy::from_joules(100.0);
        let r2 = i2.as_kilowatt_hours();

        assert_almost_eq(r1, 360000000.0);
        assert_almost_eq(r2, 2.777777777777777777777777777778e-5);
    }

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

    #[test]
    fn sub() {
        let a = Energy::from_joules(2.0);
        let b = Energy::from_joules(4.0);
        let c = a - b;
        assert_almost_eq(c.as_joules(), -2.0);
    }

    #[test]
    fn mul() {
        let a = Energy::from_joules(3.0);
        let b = a * 2.0;
        let c = 2.0 * a;
        assert_almost_eq(b.as_joules(), 6.0);
        assert_eq!(b, c);
    }

    #[test]
    fn div() {
        let a = Energy::from_joules(2.0);
        let b = Energy::from_joules(4.0);
        let c = a / b;
        let d = a / 2.0;
        assert_almost_eq(c, 0.5);
        assert_almost_eq(d.as_joules(), 1.0);
    }

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

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

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

}