diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-03-09 14:48:47 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-03-13 11:29:25 +0100 |
commit | ea25d5ff9858a7cc03477f31cb3140a43977e6fd (patch) | |
tree | 3320472f8bc6874f01660f6887d88c773cba1d22 | |
parent | 887736dc67740e5316a892bfcb569f62d0fc45da (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>
-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()?; |