Trait encrypt_config::PersistSource
source · pub trait PersistSource {
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
persist only.Expand description
A trait for persisted but not encrypted config source.
Example
use encrypt_config::{Config, PersistSource};
use serde::{Deserialize, Serialize};
let mut config = Config::new("test");
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Foo(String);
struct PersistSourceImpl;
impl PersistSource for PersistSourceImpl {
type Value = Foo;
type Map = Vec<(String, Self::Value)>;
fn path(&self) -> std::path::PathBuf {
std::path::PathBuf::from("tests").join("persist.conf")
}
fn default(&self) -> Result<Self::Map, Box<dyn std::error::Error>> {
Ok(vec![("persist".to_owned(), Foo("persist".to_owned()))])
}
}
config.add_persist_source(PersistSourceImpl).unwrap();
let new_value = Foo("new persist".to_owned());
config.upgrade("persist", &new_value).unwrap();
assert_eq!(config.get::<_, Foo>("persist").unwrap(), new_value);
let mut config_new = Config::new("test");
config_new.add_persist_source(PersistSourceImpl).unwrap(); // Read config from disk
assert_eq!(config_new.get::<_, Foo>("persist").unwrap(), new_value);Required Associated Types§
sourcetype Value: Serialize + DeserializeOwned
type Value: Serialize + DeserializeOwned
The type of the config value
sourcetype Map: IntoIterator<Item = (String, Self::Value)>
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.