Compare commits

...

2 Commits

2 changed files with 21 additions and 25 deletions

View File

@ -11,17 +11,17 @@ pub mod net_settings;
pub mod bootgen;
#[derive(Debug)]
pub enum Error<'a> {
pub enum Error {
SdError(sdio::sd_card::CardInitializationError),
IoError(io::Error),
Utf8Error(FromUtf8Error),
KeyNotFoundError(&'a str),
KeyNotFoundError(String),
NoConfig,
}
pub type Result<'a, T> = core::result::Result<T, Error<'a>>;
pub type Result<T> = core::result::Result<T, Error>;
impl<'a> fmt::Display for Error<'a> {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::SdError(error) => write!(f, "SD error: {}", error),
@ -33,29 +33,29 @@ impl<'a> fmt::Display for Error<'a> {
}
}
impl<'a> From<sdio::sd_card::CardInitializationError> for Error<'a> {
impl From<sdio::sd_card::CardInitializationError> for Error {
fn from(error: sdio::sd_card::CardInitializationError) -> Self {
Error::SdError(error)
}
}
impl<'a> From<io::Error> for Error<'a> {
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::IoError(error)
}
}
impl<'a> From<FromUtf8Error> for Error<'a> {
impl From<FromUtf8Error> for Error {
fn from(error: FromUtf8Error) -> Self {
Error::Utf8Error(error)
}
}
fn parse_config<'a>(
key: &'a str,
fn parse_config(
key: &str,
buffer: &mut Vec<u8>,
file: fatfs::File<sd_reader::SdReader>,
) -> Result<'a, ()> {
) -> Result<()> {
let prefix = [key, "="].concat().to_ascii_lowercase();
for line in BufReader::new(file).lines() {
let line = line?.to_ascii_lowercase();
@ -64,7 +64,7 @@ fn parse_config<'a>(
return Ok(());
}
}
Err(Error::KeyNotFoundError(key))
Err(Error::KeyNotFoundError(key.into()))
}
pub struct Config {
@ -74,7 +74,7 @@ pub struct Config {
const NEWLINE: &[u8] = b"\n";
impl Config {
pub fn new() -> Result<'static, Self> {
pub fn new() -> Result<Self> {
let sdio = sdio::Sdio::sdio0(true);
if !sdio.is_card_inserted() {
Err(sdio::sd_card::CardInitializationError::NoCardInserted)?;
@ -94,7 +94,7 @@ impl Config {
Config { fs: None }
}
pub fn read<'b>(&self, key: &'b str) -> Result<'b, Vec<u8>> {
pub fn read(&self, key: &str) -> Result<Vec<u8>> {
if let Some(fs) = &self.fs {
let root_dir = fs.root_dir();
let mut buffer: Vec<u8> = Vec::new();
@ -102,7 +102,7 @@ impl Config {
Ok(mut f) => f.read_to_end(&mut buffer).map(|_| ())?,
Err(_) => match root_dir.open_file("/CONFIG.TXT") {
Ok(f) => parse_config(key, &mut buffer, f)?,
Err(_) => return Err(Error::KeyNotFoundError(key)),
Err(_) => return Err(Error::KeyNotFoundError(key.into())),
},
};
Ok(buffer)
@ -111,11 +111,11 @@ impl Config {
}
}
pub fn read_str<'b>(&self, key: &'b str) -> Result<'b, String> {
pub fn read_str(&self, key: &str) -> Result<String> {
Ok(String::from_utf8(self.read(key)?)?)
}
pub fn remove<'b>(&self, key: &'b str) -> Result<'b, ()> {
pub fn remove(&self, key: &str) -> Result<()> {
if let Some(fs) = &self.fs {
let root_dir = fs.root_dir();
match root_dir.remove(&["/CONFIG/", key, ".BIN"].concat()) {
@ -136,7 +136,7 @@ impl Config {
}
Ok(())
},
Err(_) => Err(Error::KeyNotFoundError(key))
Err(_) => Err(Error::KeyNotFoundError(key.into()))
}
}
}
@ -145,7 +145,7 @@ impl Config {
}
}
pub fn write<'b>(&self, key: &'b str, value: Vec<u8>) -> Result<'b, ()> {
pub fn write(&self, key: &str, value: Vec<u8>) -> Result<()> {
if self.fs.is_none() {
return Err(Error::NoConfig);
}

View File

@ -1,6 +1,6 @@
use core_io::{BufRead, Error, ErrorKind, Read, Result as IoResult, Seek, SeekFrom, Write};
use fatfs;
use libboard_zynq::sdio::{sd_card::SdCard, CmdTransferError};
use libboard_zynq::sdio::sd_card::SdCard;
use log::debug;
use alloc::vec::Vec;
@ -12,10 +12,6 @@ const PARTID_FAT32: u8 = 0x0B;
const PARTID_FAT32_LBA: u8 = 0x0C;
const PARTID_FAT16_LBA: u8 = 0x0E;
fn cmd_error_to_io_error(_: CmdTransferError) -> Error {
Error::new(ErrorKind::Other, "Command transfer error")
}
const BLOCK_SIZE: usize = 512;
/// SdReader struct implementing `Read + BufRead + Write + Seek` traits for `core_io`.
@ -221,7 +217,7 @@ impl BufRead for SdReader {
// reload buffer
self.sd
.read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
.map_err(cmd_error_to_io_error)?;
.map_err(|_| Error::new(ErrorKind::Other, "Command transfer error"))?;
self.index = (self.byte_addr as usize) % BLOCK_SIZE;
}
Ok(&self.buffer[self.index..])
@ -260,7 +256,7 @@ impl Write for SdReader {
let block_addr = (self.byte_addr - self.index as u32) / (BLOCK_SIZE as u32);
self.sd
.write_block(block_addr, 1, &self.buffer)
.map_err(cmd_error_to_io_error)?;
.map_err(|_| Error::new(ErrorKind::Other, "Command transfer error"))?;
self.dirty = false;
}
Ok(())