diff --git a/src/config.rs b/src/config.rs index 9ee5cacde..94a166525 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,6 +19,7 @@ use sodiumoxide::base64; use sodiumoxide::crypto::sign; mod permanent_password; +mod preserved_option; pub use permanent_password::{ compute_permanent_password_h1, decode_permanent_password_h1_from_storage, diff --git a/src/config/preserved_option.rs b/src/config/preserved_option.rs new file mode 100644 index 000000000..704359474 --- /dev/null +++ b/src/config/preserved_option.rs @@ -0,0 +1,37 @@ +use super::{Config, Config2, CONFIG2}; +use std::collections::HashMap; + +impl Config2 { + fn preserve_option(&self, options: &mut HashMap, key: &str) { + if let Some(value) = self.options.get(key) { + options.insert(key.to_owned(), value.clone()); + } else { + options.remove(key); + } + } + + /// Replaces the configuration while retaining the locally stored entry for `key`. + pub fn set_preserving_option(mut incoming: Self, key: &str) -> bool { + let mut current = CONFIG2.write().unwrap(); + current.preserve_option(&mut incoming.options, key); + if *current == incoming { + return false; + } + *current = incoming; + current.store(); + true + } +} + +impl Config { + /// Replaces the options while retaining the locally stored entry for `key`. + pub fn set_options_preserving_option(mut options: HashMap, key: &str) { + Self::purify_options(&mut options); + let mut current = CONFIG2.write().unwrap(); + current.preserve_option(&mut options, key); + if current.options != options { + current.options = options; + current.store(); + } + } +}