aboutsummaryrefslogtreecommitdiffstats
path: root/src/unit_of_measurement.rs
blob: a3b4b7788a9d8fbab06c39766f1b3e49768eab6b (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
pub trait UnitOfMeasurement {
    type Unit;

    fn from_unit(unit: Self::Unit, n: i32) -> Self;
    fn as_unit(&self, unit: Self::Unit) -> i32;
}

pub mod pressure {
    use super::UnitOfMeasurement;

    #[derive(Default)]
    pub struct Pressure {
        val: i32,
    }

    pub enum Unit {
        Pascal = 1,
        KiloPascal = 1000,
    }

    impl UnitOfMeasurement for Pressure {
        type Unit = Unit;

        fn from_unit(unit: Self::Unit, n: i32) -> Self {
            Self {
                val: n * unit as i32,
            }
        }

        fn as_unit(&self, unit: Self::Unit) -> i32 {
            self.val / unit as i32
        }
    }
}