Skip to main content

config/
mem.rs

1use crate::{Result, Settings};
2
3/// Represents a [configuration provider](crate::Provider) for in-memory data.
4#[derive(Debug, Default)]
5pub struct Provider {
6    /// Gets a list of key/value pairs representing the initial data.
7    pub data: Vec<(String, String)>,
8}
9
10impl Provider {
11    /// Initializes a new in-memory configuration provider.
12    ///
13    /// # Arguments
14    ///
15    /// * `data` - The list of key/value pairs representing the initial data
16    pub fn new<S: AsRef<str>>(data: &[(S, S)]) -> Self {
17        Self {
18            data: data
19                .iter()
20                .map(|t| (t.0.as_ref().to_owned(), t.1.as_ref().to_owned()))
21                .collect(),
22        }
23    }
24}
25
26impl crate::Provider for Provider {
27    #[inline]
28    fn name(&self) -> &str {
29        "Memory"
30    }
31
32    fn load(&self, settings: &mut Settings) -> Result {
33        for (key, value) in &self.data {
34            settings.insert(key.clone(), value.clone());
35        }
36
37        Ok(())
38    }
39}