feat(config): allow bulk writes to preserve a stored option

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou committed 2026-11-26 22:07:01 +08:00
1 parent 450cc1d7b7
commit 7900bd4420
2 files changed
+38

No files matched your search

+1
View File
@@ -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,
+37
View File
@@ -0,0 +1,37 @@
use super::{Config, Config2, CONFIG2};
use std::collections::HashMap;
impl Config2 {
fn preserve_option(&self, options: &mut HashMap<String, String>, 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<String, String>, 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();
}
}
}