use core::{fmt, str}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Error { Read(ReadError), Write(WriteError), #[cfg(feature = "postcard-values")] Encode(postcard::Error), #[cfg(feature = "postcard-values")] Decode(postcard::Error), } impl From for Error { fn from(e: ReadError) -> Self { Error::Read(e) } } impl From> for Error { fn from(e: WriteError) -> 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 { /// store is full even after automatic compaction SpaceExhausted, /// error passed from the StoreBackend's `erase()` and `program()` /// methods Backend(B), } impl fmt::Display for WriteError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match &self { &WriteError::SpaceExhausted => write!(f, "space exhausted"), &WriteError::Backend(err) => write!(f, "{}", err) } } }