sfkv/src/error.rs

69 lines
1.8 KiB
Rust
Raw Permalink Normal View History

2020-12-11 07:28:24 +08:00
use core::{fmt, str};
#[derive(Debug, Clone, PartialEq, Eq)]
2020-12-11 07:28:24 +08:00
pub enum Error<B> {
Read(ReadError),
Write(WriteError<B>),
#[cfg(feature = "postcard-values")]
Encode(postcard::Error),
#[cfg(feature = "postcard-values")]
Decode(postcard::Error),
2020-12-11 07:28:24 +08:00
}
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 {
2020-12-12 01:11:55 +08:00
match &self {
2020-12-11 07:28:24 +08:00
&WriteError::SpaceExhausted =>
write!(f, "space exhausted"),
2020-12-12 01:11:55 +08:00
&WriteError::Backend(err) =>
write!(f, "{}", err)
2020-12-11 07:28:24 +08:00
}
}
}