mirror of
https://github.com/rustdesk/hbb_common.git
synced 2026-11-28 08:44:38 +00:00
refactor(config): keep option-preserving setters beside the bulk setters
Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
1 parent
7900bd4420
commit
e0e8340993
2 files changed
+31
-38
No files matched your search
+31
-1
@@ -19,7 +19,6 @@ 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,
|
||||
@@ -641,6 +640,26 @@ impl Config2 {
|
||||
lock.store();
|
||||
true
|
||||
}
|
||||
|
||||
/// 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
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn keep_encrypted_storage_if_plaintext_unchanged(plain: &str, stored: &str) -> String {
|
||||
@@ -1379,6 +1398,17 @@ impl Config {
|
||||
config.store();
|
||||
}
|
||||
|
||||
/// 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();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_option(k: &str) -> String {
|
||||
get_or(
|
||||
&OVERWRITE_SETTINGS,
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user