libconfig rewrites

This commit is contained in:
Simon Renblad 2024-07-29 17:54:35 +08:00
parent a11d94f959
commit 83959b2691
2 changed files with 43 additions and 32 deletions

View File

@ -3,7 +3,6 @@ extern crate alloc;
use core::fmt; use core::fmt;
use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc}; use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc};
use core_io::{self as io, BufRead, BufReader, Read, Write, Seek, SeekFrom};
use libboard_zynq::sdio; use libboard_zynq::sdio;
pub mod sd_reader; pub mod sd_reader;
@ -13,7 +12,7 @@ pub mod bootgen;
#[derive(Debug)] #[derive(Debug)]
pub enum Error<'a> { pub enum Error<'a> {
SdError(sdio::sd_card::CardInitializationError), SdError(sdio::sd_card::CardInitializationError),
IoError(io::Error), IoError(sd_reader::SdReader::Error),
Utf8Error(FromUtf8Error), Utf8Error(FromUtf8Error),
KeyNotFoundError(&'a str), KeyNotFoundError(&'a str),
NoConfig, NoConfig,
@ -39,8 +38,8 @@ impl<'a> From<sdio::sd_card::CardInitializationError> for Error<'a> {
} }
} }
impl<'a> From<io::Error> for Error<'a> { impl<'a> From<sd_reader::SdReader::Error> for Error<'a> {
fn from(error: io::Error) -> Self { fn from(error: sd_reader::SdReader::Error) -> Self {
Error::IoError(error) Error::IoError(error)
} }
} }
@ -57,14 +56,29 @@ fn parse_config<'a>(
file: fatfs::File<sd_reader::SdReader>, file: fatfs::File<sd_reader::SdReader>,
) -> Result<'a, ()> { ) -> Result<'a, ()> {
let prefix = [key, "="].concat().to_ascii_lowercase(); let prefix = [key, "="].concat().to_ascii_lowercase();
for line in BufReader::new(file).lines() { let mut file_buf: Vec<u8> = Vec::new();
let line = line?.to_ascii_lowercase(); loop {
if line.starts_with(&prefix) { match memchr::memchr(b'\n', &file_buf) {
buffer.extend(line[prefix.len()..].as_bytes()); Some(i) => {
return Ok(()); let s = String::from_utf8(&file_buf[..=i])
if s.starts_with(&prefix) {
buffer.extend_from_slice(&file_buf[..=i]);
return Ok(())
} else {
file.consume(i + 1)
}
}
None => {
let s = String::from_utf8(file_buf)
if s.starts_with(&prefix) {
buffer.extend_from_slice(file_buf);
return Ok(())
} else {
return Err(Error::KeyNotFoundError(key))
}
}
} }
} }
Err(Error::KeyNotFoundError(key))
} }
pub struct Config { pub struct Config {

View File

@ -188,6 +188,25 @@ impl SdReader {
fn cmd_error_to_io_error(_: CmdTransferError) -> Self::Error { fn cmd_error_to_io_error(_: CmdTransferError) -> Self::Error {
Self::Error::Other("Command transfer error") Self::Error::Other("Command transfer error")
} }
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
if self.index == BLOCK_SIZE {
// flush the buffer if it is dirty before overwriting it with new data
if self.dirty {
self.flush()?;
}
// reload buffer
self.sd
.read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
.map_err(cmd_error_to_io_error)?;
self.index = (self.byte_addr as usize) % BLOCK_SIZE;
}
Ok(&self.buffer[self.index..])
}
fn consume(&mut self, amt: usize) {
self.index += amt;
self.byte_addr += amt as u32;
}
} }
@ -217,28 +236,6 @@ impl Read for SdReader {
} }
} }
// impl BufRead for SdReader {
// fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
// if self.index == BLOCK_SIZE {
// // flush the buffer if it is dirty before overwriting it with new data
// if self.dirty {
// self.flush()?;
// }
// // reload buffer
// self.sd
// .read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
// .map_err(cmd_error_to_io_error)?;
// self.index = (self.byte_addr as usize) % BLOCK_SIZE;
// }
// Ok(&self.buffer[self.index..])
// }
//
// fn consume(&mut self, amt: usize) {
// self.index += amt;
// self.byte_addr += amt as u32;
// }
// }
impl Write for SdReader { impl Write for SdReader {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let (a, b, c) = self.block_align(buf); let (a, b, c) = self.block_align(buf);