klick_domain/units/
conversion.rs

1use std::ops::Mul;
2
3#[allow(clippy::wildcard_imports)]
4use super::*;
5
6macro_rules! direct_multiply {
7    (
8      $unit_a:ident, $unit_b:ident, $output:ident
9    ) => {
10        impl Mul<$unit_a> for $unit_b {
11            type Output = $output;
12            fn mul(self, rhs: $unit_a) -> Self::Output {
13                Self::Output::new(self.0 * rhs.0)
14            }
15        }
16
17        impl Mul<$unit_a> for &$unit_b {
18            type Output = $output;
19            fn mul(self, rhs: $unit_a) -> Self::Output {
20                Self::Output::new(self.0 * rhs.0)
21            }
22        }
23
24        impl Mul<&$unit_a> for $unit_b {
25            type Output = $output;
26            fn mul(self, rhs: &$unit_a) -> Self::Output {
27                Self::Output::new(self.0 * rhs.0)
28            }
29        }
30
31        impl Mul<$unit_b> for $unit_a {
32            type Output = $output;
33            fn mul(self, rhs: $unit_b) -> Self::Output {
34                Self::Output::new(self.0 * rhs.0)
35            }
36        }
37
38        impl Mul<&$unit_b> for $unit_a {
39            type Output = $output;
40            fn mul(self, rhs: &$unit_b) -> Self::Output {
41                Self::Output::new(self.0 * rhs.0)
42            }
43        }
44
45        impl Mul<$unit_b> for &$unit_a {
46            type Output = $output;
47            fn mul(self, rhs: $unit_b) -> Self::Output {
48                Self::Output::new(self.0 * rhs.0)
49            }
50        }
51    };
52}
53
54macro_rules! multiply {
55    ($Output:ty: |$lhs:ident: $Lhs:ty, $rhs:ident: $Rhs:ty| $body:block) => {
56        impl Mul<$Rhs> for $Lhs {
57            type Output = $Output;
58            fn mul(self, $rhs: $Rhs) -> Self::Output {
59                let $lhs = self;
60                $body
61            }
62        }
63
64        impl Mul<$Rhs> for &$Lhs {
65            type Output = $Output;
66            fn mul(self, $rhs: $Rhs) -> Self::Output {
67                let $lhs = self;
68                $body
69            }
70        }
71
72        impl Mul<&$Rhs> for $Lhs {
73            type Output = $Output;
74            fn mul(self, $rhs: &$Rhs) -> Self::Output {
75                let $lhs = self;
76                $body
77            }
78        }
79
80        impl Mul<&$Rhs> for &$Lhs {
81            type Output = $Output;
82            fn mul(self, $rhs: &$Rhs) -> Self::Output {
83                let $lhs = self;
84                $body
85            }
86        }
87    };
88}
89
90macro_rules! direct_divide {
91    (
92      $unit_a:ident, $unit_b:ident, $output:ident
93    ) => {
94        impl Div<$unit_a> for $unit_b {
95            type Output = $output;
96            fn div(self, rhs: $unit_a) -> Self::Output {
97                Self::Output::new(self.0 / rhs.0)
98            }
99        }
100    };
101}
102
103direct_multiply!(Qubicmeters, KilogramsPerQubicmeter, Kilograms);
104direct_multiply!(Kilowatthours, GramsPerKilowatthour, Grams);
105direct_multiply!(LitersPerTonKilometer, Tons, LitersPerKilometer);
106direct_multiply!(Kilometers, LitersPerKilometer, Liters);
107direct_multiply!(KilogramsPerLiter, Liters, Kilograms);
108direct_multiply!(QubicmetersPerHour, Hours, Qubicmeters);
109
110direct_divide!(Qubicmeters, Hours, QubicmetersPerHour);
111direct_divide!(Liters, Kilometers, LitersPerKilometer);
112
113multiply!(
114    Kilograms: |lhs: MilligramsPerLiter, rhs: Qubicmeters|{
115        let kg_p_m3 = lhs.convert_to::<KilogramsPerQubicmeter>();
116        Kilograms::new(kg_p_m3.0 * rhs.0)
117    }
118);
119
120multiply!(
121    Kilograms: |lhs: Qubicmeters, rhs: MilligramsPerLiter|{
122        let kg_p_m3 = rhs.convert_to::<KilogramsPerQubicmeter>();
123        Kilograms::new(kg_p_m3.0 * lhs.0)
124    }
125);
126
127impl From<Percent> for Factor {
128    fn from(from: Percent) -> Factor {
129        Factor::new(from.0 / 100.0)
130    }
131}
132
133impl Mul<Factor> for f64 {
134    type Output = f64;
135
136    fn mul(self, rhs: Factor) -> f64 {
137        self * rhs.0
138    }
139}
140
141impl Mul<Percent> for f64 {
142    type Output = f64;
143    fn mul(self, value: Percent) -> Self::Output {
144        self * value.0 / 100.0
145    }
146}