diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-03-09 14:48:47 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-03-17 17:34:31 +0100 |
commit | 6ab07ddedaac9e4a906e8146f9de1b3a14fb15f2 (patch) | |
tree | 7d31498edb8b3093bfd54f43741b43a4f9b3c1db /src/config.rs | |
parent | a9ce6521d021909c13e52b50c3b26a28edaad65a (diff) |
Add a Config::set_once() function to set a value once (and let Config::merge() overwrite it later
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index 888d8cc..e6da482 100644 --- a/src/config.rs +++ b/src/config.rs @@ -151,6 +151,18 @@ impl Config { self.refresh() } + pub fn set_once(&mut self, key: &str, value: Value) -> Result<()> { + let expr: path::Expression = key.parse()?; + + // Traverse the cache using the path to (possibly) retrieve a value + if let Some(ref mut val) = expr.get_mut(&mut self.cache) { + **val = value; + } else { + expr.set(&mut self.cache, value); + } + Ok(()) + } + pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T> { // Parse the key into a path expression let expr: path::Expression = key.parse()?; |