unit_converter_lib/units/
volume.rs

1use std::slice::Iter;
2
3use super::Unit;
4
5#[derive(Clone, Copy)]
6pub enum UNITS {
7    CmCube,
8    MCube,
9    Liter,
10    InchCube,
11    FootCube,
12    UsGallon,
13    ImperialGallon,
14}
15pub enum UnitContainer {
16    CmCube(CmCube),
17    MCube(MCube),
18    Liter(Liter),
19    InchCube(InchCube),
20    FootCube(FootCube),
21    UsGallon(UsGallon),
22    ImperialGallon(ImperialGallon),
23}
24impl UnitContainer {
25    pub fn new(unit: UNITS, value: f64) -> Self {
26        match unit {
27            UNITS::CmCube => UnitContainer::CmCube(CmCube(value)),
28            UNITS::MCube => UnitContainer::MCube(MCube(value)),
29            UNITS::Liter => UnitContainer::Liter(Liter(value)),
30            UNITS::InchCube => UnitContainer::InchCube(InchCube(value)),
31            UNITS::FootCube => UnitContainer::FootCube(FootCube(value)),
32            UNITS::UsGallon => UnitContainer::UsGallon(UsGallon(value)),
33            UNITS::ImperialGallon => UnitContainer::ImperialGallon(ImperialGallon(value)),
34        }
35    }
36}
37
38pub struct CmCube(pub f64);
39pub struct MCube(pub f64);
40pub struct Liter(pub f64);
41pub struct InchCube(pub f64);
42pub struct FootCube(pub f64);
43pub struct UsGallon(pub f64);
44pub struct ImperialGallon(pub f64);
45
46mod conversions;
47
48impl UNITS {
49    pub fn iterator() -> Iter<'static, UNITS> {
50        use UNITS::*;
51        static DIRECTIONS: [UNITS;7] = [CmCube,MCube,Liter,InchCube,FootCube,UsGallon,ImperialGallon];
52        DIRECTIONS.iter()
53    }
54}
55impl Unit for UNITS {
56    fn get_type(n: u32) -> Self {
57        use UNITS::*;
58        static DIRECTIONS: [UNITS;7] = [CmCube,MCube,Liter,InchCube,FootCube,UsGallon,ImperialGallon];
59        DIRECTIONS[n as usize]
60    }
61    fn to_string(&self) -> String {
62        match self {
63            Self::CmCube => String::from("cmcube"),
64            Self::MCube => String::from("mcube"),
65            Self::Liter => String::from("liter"),
66            Self::InchCube => String::from("inchcube"),
67            Self::FootCube => String::from("footcube"),
68            Self::UsGallon => String::from("us_gallon"),
69            Self::ImperialGallon => String::from("imperial_gallon"),
70        }
71    }
72    fn by_name(name: &str) -> Self {
73        match name {
74            "cmcube" => Self::CmCube,
75            "mcube" => Self::MCube,
76            "liter" => Self::Liter,
77            "inchcube" => Self::InchCube,
78            "footcube" => Self::FootCube,
79            "us_gallon" => Self::UsGallon,
80            "imperial_gallon" => Self::ImperialGallon,
81            _ => panic!("unit with name '{}' does not exist", name)
82        }
83    }
84}