sfkv/src/error.rs

69 lines
1.8 KiB
Rust

use core::{fmt, str};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error<B> {
Read(ReadError),
Write(WriteError<B>),
#[cfg(feature = "postcard-values")]
Encode(postcard::Error),
#[cfg(feature = "postcard-values")]
Decode(postcard::Error),
}
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)
}
}
/// Errors decoding the store
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadError {
/// invalid record size
InvalidSize { offset: usize, size: usize },
/// missing separator
MissingSeparator { offset: usize },
/// `str::from_utf8` error
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),
}
}
}
/// Errors when writing to the store
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteError<B> {
/// store is full even after automatic compaction
SpaceExhausted,
/// error passed from the StoreBackend's `erase()` and `program()`
/// methods
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(err) =>
write!(f, "{}", err)
}
}
}