summaryrefslogtreecommitdiffstats
path: root/src/metric/energy.rs
blob: 5884a2f3edcd977f747bd7bc0f41f0b6ea324973 (plain) (blame)
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
use std::fmt::{Display, Formatter, Result};
use std::ops::{Add, Sub, AddAssign, SubAssign, Div, Mul, DivAssign, MulAssign};

use crate::metric::Unit;
use crate::metric::Primitive;

use crate::declare_unit_basics;

#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub enum Energy {
    Base(f64, fn(f64) -> f64, &'static str),
    Canonical(f64),
    Joule(f64),
    KiloJoule(f64),
    Calorie(f64),
    Kilocalorie(f64),
}

impl Unit<Energy> for Energy {
    fn to_si_unit(&self) -> Energy {
        match self {
            Energy::Base(_, _, _) => self.clone(),
            Energy::Canonical(value) => Energy::Base(value.clone(), |x| x, "J"),
            Energy::Joule(value) => Energy::Base(value.clone(), |x| x, "J"),
            Energy::KiloJoule(value) => Energy::Base(value * 1000.0, |x| x / 1000.0, "kJ"),
            Energy::Calorie(value) => Energy::Base(value * 4.184, |x| x / 4.184, "cal"),
            Energy::Kilocalorie(value) => Energy::Base(value * 4184.0, |x| x / 4184.0, "kcal"),
        }
    }
}

declare_unit_basics!(Energy);