use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use time::Duration;
const THOUSAND: Decimal = Decimal::from_parts(1000, 0, 0, false, 0);
const SECONDS_PER_HOUR: Decimal = Decimal::from_parts(3600, 0, 0, false, 0);
fn hours(duration: Duration) -> Option<Decimal> {
let nanos = Decimal::from_i128_with_scale(duration.whole_nanoseconds(), 9);
nanos.checked_div(SECONDS_PER_HOUR)
}
macro_rules! quantity {
($(#[$meta:meta])* $name:ident, $unit:literal) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct $name(#[serde(with = "rust_decimal::serde::str")] Decimal);
impl $name {
pub const ZERO: Self = Self(Decimal::ZERO);
#[must_use]
pub const fn new(value: Decimal) -> Self {
Self(value)
}
#[must_use]
pub const fn value(self) -> Decimal {
self.0
}
#[must_use]
pub fn checked_add(self, other: Self) -> Option<Self> {
self.0.checked_add(other.0).map(Self)
}
#[must_use]
pub fn checked_sub(self, other: Self) -> Option<Self> {
self.0.checked_sub(other.0).map(Self)
}
}
impl std::ops::Neg for $name {
type Output = Self;
fn neg(self) -> Self {
Self(-self.0)
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.0.normalize(), $unit)
}
}
};
}
quantity!(
Kw,
"kW"
);
quantity!(
Kwh,
"kWh"
);
quantity!(
Eur,
"EUR"
);
quantity!(
EurPerMwh,
"EUR/MWh"
);
quantity!(
EurPerMwPerH,
"EUR/MW/h"
);
impl Kw {
#[must_use]
pub fn from_mw(mw: Decimal) -> Option<Self> {
mw.checked_mul(THOUSAND).map(Self)
}
#[must_use]
pub fn to_mw(self) -> Decimal {
self.0 / THOUSAND
}
#[must_use]
pub fn energy_over(self, duration: Duration) -> Option<Kwh> {
hours(duration).and_then(|h| self.0.checked_mul(h)).map(Kwh)
}
}
impl Kwh {
#[must_use]
pub fn from_mwh(mwh: Decimal) -> Option<Self> {
mwh.checked_mul(THOUSAND).map(Self)
}
#[must_use]
pub fn to_mwh(self) -> Decimal {
self.0 / THOUSAND
}
#[must_use]
pub fn mean_power_over(self, duration: Duration) -> Option<Kw> {
hours(duration).and_then(|h| self.0.checked_div(h)).map(Kw)
}
#[must_use]
pub fn priced_at(self, price: EurPerMwh) -> Option<Eur> {
self.to_mwh().checked_mul(price.0).map(Eur)
}
}
impl EurPerMwPerH {
#[must_use]
pub fn for_capacity(self, power: Kw, duration: Duration) -> Option<Eur> {
let mw_h = hours(duration).and_then(|h| power.to_mw().checked_mul(h))?;
self.0.checked_mul(mw_h).map(Eur)
}
}
impl Eur {
#[must_use]
pub fn round_cents(self) -> Self {
Self(
self.0
.round_dp_with_strategy(2, rust_decimal::RoundingStrategy::MidpointNearestEven),
)
}
}
#[must_use]
pub fn parse_plain(s: &str) -> Option<Decimal> {
let body = s.strip_prefix('-').unwrap_or(s);
let (int, frac) = body.split_once('.').unwrap_or((body, ""));
let digits = |p: &str| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit());
let plain = digits(int) && (frac.is_empty() && !body.ends_with('.') || digits(frac));
if plain {
<Decimal as std::str::FromStr>::from_str(s).ok()
} else {
None
}
}