1#![deny(missing_docs)]
36
37mod bill;
38mod contract;
39mod demand;
40mod facilities;
41mod ledgers;
42mod parse;
43mod peak;
44mod settlement;
45mod tou;
46
47pub use bill::{MonthBill, MonthInputs, month_bill};
48pub use contract::{
49 CalendarDate, Charge, ChargeName, DateRange, FixedCharge, Kilowatt, KilowattHour, MeterScope,
50 Minutes, PassThrough, RateUnit, RateWithSource, SiteAlias, SiteTariff, TimeOfUse, Usd,
51 UsdPerMwh, YearMonth,
52};
53pub use demand::{
54 DemandCharge, DemandHistory, DemandResult, Ratchet, demand_bill, demand_charge,
55 marginal_demand_value,
56};
57pub use facilities::{FacilitiesBasis, FacilitiesCharge, facilities_bill};
58pub use ledgers::{LedgerInputs, ledgers};
59pub use peak::{CoincidentPeakRule, PeakExposure, coincident_peak_exposure};
60pub use settlement::{
61 EnergyAdder, EnergyPrices, HedgedRate, Settlement, energy_bill, energy_bill_local,
62};
63pub use tou::{CivilMinute, TouPeriod, TouSchedule};
64
65#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
67pub enum TariffError {
68 #[error("a site alias cannot be empty; it looks like synthetic-4cp")]
70 EmptySiteAlias,
71 #[error("`{alias}` is not a site alias: `{character}` is not one of a-z, 0-9 or -")]
73 SiteAliasCharacter {
74 alias: String,
76 character: char,
78 },
79 #[error("a contract name cannot be empty")]
81 EmptyContractName,
82 #[error("a meter scope cannot be empty")]
84 EmptyMeterScope,
85 #[error("rate is negative or not finite: {value}")]
87 IllegalRate {
88 value: String,
90 },
91 #[error("demand window must be 15 or 30 minutes, got {0}")]
93 InvalidWindow(u16),
94 #[error("ratchet pct must be in (0, 100], got {0}")]
96 InvalidRatchetPct(String),
97 #[error("ratchet months must be at least 1, got {0}")]
99 InvalidRatchetMonths(u32),
100 #[error("intervals_per_year must be 4 or 12, got {0}")]
102 InvalidIntervalsPerYear(u8),
103 #[error("interval minutes must be positive, got {0}")]
105 InvalidIntervalMinutes(u16),
106 #[error("month {0} is not in 1..=12")]
108 InvalidMonth(u8),
109 #[error("date `{0}` is not YYYY-MM-DD")]
111 InvalidDate(String),
112 #[error("date range starts after it ends")]
114 InvertedDateRange,
115 #[error("time-of-use hour {0} is not in 0..=23")]
117 InvalidHour(u8),
118 #[error("minute {0} is outside 0..1440 (end may be 1440)")]
120 InvalidMinute(u16),
121 #[error("ISO weekday {0} is not in 1..=7")]
123 InvalidWeekday(u8),
124 #[error("a time-of-use period name cannot be empty")]
126 EmptyTouName,
127 #[error("a time-of-use period has no months")]
129 EmptyTouMonths,
130 #[error("a time-of-use period has no weekdays")]
132 EmptyTouWeekdays,
133 #[error("duplicate month {0} in a time-of-use period")]
135 DuplicateTouMonth(u8),
136 #[error("duplicate weekday {0} in a time-of-use period")]
138 DuplicateTouWeekday(u8),
139 #[error("time-of-use window is empty")]
141 EmptyTouWindow,
142 #[error("time-of-use schedule has no periods")]
144 EmptyTouSchedule,
145 #[error("time-of-use periods `{first}` and `{second}` overlap")]
147 OverlappingTouPeriods {
148 first: String,
150 second: String,
152 },
153 #[error("civil time matches no time-of-use period")]
155 UnmatchedCivilTime,
156 #[error("time-of-use interval {index} matches no period")]
158 UnmatchedTouInterval {
159 index: usize,
161 },
162 #[error("load length {load} does not match civil-time length {local}")]
164 CivilTimeLengthMismatch {
165 load: usize,
167 local: usize,
169 },
170 #[error("time-of-use energy needs civil local time")]
172 MissingCivilTime,
173 #[error("an energy adder of zero is the unit settlement, not an adder")]
175 ZeroAdder,
176 #[error("facilities basis contract_kw needs contract_kw")]
178 MissingContractKw,
179 #[error("facilities basis ratcheted_peak does not take contract_kw")]
181 UnexpectedContractKw,
182 #[error("facilities basis ratcheted_peak needs a demand charge")]
184 RatchetedPeakWithoutDemand,
185 #[error("coincident-peak intervals: expected {expected}, got {got}")]
187 IntervalCountMismatch {
188 expected: usize,
190 got: usize,
192 },
193 #[error("load length {load} does not match price length {prices}")]
195 SeriesLengthMismatch {
196 load: usize,
198 prices: usize,
200 },
201 #[error("JSON: {0}")]
203 Json(String),
204 #[error("TOML: {0}")]
206 Toml(String),
207}
208
209impl From<serde_json::Error> for TariffError {
210 fn from(error: serde_json::Error) -> Self {
211 Self::Json(error.to_string())
212 }
213}
214
215impl From<toml::de::Error> for TariffError {
216 fn from(error: toml::de::Error) -> Self {
217 Self::Toml(error.to_string())
218 }
219}
220
221impl From<toml::ser::Error> for TariffError {
222 fn from(error: toml::ser::Error) -> Self {
223 Self::Toml(error.to_string())
224 }
225}
226
227pub(crate) fn illegal_rate(value: f64) -> TariffError {
228 TariffError::IllegalRate {
229 value: value.to_string(),
230 }
231}
232
233pub(crate) fn require_non_negative_finite(value: f64) -> Result<f64, TariffError> {
234 if value.is_finite() && value >= 0.0 {
235 Ok(value)
236 } else {
237 Err(illegal_rate(value))
238 }
239}