diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index e1ca5f5..65e8312 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -56,9 +56,9 @@ impl<'a> From for Error<'a> { } } -fn read_to_string<'a>(file: &mut File<'a>, read_str: &mut String) -> Result<'a, ()> { - let mut buffer = Vec::new(); - let mut temp_buffer = [0; 500]; +// Simplified replacements to `read_to_end` and `read_to_string` from core_io +fn read_to_end<'a>(file: &mut File<'a>, buffer: &mut Vec) -> Result<'a, ()> { + let mut temp_buffer = [0; 1024]; loop { let read_bytes = file.read(&mut temp_buffer)?; if read_bytes != buffer.len() { @@ -69,18 +69,22 @@ fn read_to_string<'a>(file: &mut File<'a>, read_str: &mut String) -> Result<'a, buffer.extend_from_slice(&temp_buffer); } } - read_str.extend(String::from_utf8(buffer)); Ok(()) } +fn read_to_string<'a>(file: &mut File<'a>) -> Result<'a, String> { + let mut buffer: Vec = Vec::new(); + read_to_end(file, &mut buffer); + Ok(String::from_utf8(buffer)?) +} + fn parse_config<'a>( key: &'a str, buffer: &mut Vec, mut file: File<'a>, ) -> Result<'a, ()> { let prefix = [key, "="].concat().to_ascii_lowercase(); - let mut read_buffer = String::new(); - read_to_string(&mut file, &mut read_buffer)?; + let read_buffer = read_to_string(&mut file)?; for line in read_buffer.lines() { let line = line.to_ascii_lowercase(); if line.starts_with(&prefix) { @@ -125,7 +129,7 @@ impl Config { let root_dir = fs.root_dir(); let mut buffer: Vec = Vec::new(); match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) { - Ok(mut f) => f.read(&mut buffer).map(|_| ())?, + Ok(mut f) => read_to_end( &mut f, &mut buffer)?, Err(_) => match root_dir.open_file("/CONFIG.TXT") { Ok(f) => parse_config(key, &mut buffer, f)?, Err(_) => return Err(Error::KeyNotFoundError(key)), @@ -150,8 +154,7 @@ impl Config { let prefix = [key, "="].concat().to_ascii_lowercase(); match root_dir.create_file("/CONFIG.TXT") { Ok(mut f) => { - let mut buffer = String::new(); - read_to_string(&mut f, &mut buffer)?; + let buffer = read_to_string(&mut f)?; f.seek(SeekFrom::Start(0))?; f.truncate()?; for line in buffer.lines() {