use crate::domain::CarbonIntensity;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IntensityIndex {
VeryLow,
Low,
Moderate,
High,
VeryHigh,
}
impl IntensityIndex {
pub const VERSION: u32 = 1;
pub const THRESHOLDS_V1: [f64; 4] = [20.0, 64.8, 100.0, 200.0];
pub const ALL: [IntensityIndex; 5] = [
IntensityIndex::VeryLow,
IntensityIndex::Low,
IntensityIndex::Moderate,
IntensityIndex::High,
IntensityIndex::VeryHigh,
];
pub fn of(intensity: CarbonIntensity) -> Self {
let value = intensity.value();
let rank = Self::THRESHOLDS_V1
.iter()
.take_while(|&&threshold| value >= threshold)
.count();
Self::ALL[rank]
}
pub fn slug(self) -> &'static str {
match self {
IntensityIndex::VeryLow => "very-low",
IntensityIndex::Low => "low",
IntensityIndex::Moderate => "moderate",
IntensityIndex::High => "high",
IntensityIndex::VeryHigh => "very-high",
}
}
pub fn bounds(self) -> (Option<f64>, Option<f64>) {
let rank = Self::ALL
.iter()
.position(|&band| band == self)
.unwrap_or(Self::ALL.len() - 1);
let min = rank.checked_sub(1).map(|i| Self::THRESHOLDS_V1[i]);
let max = Self::THRESHOLDS_V1.get(rank).copied();
(min, max)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn index_of(g: f64) -> IntensityIndex {
IntensityIndex::of(CarbonIntensity::new(g).unwrap())
}
#[test]
fn bands_are_closed_below_and_open_above() {
assert_eq!(index_of(0.0), IntensityIndex::VeryLow);
assert_eq!(index_of(19.999), IntensityIndex::VeryLow);
assert_eq!(index_of(20.0), IntensityIndex::Low);
assert_eq!(index_of(64.79), IntensityIndex::Low);
assert_eq!(index_of(64.8), IntensityIndex::Moderate);
assert_eq!(index_of(99.99), IntensityIndex::Moderate);
assert_eq!(index_of(100.0), IntensityIndex::High);
assert_eq!(index_of(199.9), IntensityIndex::High);
assert_eq!(index_of(200.0), IntensityIndex::VeryHigh);
assert_eq!(index_of(1_038.0), IntensityIndex::VeryHigh);
assert_eq!(index_of(f64::MAX), IntensityIndex::VeryHigh);
}
#[test]
fn thresholds_are_the_adr_0033_ones_and_increasing() {
assert_eq!(IntensityIndex::THRESHOLDS_V1, [20.0, 64.8, 100.0, 200.0]);
assert!(
IntensityIndex::THRESHOLDS_V1
.windows(2)
.all(|pair| pair[0] < pair[1])
);
assert_eq!(IntensityIndex::VERSION, 1);
assert!((18.0 * 3.6 - IntensityIndex::THRESHOLDS_V1[1]).abs() < 1e-9);
}
#[test]
fn slugs_are_kebab_case_and_unique() {
let slugs: Vec<&str> = IntensityIndex::ALL.iter().map(|b| b.slug()).collect();
assert_eq!(slugs, ["very-low", "low", "moderate", "high", "very-high"]);
for slug in slugs {
assert!(
slug.chars().all(|c| c.is_ascii_lowercase() || c == '-'),
"{slug}"
);
}
}
#[test]
fn bounds_follow_the_thresholds() {
assert_eq!(IntensityIndex::VeryLow.bounds(), (None, Some(20.0)));
assert_eq!(IntensityIndex::Low.bounds(), (Some(20.0), Some(64.8)));
assert_eq!(IntensityIndex::Moderate.bounds(), (Some(64.8), Some(100.0)));
assert_eq!(IntensityIndex::High.bounds(), (Some(100.0), Some(200.0)));
assert_eq!(IntensityIndex::VeryHigh.bounds(), (Some(200.0), None));
for pair in IntensityIndex::ALL.windows(2) {
assert_eq!(pair[0].bounds().1, pair[1].bounds().0);
}
}
#[test]
fn bands_are_ordered() {
assert!(IntensityIndex::VeryLow < IntensityIndex::Low);
assert!(IntensityIndex::High < IntensityIndex::VeryHigh);
assert!(IntensityIndex::ALL.windows(2).all(|p| p[0] < p[1]));
}
}