Skip to main content

rustledger_plugin/native/plugins/
unique_prices.rs

1//! One price per day per currency pair.
2
3use crate::types::{DirectiveData, PluginError, PluginInput, PluginOutput};
4
5use super::super::NativePlugin;
6
7/// Plugin that enforces unique prices (one per commodity pair per day).
8pub struct UniquePricesPlugin;
9
10impl NativePlugin for UniquePricesPlugin {
11    fn name(&self) -> &'static str {
12        "unique_prices"
13    }
14
15    fn description(&self) -> &'static str {
16        "One price per day per currency pair"
17    }
18
19    fn process(&self, input: PluginInput) -> PluginOutput {
20        use std::collections::HashSet;
21
22        // Track (date, base_currency, quote_currency) tuples
23        let mut seen: HashSet<(String, String, String)> = HashSet::new();
24        let mut errors = Vec::new();
25
26        for wrapper in &input.directives {
27            if let DirectiveData::Price(price) = &wrapper.data {
28                let key = (
29                    wrapper.date.clone(),
30                    price.currency.clone(),
31                    price.amount.currency.clone(),
32                );
33                if !seen.insert(key.clone()) {
34                    errors.push(PluginError::error(format!(
35                        "Duplicate price for {}/{} on {}",
36                        price.currency, price.amount.currency, wrapper.date
37                    )));
38                }
39            }
40        }
41
42        PluginOutput {
43            directives: input.directives,
44            errors,
45        }
46    }
47}