Skip to main content

rskit_config/source/
map.rs

1use rskit_errors::{AppError, AppResult};
2
3use super::contract::ConfigSource;
4
5/// In-memory key/value config source.
6#[derive(Debug, Clone, Default)]
7pub struct ConfigMapSource {
8    values: Vec<(String, config::Value)>,
9}
10
11impl ConfigMapSource {
12    /// Create an empty in-memory source.
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    /// Add a value to the source.
18    #[must_use]
19    pub fn with_value(mut self, key: impl Into<String>, value: impl Into<config::Value>) -> Self {
20        self.values.push((key.into(), value.into()));
21        self
22    }
23}
24
25impl ConfigSource for ConfigMapSource {
26    fn collect(&self) -> AppResult<config::Config> {
27        let mut builder = config::Config::builder();
28        for (key, value) in &self.values {
29            builder = builder
30                .set_override(key, value.clone())
31                .map_err(|e| AppError::invalid_input("config", e.to_string()))?;
32        }
33        builder
34            .build()
35            .map_err(|e| AppError::invalid_input("config", e.to_string()))
36    }
37}