use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use crate::{
impl_dec_newtype, into_caveat_all, json,
money::Cost,
number::{self, FromDecimal as _},
Money,
};
impl_dec_newtype!(Ampere, "A");
impl_dec_newtype!(Kw, "kW");
impl_dec_newtype!(Kwh, "kWh");
into_caveat_all!(Ampere, Kw, Kwh);
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Default)]
#[cfg_attr(test, derive(serde::Deserialize))]
pub struct Kwh(Decimal);
impl Cost for Kwh {
fn cost(&self, money: Money) -> Money {
let cost = self.0.saturating_mul(money.into());
Money::from_decimal(cost)
}
}
const KILO: Decimal = dec!(1000);
impl Kwh {
pub fn watt_hours(self) -> Decimal {
self.0.saturating_mul(KILO)
}
#[expect(clippy::missing_panics_doc, reason = "divisor is non-zero")]
#[expect(clippy::unwrap_used, reason = "divisor is non-zero")]
pub fn from_watt_hours(num: Decimal) -> Self {
Self(num.checked_div(KILO).unwrap())
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub struct Kw(Decimal);
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub struct Ampere(Decimal);