pub trait Source {
    type Value: Serialize + DeserializeOwned;
    type Map: IntoIterator<Item = (String, Self::Value)>;

    // Required method
    fn default(&self) -> Result<Self::Map, Box<dyn Error>>;
}
Expand description

A trait for normal config source that is neither encrypted or persisted.

Example

use encrypt_config::{Config, Source};

#[cfg(feature = "secret")]
let mut config = Config::new("test");
#[cfg(not(feature = "secret"))]
let mut config = Config::new();

struct NormalSource;
impl Source for NormalSource {
    type Value = String;
    type Map = Vec<(String, Self::Value)>;

    fn default(&self) -> Result<Self::Map, Box<dyn std::error::Error>> {
        Ok(vec![("key".to_owned(), "value".to_owned())])
    }
}

config.add_source(NormalSource).unwrap();
let v: String = config.get("key").unwrap();
assert_eq!(v, "value");

Required Associated Types§

source

type Value: Serialize + DeserializeOwned

The type of the config value

source

type Map: IntoIterator<Item = (String, Self::Value)>

The type of the config map. It must be iterable, the first item of the tuple is the key, which should be String only.

Required Methods§

source

fn default(&self) -> Result<Self::Map, Box<dyn Error>>

The default config values from this source. This is the only way to add new config key-value pairs, because we cannot infer the source type(normal, persist and secret) of a new key after source merged into config if not so.

Implementors§