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

    // Required methods
    fn default(&self) -> Result<Self::Map, Box<dyn Error>>;
    fn path(&self) -> PathBuf;
}
Available on crate feature secret only.
Expand description

A trait for persisted and encrypted config source.

Example

use encrypt_config::{Config, SecretSource};
use serde::{Deserialize, Serialize};

let mut config = Config::new("test");

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Foo(String);

struct SecretSourceImpl;
impl SecretSource for SecretSourceImpl {
    type Value = Foo;
    type Map = Vec<(String, Self::Value)>;

    fn path(&self) -> std::path::PathBuf {
        std::path::PathBuf::from("tests").join("secret.conf")
    }

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

config.add_secret_source(SecretSourceImpl).unwrap();
assert_eq!(config.get::<_, Foo>("secret").unwrap(), Foo("secret".to_owned()));
let new_value = Foo("new secret".to_owned());
config.upgrade("secret", &new_value).unwrap();
assert_eq!(config.get::<_, Foo>("secret").unwrap(), new_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.

source

fn path(&self) -> PathBuf

Available on non-crate feature default_config_dir only.

The path to persist the config file.

Implementors§