Skip to content

Commit 6ab07dd

Browse files
committed
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>
1 parent a9ce652 commit 6ab07dd

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ impl Config {
151151
self.refresh()
152152
}
153153

154+
pub fn set_once(&mut self, key: &str, value: Value) -> Result<()> {
155+
let expr: path::Expression = key.parse()?;
156+
157+
// Traverse the cache using the path to (possibly) retrieve a value
158+
if let Some(ref mut val) = expr.get_mut(&mut self.cache) {
159+
**val = value;
160+
} else {
161+
expr.set(&mut self.cache, value);
162+
}
163+
Ok(())
164+
}
165+
154166
pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T> {
155167
// Parse the key into a path expression
156168
let expr: path::Expression = key.parse()?;

0 commit comments

Comments
 (0)