From 413608ea229f0adec21feb27501178ccadd47844 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 20 Jan 2023 19:55:20 -0330 Subject: add cc unit --- src/main.rs | 2 +- src/volume.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index cc06d95..8a0e170 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,7 +77,7 @@ impl Sandbox for App { fn view(&self) -> Element { column![row![ text("Displacement:"), - text_input("2.0", &self.ui.displacement, Message::Displacement), + text_input("0.0", &self.ui.displacement, Message::Displacement), pick_list( &volume::Unit::ALL[..], Some(self.ui.displacement_unit), diff --git a/src/volume.rs b/src/volume.rs index 5818ea4..a288f7e 100644 --- a/src/volume.rs +++ b/src/volume.rs @@ -1,17 +1,15 @@ -use std::{ - fmt::{self, Display, Formatter}, - ops::Mul, -}; +use std::fmt::{self, Display, Formatter}; #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)] pub enum Unit { #[default] CubicMetre, Litre, + CubicCentimetre, } impl Unit { - pub const ALL: [Self; 2] = [Self::CubicMetre, Self::Litre]; + pub const ALL: [Self; 3] = [Self::CubicMetre, Self::Litre, Self::CubicCentimetre]; } impl Display for Unit { @@ -22,6 +20,7 @@ impl Display for Unit { match self { Self::CubicMetre => "m³", Self::Litre => "L", + Self::CubicCentimetre => "cc", } ) } @@ -35,6 +34,7 @@ impl CubicMetre { match unit { Unit::CubicMetre => Self(value.into()), Unit::Litre => Self::from(Litre(value.into())), + Unit::CubicCentimetre => Self::from(CubicCentimetre(value.into())), } } } @@ -45,26 +45,39 @@ impl From for CubicMetre { } } -#[derive(Debug, PartialEq)] -struct Litre(f64); - -impl> From for Litre { - fn from(value: F) -> Self { - Self(value.into()) +impl From for CubicMetre { + fn from(value: CubicCentimetre) -> Self { + Self(value.0 * 10_f64.powi(-6)) } } +#[derive(Debug, PartialEq)] +struct Litre(f64); + impl From for Litre { fn from(value: CubicMetre) -> Self { Self(value.0 * 10_f64.powi(3)) } } -impl> Mul for Litre { - type Output = Self; +impl From for Litre { + fn from(value: CubicCentimetre) -> Self { + Self(value.0 * 10_f64.powi(-3)) + } +} + +#[derive(Debug, PartialEq)] +pub struct CubicCentimetre(f64); + +impl From for CubicCentimetre { + fn from(value: CubicMetre) -> Self { + Self(value.0 * 10_f64.powi(6)) + } +} - fn mul(self, rhs: F) -> Self::Output { - Self(self.0 * rhs.into()) +impl From for CubicCentimetre { + fn from(value: Litre) -> Self { + Self(value.0 * 10_f64.powi(3)) } } @@ -73,25 +86,58 @@ pub fn convert>(val: F, from: Unit, to: Unit) -> f64 { Unit::CubicMetre => match to { Unit::CubicMetre => val.into(), Unit::Litre => Litre::from(CubicMetre(val.into())).0, + Unit::CubicCentimetre => CubicCentimetre::from(CubicMetre(val.into())).0, }, Unit::Litre => match to { Unit::Litre => val.into(), Unit::CubicMetre => CubicMetre::from(Litre(val.into())).0, + Unit::CubicCentimetre => CubicCentimetre::from(Litre(val.into())).0, + }, + Unit::CubicCentimetre => match to { + Unit::CubicCentimetre => val.into(), + Unit::CubicMetre => CubicMetre::from(CubicCentimetre(val.into())).0, + Unit::Litre => Litre::from(CubicCentimetre(val.into())).0, }, } } #[cfg(test)] mod tests { - use super::{CubicMetre, Litre}; + use super::{CubicCentimetre, CubicMetre, Litre}; + + #[test] + fn cubic_metre_to_litre() { + assert_eq!(Litre::from(CubicMetre(1.)), Litre(1000.)); + } + + #[test] + fn cubic_metre_to_cubic_centimetre() { + assert_eq!( + CubicCentimetre::from(CubicMetre(1.)), + CubicCentimetre(1_000_000.) + ); + } #[test] fn litre_to_cubic_metre() { - assert_eq!(CubicMetre::from(Litre(1000.)), CubicMetre(1.)) + assert_eq!(CubicMetre::from(Litre(1000.)), CubicMetre(1.)); } #[test] - fn cubic_metre_to_litre() { - assert_eq!(Litre::from(CubicMetre(1.)), Litre(1000.)) + fn litre_to_cubic_centimetre() { + assert_eq!(CubicCentimetre::from(Litre(1.)), CubicCentimetre(1000.)); + } + + #[test] + fn cubic_centimetre_to_cubic_metre() { + assert_eq!( + CubicMetre::from(CubicCentimetre(1_000_000.)), + CubicMetre(1.) + ); + } + + #[test] + fn cubic_centimetre_to_litre() { + assert_eq!(Litre::from(CubicCentimetre(1000.)), Litre(1.)); } } -- cgit v1.2.3