aboutsummaryrefslogtreecommitdiffstats
path: root/src/volume.rs
blob: 675dfaa7ec578b43b779197fcf44d56afffaf96a (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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) -> Unit;

    fn convert(self, unit: Unit) -> Box<dyn Volume>;
}

pub enum Unit {
    CubicMetre,
    Litre,
}

impl Display for Unit {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::CubicMetre => "m³",
                Self::Litre => "L",
            }
        )
    }
}

#[derive(Debug, Default, PartialEq)]
pub struct CubicMetre(f64);

impl Volume for CubicMetre {
    fn si(self) -> CubicMetre {
        self
    }

    fn set(&mut self, val: f64) {
        self.0 = val;
    }

    fn unit(&self) -> Unit {
        Unit::CubicMetre
    }

    fn convert(self, unit: Unit) -> Box<dyn Volume> {
        match unit {
            Unit::CubicMetre => Box::new(self),
            Unit::Litre => Box::new(Litre::from(self)),
        }
    }
}

impl From<Litre> for CubicMetre {
    fn from(value: Litre) -> Self {
        value.si()
    }
}

#[derive(Debug, PartialEq)]
struct Litre(f64);

impl Volume for Litre {
    fn si(self) -> CubicMetre {
        CubicMetre(self.0 * 10_f64.powf(-3.))
    }

    fn set(&mut self, val: f64) {
        self.0 = val
    }

    fn unit(&self) -> Unit {
        Unit::Litre
    }

    fn convert(self, unit: Unit) -> Box<dyn Volume> {
        match unit {
            Unit::CubicMetre => Box::new(CubicMetre::from(self)),
            Unit::Litre => Box::new(self),
        }
    }
}

impl From<i32> for Litre {
    fn from(value: i32) -> Self {
        Self(value as f64)
    }
}

impl From<CubicMetre> for Litre {
    fn from(value: CubicMetre) -> Self {
        Self(value.0 * 10_f64.powf(3.))
    }
}

impl Mul<f64> for Litre {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        Self(self.0 * rhs)
    }
}

#[cfg(test)]
mod tests {
    use crate::volume::{CubicMetre, Litre, Volume};

    #[test]
    fn litre_to_cubic_metre() {
        assert_eq!(Litre(1000.).si(), CubicMetre(1.))
    }

    #[test]
    fn cubic_metre_to_litre() {
        assert_eq!(Litre::from(CubicMetre(1.)), Litre(1000.))
    }
}