From bcfb575e16db5939f18dc960df0263bf94fdb3a2 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 19 Jan 2023 14:59:14 -0330 Subject: displacement unit --- src/main.rs | 1 + src/volume.rs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'src') 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 for Litre { -- cgit v1.2.3