use time::OffsetDateTime;
use crate::domain::{CarbonIntensity, Methodology, Region};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ModelVersion {
pub id: String,
pub version: u32,
}
impl ModelVersion {
pub fn new(id: impl Into<String>, version: u32) -> Self {
Self {
id: id.into(),
version,
}
}
}
impl std::fmt::Display for ModelVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}@{}", self.id, self.version)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ForecastPoint {
pub at: OffsetDateTime,
pub region: Region,
pub expected: CarbonIntensity,
pub lower: CarbonIntensity,
pub upper: CarbonIntensity,
pub methodology: Methodology,
pub model: ModelVersion,
}
impl ForecastPoint {
#[allow(clippy::too_many_arguments)]
pub fn new(
at: OffsetDateTime,
region: Region,
expected: CarbonIntensity,
lower: CarbonIntensity,
upper: CarbonIntensity,
methodology: Methodology,
model: ModelVersion,
) -> Self {
let lower = if lower.value() <= expected.value() {
lower
} else {
expected
};
let upper = if upper.value() >= expected.value() {
upper
} else {
expected
};
Self {
at,
region,
expected,
lower,
upper,
methodology,
model,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ci(g: f64) -> CarbonIntensity {
CarbonIntensity::new(g).unwrap()
}
#[test]
fn model_version_displays_id_at_version() {
assert_eq!(
ModelVersion::new("climatology", 1).to_string(),
"climatology@1"
);
}
#[test]
fn construction_keeps_consistent_band() {
let p = ForecastPoint::new(
OffsetDateTime::UNIX_EPOCH,
Region::National,
ci(50.0),
ci(40.0),
ci(70.0),
Methodology::rte_direct(),
ModelVersion::new("climatology", 1),
);
assert_eq!(p.lower.value(), 40.0);
assert_eq!(p.expected.value(), 50.0);
assert_eq!(p.upper.value(), 70.0);
}
#[test]
fn inconsistent_bounds_are_snapped_to_expected() {
let p = ForecastPoint::new(
OffsetDateTime::UNIX_EPOCH,
Region::National,
ci(50.0),
ci(60.0), ci(45.0), Methodology::rte_direct(),
ModelVersion::new("climatology", 1),
);
assert_eq!(p.lower.value(), 50.0);
assert_eq!(p.upper.value(), 50.0);
assert!(p.lower.value() <= p.expected.value());
assert!(p.expected.value() <= p.upper.value());
}
}