simm_rs/
engine_config.rs

1use serde::Deserialize;
2use anyhow::{Result, bail};
3use std::fs;
4use std::path::Path;
5
6#[derive(Debug, Deserialize)]
7pub struct EngineConfig {
8    pub weights_and_corr_version: String,
9    pub calculation_currency: String,
10    pub exchange_rate: f64,
11}
12
13impl EngineConfig {
14    /// Load from TOML file
15    pub fn load(path: impl AsRef<Path>) -> Result<Self> {
16        let text = fs::read_to_string(path)?;
17        let cfg: EngineConfig = toml::from_str(&text)?;
18        cfg.validate()?;
19        Ok(cfg)
20    }
21
22    /// Optional but recommended: semantic validation
23    fn validate(&self) -> Result<()> {
24        if self.weights_and_corr_version.is_empty() {
25            bail!("weights_and_corr_version must not be empty");
26        }
27
28        if self.calculation_currency.len() != 3 {
29            bail!("calculation_currency must be ISO-4217 (e.g. USD, EUR)");
30        }
31
32        if self.exchange_rate <= 0.0 {
33            bail!("exchange_rate must be > 0");
34        }
35
36        Ok(())
37    }
38}