use crate::types::{DirectiveData, PluginError, PluginInput, PluginOutput};
use super::super::NativePlugin;
pub struct UniquePricesPlugin;
impl NativePlugin for UniquePricesPlugin {
fn name(&self) -> &'static str {
"unique_prices"
}
fn description(&self) -> &'static str {
"One price per day per currency pair"
}
fn process(&self, input: PluginInput) -> PluginOutput {
use std::collections::HashSet;
let mut seen: HashSet<(String, String, String)> = HashSet::new();
let mut errors = Vec::new();
for wrapper in &input.directives {
if let DirectiveData::Price(price) = &wrapper.data {
let key = (
wrapper.date.clone(),
price.currency.clone(),
price.amount.currency.clone(),
);
if !seen.insert(key.clone()) {
errors.push(PluginError::error(format!(
"Duplicate price for {}/{} on {}",
price.currency, price.amount.currency, wrapper.date
)));
}
}
}
PluginOutput {
directives: input.directives,
errors,
}
}
}