aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-01-19 14:59:14 -0330
committerSam Anthony <sam@samanthony.xyz>2023-01-19 14:59:14 -0330
commitbcfb575e16db5939f18dc960df0263bf94fdb3a2 (patch)
tree501e1dd54ec62090fd918f722eeb730e51b6606b
parentf6e96daa12f70f40601eb4d04dc8c8a492008af8 (diff)
downloadvolute-bcfb575e16db5939f18dc960df0263bf94fdb3a2.zip
displacement unit
-rw-r--r--src/main.rs1
-rw-r--r--src/volume.rs33
2 files changed, 33 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index 7af7bac..2c1fa85 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -63,6 +63,7 @@ impl Sandbox for App {
column![row![
text("Displacement:"),
text_input("2.0", &self.ui.displacement, Message::DisplacementChanged),
+ text(self.displacement.unit()),
]]
.into()
}
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 {