use time::OffsetDateTime;
use crate::domain::CarbonIntensity;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Neighbor {
Belgium,
Germany,
Spain,
Italy,
Switzerland,
GreatBritain,
}
impl Neighbor {
pub const ALL: [Neighbor; 6] = [
Neighbor::Belgium,
Neighbor::Germany,
Neighbor::Spain,
Neighbor::Italy,
Neighbor::Switzerland,
Neighbor::GreatBritain,
];
pub fn slug(self) -> &'static str {
match self {
Neighbor::Belgium => "be",
Neighbor::Germany => "de-lu",
Neighbor::Spain => "es",
Neighbor::Italy => "it-north",
Neighbor::Switzerland => "ch",
Neighbor::GreatBritain => "gb",
}
}
pub fn from_slug(slug: &str) -> Option<Neighbor> {
Neighbor::ALL.into_iter().find(|n| n.slug() == slug)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CrossBorderFlow {
pub neighbor: Neighbor,
pub flow_mw: f64,
pub neighbor_intensity: CarbonIntensity,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct CrossBorderFlows {
pub flows: Vec<CrossBorderFlow>,
}
impl CrossBorderFlows {
pub fn new(flows: Vec<CrossBorderFlow>) -> Self {
Self { flows }
}
pub fn imports_mw(&self) -> f64 {
self.flows.iter().map(|f| f.flow_mw.max(0.0)).sum()
}
pub fn exports_mw(&self) -> f64 {
self.flows.iter().map(|f| (-f.flow_mw).max(0.0)).sum()
}
pub fn imported_emissions(&self) -> f64 {
self.flows
.iter()
.filter(|f| f.flow_mw > 0.0)
.map(|f| f.flow_mw * f.neighbor_intensity.value())
.sum()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CrossBorderSnapshot {
pub at: OffsetDateTime,
pub flows: CrossBorderFlows,
}