diff options
Diffstat (limited to 'src/volume/mod.rs')
| -rw-r--r-- | src/volume/mod.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/volume/mod.rs b/src/volume/mod.rs new file mode 100644 index 0000000..0f36454 --- /dev/null +++ b/src/volume/mod.rs @@ -0,0 +1,70 @@ +mod cubic_centimetre; +mod cubic_metre; +mod litre; +mod unit; + +pub use cubic_centimetre::CubicCentimetre; +pub use cubic_metre::CubicMetre; +pub use litre::Litre; +pub use unit::Unit; + +pub fn convert<F: Into<f64>>(val: F, from: Unit, to: Unit) -> f64 { + match from { + 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::{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.)); + } + + #[test] + 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.)); + } +} |