2020-12-11 07:28:24 +08:00
|
|
|
use core::{fmt, str};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum Error<B> {
|
|
|
|
Read(ReadError),
|
|
|
|
Write(WriteError<B>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B> From<ReadError> for Error<B> {
|
|
|
|
fn from(e: ReadError) -> Self {
|
|
|
|
Error::Read(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B> From<WriteError<B>> for Error<B> {
|
|
|
|
fn from(e: WriteError<B>) -> Self {
|
|
|
|
Error::Write(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:06:20 +08:00
|
|
|
/// Errors decoding the store
|
2020-12-11 07:28:24 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum ReadError {
|
2020-12-11 08:06:20 +08:00
|
|
|
/// invalid record size
|
2020-12-11 07:28:24 +08:00
|
|
|
InvalidSize { offset: usize, size: usize },
|
2020-12-11 08:06:20 +08:00
|
|
|
/// missing separator
|
2020-12-11 07:28:24 +08:00
|
|
|
MissingSeparator { offset: usize },
|
2020-12-11 08:06:20 +08:00
|
|
|
/// `str::from_utf8` error
|
2020-12-11 07:28:24 +08:00
|
|
|
Utf8Error(str::Utf8Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ReadError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
&ReadError::InvalidSize { offset, size } =>
|
|
|
|
write!(f, "invalid record size {} at offset {}", size, offset),
|
|
|
|
&ReadError::MissingSeparator { offset } =>
|
|
|
|
write!(f, "missing separator at offset {}", offset),
|
|
|
|
&ReadError::Utf8Error(err) =>
|
|
|
|
write!(f, "{}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:06:20 +08:00
|
|
|
/// Errors when writing to the store
|
2020-12-11 07:28:24 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum WriteError<B> {
|
2020-12-11 08:06:20 +08:00
|
|
|
/// store is full even after automatic compaction
|
2020-12-11 07:28:24 +08:00
|
|
|
SpaceExhausted,
|
2020-12-11 08:06:20 +08:00
|
|
|
/// error passed from the StoreBackend's `erase()` and `program()`
|
|
|
|
/// methods
|
2020-12-11 07:28:24 +08:00
|
|
|
Backend(B),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: fmt::Display> fmt::Display for WriteError<B> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
&WriteError::SpaceExhausted =>
|
|
|
|
write!(f, "space exhausted"),
|
|
|
|
&WriteError::Backend(ref e) =>
|
|
|
|
e.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|