From 0f6276e72277439cd646c9fe447b2c2f3172abbf Mon Sep 17 00:00:00 2001 From: cw Date: Tue, 22 Sep 2020 17:28:30 +0800 Subject: [PATCH] WIP: fix borrow --- libconfig/src/lib.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 0b42072..7866e94 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -152,7 +152,7 @@ impl Config { let root_dir = fs.root_dir(); match root_dir.create_file("/CONFIG.TXT") { - Ok(file) => { + Ok(mut file) => { file.truncate()?; }, Err(e) => { @@ -230,6 +230,9 @@ impl Config { Ok(()) } pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{ + let call_remove_config_key_bin = false; + let call_remove_config_txt = false; + if let Some(fs) = &self.fs { let root_dir = fs.root_dir(); @@ -254,7 +257,7 @@ impl Config { file.seek(SeekFrom::End(0))?; file.write_all(&["\n", key, "=", data, "\n"].concat().as_bytes())?; - self.remove_config_key_bin(key)?; + call_remove_config_key_bin = true; } else { root_dir.create_dir("/CONFIG")?; match root_dir.create_file(config_key_bin) { @@ -267,12 +270,18 @@ impl Config { } }; - self.remove_config_txt(key)?; + call_remove_config_txt = true; } - - Ok(()) } else { Err(Error::NoConfig) } + + if call_remove_config_key_bin { + self.remove_config_key_bin(key)?; + } + if call_remove_config_txt { + self.remove_config_txt(key)?; + } + Ok(()) } }