aboutsummaryrefslogtreecommitdiffstats
path: root/src/volume.rs
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-01-19 15:48:41 -0330
committerSam Anthony <sam@samanthony.xyz>2023-01-19 15:48:41 -0330
commit4d448d3ba4df1b16d2bea9e4fc28095fdcd1c0bd (patch)
treec5786243cb4fcba09a3b3a756da823efcb929a5e /src/volume.rs
parentbcfb575e16db5939f18dc960df0263bf94fdb3a2 (diff)
downloadvolute-4d448d3ba4df1b16d2bea9e4fc28095fdcd1c0bd.zip
add convert() method to Volume trait
Diffstat (limited to 'src/volume.rs')
-rw-r--r--src/volume.rs36
1 files changed, 29 insertions, 7 deletions
diff --git a/src/volume.rs b/src/volume.rs
index feeae14..675dfaa 100644
--- a/src/volume.rs
+++ b/src/volume.rs
@@ -9,15 +9,17 @@ pub trait Volume {
fn set(&mut self, val: f64);
- fn unit(&self) -> VolumeUnit;
+ fn unit(&self) -> Unit;
+
+ fn convert(self, unit: Unit) -> Box<dyn Volume>;
}
-pub enum VolumeUnit {
+pub enum Unit {
CubicMetre,
Litre,
}
-impl Display for VolumeUnit {
+impl Display for Unit {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
@@ -42,8 +44,21 @@ impl Volume for CubicMetre {
self.0 = val;
}
- fn unit(&self) -> VolumeUnit {
- VolumeUnit::CubicMetre
+ fn unit(&self) -> Unit {
+ Unit::CubicMetre
+ }
+
+ fn convert(self, unit: Unit) -> Box<dyn Volume> {
+ match unit {
+ Unit::CubicMetre => Box::new(self),
+ Unit::Litre => Box::new(Litre::from(self)),
+ }
+ }
+}
+
+impl From<Litre> for CubicMetre {
+ fn from(value: Litre) -> Self {
+ value.si()
}
}
@@ -59,8 +74,15 @@ impl Volume for Litre {
self.0 = val
}
- fn unit(&self) -> VolumeUnit {
- VolumeUnit::Litre
+ fn unit(&self) -> Unit {
+ Unit::Litre
+ }
+
+ fn convert(self, unit: Unit) -> Box<dyn Volume> {
+ match unit {
+ Unit::CubicMetre => Box::new(CubicMetre::from(self)),
+ Unit::Litre => Box::new(self),
+ }
}
}