use chrono::NaiveDate;
use super::resolve::all_rulesets;
use crate::ruleset::Effectivity;
#[derive(Debug, Clone, PartialEq)]
pub enum CalculatorImpl {
Ruleset(&'static str),
ReportingOnly,
NotImplemented,
}
#[derive(Debug, Clone, PartialEq)]
pub enum CalculatorStatus {
Active { ruleset_id: &'static str },
NotYetEffective {
ruleset_id: &'static str,
from: NaiveDate,
},
AwaitingInstrument {
ruleset_id: &'static str,
empowerment: &'static str,
},
Expired {
ruleset_id: &'static str,
until: NaiveDate,
},
NotImplemented,
ReportingOnly,
UnknownRuleset { ruleset_id: &'static str },
}
pub struct SectorCalculatorEntry {
pub sector_key: &'static str,
pub product_category: &'static str,
pub methodology: &'static str,
pub implementation: CalculatorImpl,
}
impl SectorCalculatorEntry {
#[must_use]
pub fn status_on(&self, law_in_force_on: NaiveDate) -> CalculatorStatus {
let ruleset_id = match self.implementation {
CalculatorImpl::ReportingOnly => return CalculatorStatus::ReportingOnly,
CalculatorImpl::NotImplemented => return CalculatorStatus::NotImplemented,
CalculatorImpl::Ruleset(id) => id,
};
let Some(ruleset) = all_rulesets().iter().find(|r| r.id().0 == ruleset_id) else {
return CalculatorStatus::UnknownRuleset { ruleset_id };
};
match ruleset.effectivity() {
Effectivity::Pending { empowerment, .. } => CalculatorStatus::AwaitingInstrument {
ruleset_id,
empowerment,
},
Effectivity::InForce { from, until } => {
if law_in_force_on < *from {
CalculatorStatus::NotYetEffective {
ruleset_id,
from: *from,
}
} else if let Some(until) = until
&& law_in_force_on > *until
{
CalculatorStatus::Expired {
ruleset_id,
until: *until,
}
} else {
CalculatorStatus::Active { ruleset_id }
}
}
}
}
}
pub fn sector_calculator_map() -> &'static [SectorCalculatorEntry] {
&[
SectorCalculatorEntry {
sector_key: "electronics",
product_category: "smartphone-tablet",
methodology: "repairability-heuristic",
implementation: CalculatorImpl::Ruleset("repairability-heuristic-v1"),
},
SectorCalculatorEntry {
sector_key: "electronics",
product_category: "laptop",
methodology: "repairability-heuristic",
implementation: CalculatorImpl::Ruleset("laptop-repairability"),
},
SectorCalculatorEntry {
sector_key: "electronics",
product_category: "displays",
methodology: "repairability-heuristic",
implementation: CalculatorImpl::Ruleset("displays-repairability"),
},
SectorCalculatorEntry {
sector_key: "electronics",
product_category: "washing-machine",
methodology: "repairability-heuristic",
implementation: CalculatorImpl::Ruleset("washing-machine-repairability"),
},
SectorCalculatorEntry {
sector_key: "battery",
product_category: "all",
methodology: "co2e-pef",
implementation: CalculatorImpl::Ruleset("co2e-cradle-to-gate"),
},
SectorCalculatorEntry {
sector_key: "battery",
product_category: "all",
methodology: "co2e-battery-regulation-art7",
implementation: CalculatorImpl::NotImplemented,
},
SectorCalculatorEntry {
sector_key: "unsoldGoods",
product_category: "all",
methodology: "unsold-goods-reporting",
implementation: CalculatorImpl::ReportingOnly,
},
]
}