summaryrefslogtreecommitdiffstats
path: root/src/metric/length.rs
blob: a605ad22d579564ccf0d7400f261b72ad015b2a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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<Length> 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);