grid_tariffs/
power_tariffs.rs

1use serde::Serialize;
2
3use crate::{
4    Language, PartialPowerAverage, PowerAverage, PowerTariffMatches,
5    costs::{CostPeriods, CostPeriodsSimple},
6};
7
8#[derive(Debug, Clone, Serialize)]
9#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
10pub enum PowerTariff {
11    Unverified,
12    NotImplemented,
13    Periods {
14        method: TariffCalculationMethod,
15        periods: CostPeriods,
16    },
17}
18
19impl PowerTariff {
20    pub const fn new(method: TariffCalculationMethod, periods: CostPeriods) -> Self {
21        Self::Periods { method, periods }
22    }
23
24    pub const fn is_unverified(&self) -> bool {
25        matches!(self, Self::Unverified)
26    }
27
28    pub fn simplified(
29        &self,
30        fuse_size: u16,
31        yearly_consumption: u32,
32        language: Language,
33    ) -> PowerTariffSimplified {
34        PowerTariffSimplified::new(self, fuse_size, yearly_consumption, language)
35    }
36
37    pub fn periods(
38        &self,
39        averages: Vec<PowerAverage>,
40        current_power_average: Option<PartialPowerAverage>,
41    ) -> PowerTariffMatches {
42        match self {
43            PowerTariff::Unverified => PowerTariffMatches::new_dummy(),
44            PowerTariff::NotImplemented => PowerTariffMatches::new_dummy(),
45            PowerTariff::Periods { method, periods } => {
46                PowerTariffMatches::new(*method, *periods, &averages, current_power_average)
47            }
48        }
49    }
50}
51
52/// The method used to calculate power tariffs
53#[derive(Debug, Clone, Copy, Serialize)]
54#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
55pub enum TariffCalculationMethod {
56    /// Power peak for top hour of the top x days of the month
57    AverageDays(u8),
58    /// Average of top x hours of the month
59    AverageHours(u8),
60}
61
62/// Like PowerTariff, but with costs being simple Money objects
63#[derive(Debug, Clone, Serialize)]
64#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
65pub enum PowerTariffSimplified {
66    Unverified,
67    NotImplemented,
68    Periods {
69        method: TariffCalculationMethod,
70        periods: CostPeriodsSimple,
71    },
72}
73
74impl PowerTariffSimplified {
75    fn new(fee: &PowerTariff, fuse_size: u16, yearly_consumption: u32, language: Language) -> Self {
76        match *fee {
77            PowerTariff::Unverified => PowerTariffSimplified::Unverified,
78            PowerTariff::NotImplemented => PowerTariffSimplified::NotImplemented,
79            PowerTariff::Periods { method, periods } => PowerTariffSimplified::Periods {
80                method,
81                periods: CostPeriodsSimple::new(periods, fuse_size, yearly_consumption, language),
82            },
83        }
84    }
85}