From 22db44fdb989b7f341aefdd544f9ff74b6d8bce3 Mon Sep 17 00:00:00 2001 From: cw Date: Fri, 28 Aug 2020 12:02:26 +0800 Subject: [PATCH] Improve error handling and rename variables --- src/runtime/src/config.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/runtime/src/config.rs b/src/runtime/src/config.rs index 91c3136..18c1898 100644 --- a/src/runtime/src/config.rs +++ b/src/runtime/src/config.rs @@ -1,7 +1,7 @@ use crate::sd_reader; use core::fmt; use alloc::{string::FromUtf8Error, string::String, vec::Vec}; -use core_io::{self as io, BufRead, BufReader, Read, Write}; +use core_io::{self as io, BufRead, BufReader, Read, Write, ErrorKind}; use libboard_zynq::sdio; @@ -107,19 +107,18 @@ impl Config { pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{ if let Some(fs) = &self.fs { let root_dir = fs.root_dir(); - let mut file; - let isascii_and_short = data.is_ascii() & (data.len() <= 100); + let use_config_txt = data.is_ascii() & (data.len() <= 100); let file_path; let config_key_bin = &["/CONFIG/", key, ".BIN"].concat(); - if isascii_and_short { + if use_config_txt { file_path = "/CONFIG.TXT"; } else { file_path = config_key_bin; } - file = match root_dir.create_file(file_path) { + let mut file = match root_dir.create_file(file_path) { Ok(f) => f, Err(_) => root_dir.open_file("/CONFIG.TXT")? }; @@ -128,10 +127,15 @@ impl Config { file.write_all(&[key,"="].concat().as_bytes())?; file.write_all(data.as_bytes())?; - if isascii_and_short { + if use_config_txt { match root_dir.remove(config_key_bin) { Ok(_) => {}, - Err(_) => {} + Err(e) => match e.kind() { + ErrorKind::NotFound => {}, + _ => { + return Err(Error::IoError(e)); + } + } }; }