From 3e4fb93a594f11a80904653396698d6c70e07cb0 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sat, 31 Oct 2020 20:44:44 +0100 Subject: Initial public release --- src/metric/length.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/metric/length.rs (limited to 'src/metric/length.rs') diff --git a/src/metric/length.rs b/src/metric/length.rs new file mode 100644 index 0000000..a605ad2 --- /dev/null +++ b/src/metric/length.rs @@ -0,0 +1,44 @@ +use std::fmt::{Display, Formatter, Result}; +use std::ops::{Add, Sub, AddAssign, SubAssign, Div, Mul, DivAssign, MulAssign}; + +use crate::metric::Unit; +use crate::metric::Primitive; + +use crate::declare_unit_basics; + +#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] +pub enum Length { + Base(f64, fn(f64) -> f64, &'static str), + Canonical(f64), + Meter(f64), + Metre(f64), + Centimeter(f64), + Centimetre(f64), + Kilometer(f64), + Kilometre(f64), + Inch(f64), // Zoll + Foot(f64), // Fuß + Yard(f64), // Schritt + Mile(f64), // Meile +} + +impl Unit for Length { + fn to_si_unit(&self) -> Length { + match self { + Length::Base(_, _, _) => self.clone(), + Length::Canonical(value) => Length::Base(value.clone(), |x| x, "m"), + Length::Meter(value) => Length::Base(value.clone(), |x| x, "m"), + Length::Metre(value) => Length::Base(value.clone(), |x| x, "m"), + Length::Centimeter(value) => Length::Base(value / 100.0, |x| x * 100.0, "cm"), + Length::Centimetre(value) => Length::Base(value / 100.0, |x| x * 100.0, "cm"), + Length::Kilometer(value) => Length::Base(value * 1000.0, |x| x / 1000.0, "km"), + Length::Kilometre(value) => Length::Base(value * 1000.0, |x| x / 1000.0, "km"), + Length::Inch(value) => Length::Base(value * 0.0254, |x| x / 0.0254, "in"), + Length::Foot(value) => Length::Base(value * 0.3048, |x| x / 0.3048, "foot"), + Length::Yard(value) => Length::Base(value * 0.9144, |x| x / 0.9144, "yard"), + Length::Mile(value) => Length::Base(value * 1609.344, |x| x / 1609.344, "mile"), + } + } +} + +declare_unit_basics!(Length); \ No newline at end of file -- cgit v1.2.3-54-g00ecf