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 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);