ocpi_tariffs/
energy.rs

1use rust_decimal::Decimal;
2
3use crate::{
4    impl_dec_newtype, json,
5    money::Cost,
6    number::{self, FromDecimal as _},
7    Money,
8};
9
10impl_dec_newtype!(Ampere, "A");
11impl_dec_newtype!(Kwh, "Kwh");
12impl_dec_newtype!(Kw, "Kw");
13
14/// A value of kilo watt hours.
15#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Default)]
16#[cfg_attr(test, derive(serde::Deserialize))]
17pub struct Kwh(Decimal);
18
19impl Cost for Kwh {
20    fn cost(&self, money: Money) -> Money {
21        let cost = self.0.saturating_mul(money.into());
22        Money::from_decimal(cost)
23    }
24}
25
26impl Kwh {
27    pub fn watt_hours(self) -> Decimal {
28        self.0.saturating_mul(Decimal::from(1000))
29    }
30
31    #[expect(clippy::missing_panics_doc, reason = "divisor is non-zero")]
32    #[expect(clippy::unwrap_used, reason = "divisor is non-zero")]
33    pub fn from_watt_hours(num: Decimal) -> Self {
34        Self(num.checked_div(Decimal::from(1000)).unwrap())
35    }
36}
37
38/// A value of kilo watts.
39#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
40pub struct Kw(Decimal);
41
42/// A value of amperes.
43#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
44pub struct Ampere(Decimal);