use std::collections::HashMap;
use chrono::NaiveDate;
use super::strategies::{PassthroughBatteryStrategy, PassthroughTextileStrategy};
use crate::{
compliance::{ComplianceError, ComplianceResult},
ports::compliance::{ComplianceRegistry, ComplianceStrategy},
product_group::ProductGroupData,
};
pub struct PassthroughRegistry {
strategies: HashMap<String, Box<dyn ComplianceStrategy>>,
}
impl PassthroughRegistry {
#[must_use]
pub fn new() -> Self {
let mut registry = Self {
strategies: HashMap::new(),
};
registry.register(Box::new(PassthroughBatteryStrategy));
registry.register(Box::new(PassthroughTextileStrategy));
registry
}
#[must_use]
pub fn empty() -> Self {
Self {
strategies: HashMap::new(),
}
}
pub fn register(&mut self, strategy: Box<dyn ComplianceStrategy>) {
self.strategies
.insert(strategy.product_group_key().to_owned(), strategy);
}
#[must_use]
pub fn registered_product_groups(&self) -> Vec<&str> {
let mut keys: Vec<&str> = self.strategies.keys().map(String::as_str).collect();
keys.sort_unstable();
keys
}
}
impl Default for PassthroughRegistry {
fn default() -> Self {
Self::new()
}
}
impl ComplianceRegistry for PassthroughRegistry {
fn compute(
&self,
product_group_key: &str,
data: &ProductGroupData,
law_in_force_on: Option<NaiveDate>,
) -> Result<ComplianceResult, ComplianceError> {
match self.strategies.get(product_group_key) {
Some(strategy) => strategy.compute(data, law_in_force_on),
None => Ok(ComplianceResult::passthrough()),
}
}
}