better option save
This commit is contained in:
		
							parent
							
								
									a3f79065c5
								
							
						
					
					
						commit
						077569dd5d
					
				| @ -903,7 +903,13 @@ impl Config { | |||||||
|         res |         res | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn set_options(v: HashMap<String, String>) { |     #[inline] | ||||||
|  |     fn purify_options(v: &mut HashMap<String, String>) { | ||||||
|  |         v.retain(|k, v| is_option_can_save(&OVERWRITE_SETTINGS, &DEFAULT_SETTINGS, k, v)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn set_options(mut v: HashMap<String, String>) { | ||||||
|  |         Self::purify_options(&mut v); | ||||||
|         let mut config = CONFIG2.write().unwrap(); |         let mut config = CONFIG2.write().unwrap(); | ||||||
|         if config.options == v { |         if config.options == v { | ||||||
|             return; |             return; | ||||||
| @ -923,6 +929,9 @@ impl Config { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn set_option(k: String, v: String) { |     pub fn set_option(k: String, v: String) { | ||||||
|  |         if !is_option_can_save(&OVERWRITE_SETTINGS, &DEFAULT_SETTINGS, &k, &v) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         let mut config = CONFIG2.write().unwrap(); |         let mut config = CONFIG2.write().unwrap(); | ||||||
|         let v2 = if v.is_empty() { None } else { Some(&v) }; |         let v2 = if v.is_empty() { None } else { Some(&v) }; | ||||||
|         if v2 != config.options.get(&k) { |         if v2 != config.options.get(&k) { | ||||||
| @ -1384,6 +1393,9 @@ impl LocalConfig { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn set_option(k: String, v: String) { |     pub fn set_option(k: String, v: String) { | ||||||
|  |         if !is_option_can_save(&OVERWRITE_LOCAL_SETTINGS, &DEFAULT_LOCAL_SETTINGS, &k, &v) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         let mut config = LOCAL_CONFIG.write().unwrap(); |         let mut config = LOCAL_CONFIG.write().unwrap(); | ||||||
|         let v2 = if v.is_empty() { None } else { Some(&v) }; |         let v2 = if v.is_empty() { None } else { Some(&v) }; | ||||||
|         if v2 != config.options.get(&k) { |         if v2 != config.options.get(&k) { | ||||||
| @ -1560,6 +1572,14 @@ impl UserDefaultConfig { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn set(&mut self, key: String, value: String) { |     pub fn set(&mut self, key: String, value: String) { | ||||||
|  |         if !is_option_can_save( | ||||||
|  |             &OVERWRITE_DISPLAY_SETTINGS, | ||||||
|  |             &DEFAULT_DISPLAY_SETTINGS, | ||||||
|  |             &key, | ||||||
|  |             &value, | ||||||
|  |         ) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         if value.is_empty() { |         if value.is_empty() { | ||||||
|             self.options.remove(&key); |             self.options.remove(&key); | ||||||
|         } else { |         } else { | ||||||
| @ -1853,6 +1873,22 @@ fn get_or( | |||||||
|         .cloned() |         .cloned() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #[inline] | ||||||
|  | fn is_option_can_save( | ||||||
|  |     overwrite: &RwLock<HashMap<String, String>>, | ||||||
|  |     defaults: &RwLock<HashMap<String, String>>, | ||||||
|  |     k: &str, | ||||||
|  |     v: &str, | ||||||
|  | ) -> bool { | ||||||
|  |     if overwrite.read().unwrap().contains_key(k) { | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  |     if defaults.read().unwrap().get(k).map_or(false, |x| x == v) { | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  |     true | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[inline] | #[inline] | ||||||
| pub fn is_incoming_only() -> bool { | pub fn is_incoming_only() -> bool { | ||||||
|     HARD_SETTINGS |     HARD_SETTINGS | ||||||
| @ -1947,6 +1983,33 @@ mod tests { | |||||||
|             .write() |             .write() | ||||||
|             .unwrap() |             .unwrap() | ||||||
|             .insert("d".to_string(), "c".to_string()); |             .insert("d".to_string(), "c".to_string()); | ||||||
|  |         let mut res: HashMap<String, String> = Default::default(); | ||||||
|  |         res.insert("b".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("d".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("c".to_owned(), "a".to_string()); | ||||||
|  |         Config::purify_options(&mut res); | ||||||
|  |         assert!(res.len() == 0); | ||||||
|  |         res.insert("b".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("d".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("c".to_owned(), "a".to_string()); | ||||||
|  |         res.insert("f".to_owned(), "a".to_string()); | ||||||
|  |         Config::purify_options(&mut res); | ||||||
|  |         assert!(res.len() == 1); | ||||||
|  |         res.insert("b".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("d".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("c".to_owned(), "a".to_string()); | ||||||
|  |         res.insert("f".to_owned(), "a".to_string()); | ||||||
|  |         res.insert("c".to_owned(), "d".to_string()); | ||||||
|  |         Config::purify_options(&mut res); | ||||||
|  |         assert!(res.len() == 2); | ||||||
|  |         res.insert("b".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("d".to_owned(), "c".to_string()); | ||||||
|  |         res.insert("c".to_owned(), "a".to_string()); | ||||||
|  |         res.insert("f".to_owned(), "a".to_string()); | ||||||
|  |         res.insert("c".to_owned(), "d".to_string()); | ||||||
|  |         res.insert("d".to_owned(), "cc".to_string()); | ||||||
|  |         Config::purify_options(&mut res); | ||||||
|  |         assert!(res.len() == 2); | ||||||
|         let res = Config::get_options(); |         let res = Config::get_options(); | ||||||
|         assert!(res["a"] == "b"); |         assert!(res["a"] == "b"); | ||||||
|         assert!(res["c"] == "a"); |         assert!(res["c"] == "a"); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user