aboutsummaryrefslogtreecommitdiffstats
path: root/src/volume.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/volume.rs')
-rw-r--r--src/volume.rs33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/volume.rs b/src/volume.rs
index 087676c..feeae14 100644
--- a/src/volume.rs
+++ b/src/volume.rs
@@ -1,10 +1,33 @@
-use std::ops::Mul;
+use std::{
+ fmt::{self, Display, Formatter},
+ ops::Mul,
+};
pub trait Volume {
/// Returns the volume in SI units (cubic metres).
fn si(self) -> CubicMetre;
fn set(&mut self, val: f64);
+
+ fn unit(&self) -> VolumeUnit;
+}
+
+pub enum VolumeUnit {
+ CubicMetre,
+ Litre,
+}
+
+impl Display for VolumeUnit {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "{}",
+ match self {
+ Self::CubicMetre => "m³",
+ Self::Litre => "L",
+ }
+ )
+ }
}
#[derive(Debug, Default, PartialEq)]
@@ -18,6 +41,10 @@ impl Volume for CubicMetre {
fn set(&mut self, val: f64) {
self.0 = val;
}
+
+ fn unit(&self) -> VolumeUnit {
+ VolumeUnit::CubicMetre
+ }
}
#[derive(Debug, PartialEq)]
@@ -31,6 +58,10 @@ impl Volume for Litre {
fn set(&mut self, val: f64) {
self.0 = val
}
+
+ fn unit(&self) -> VolumeUnit {
+ VolumeUnit::Litre
+ }
}
impl From<i32> for Litre {