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