Skip to content

Commit

Permalink
implemented core::error::Error trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kaganege committed Jan 25, 2025
1 parent 4baa75b commit 9d557bf
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmt = { version = "0.3.2", optional = true }
log = { version = "0.4.17", optional = true }
heapless = "0.8"
embassy-sync = "0.6.0"
thiserror = { version = "2.0.11", default-features = false }

[dev-dependencies]
ekv = { path = ".", features = ["std", "log"]}
Expand Down
117 changes: 94 additions & 23 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
use crate::file::SearchSeekError;
use crate::page::ReadError as PageReadError;
use thiserror::Error;

/// Error returned by [`ReadTransaction::read_all`](crate::ReadTransaction::read_all), [`ReadTransaction::read_range`](crate::ReadTransaction::read_range).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error<E> {
pub enum Error<E>
where
E: core::error::Error,
{
/// Database is corrupted, or not formatted yet.
#[error("Database is corrupted, or not formatted yet.")]
Corrupted,
/// Some operation on the underlying [`Flash`](crate::flash::Flash) failed.
#[error("Some operation on the underlying Flash failed: {0}")]
Flash(E),
}

impl<E> From<FormatError<E>> for Error<E> {
impl<E> From<FormatError<E>> for Error<E>
where
E: core::error::Error,
{
fn from(value: FormatError<E>) -> Self {
match value {
FormatError::Flash(e) => Self::Flash(e),
Expand All @@ -20,24 +29,36 @@ impl<E> From<FormatError<E>> for Error<E> {
}

/// Error returned by [`Database::format`](crate::Database::format).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FormatError<E> {
pub enum FormatError<E>
where
E: core::error::Error,
{
/// Some operation on the underlying [`Flash`](crate::flash::Flash) failed.
#[error("Some operation on the underlying Flash failed: {0}")]
Flash(E),
}

/// Error returned by [`Database::mount`](crate::Database::mount).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MountError<E> {
pub enum MountError<E>
where
E: core::error::Error,
{
/// Database is corrupted, or not formatted yet.
#[error("Database is corrupted, or not formatted yet.")]
Corrupted,
/// Some operation on the underlying [`Flash`](crate::flash::Flash) failed.
#[error("Some operation on the underlying Flash failed: {0}")]
Flash(E),
}

impl<E> From<Error<E>> for MountError<E> {
impl<E> From<Error<E>> for MountError<E>
where
E: core::error::Error,
{
fn from(e: Error<E>) -> Self {
match e {
Error::Flash(e) => Self::Flash(e),
Expand All @@ -47,22 +68,33 @@ impl<E> From<Error<E>> for MountError<E> {
}

/// Error returned by [`ReadTransaction::read`](crate::ReadTransaction::read).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ReadError<E> {
pub enum ReadError<E>
where
E: core::error::Error,
{
/// The requested key is not present in the database.
#[error("The requested key is not present in the database.")]
KeyNotFound,
/// The requested key is larger than [`MAX_KEY_SIZE`](crate::config::MAX_KEY_SIZE)
#[error("The requested key is larger than max key size.")]
KeyTooBig,
/// The requested key was found, but the value was larger than the provided buffer.
#[error("The requested key was found, but the value was larger than the provided buffer.")]
BufferTooSmall,
/// Database is corrupted, or not formatted yet.
#[error("Database is corrupted, or not formatted yet.")]
Corrupted,
/// Some operation on the underlying [`Flash`](crate::flash::Flash) failed.
#[error("Some operation on the underlying Flash failed: {0}")]
Flash(E),
}

impl<E> From<Error<E>> for ReadError<E> {
impl<E> From<Error<E>> for ReadError<E>
where
E: core::error::Error,
{
fn from(e: Error<E>) -> Self {
match e {
Error::Flash(e) => Self::Flash(e),
Expand All @@ -72,29 +104,42 @@ impl<E> From<Error<E>> for ReadError<E> {
}

/// Error returned by [`WriteTransaction::write`](crate::WriteTransaction::write).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WriteError<E> {
pub enum WriteError<E>
where
E: core::error::Error,
{
/// The key is not lexicographically larger than the last written key in this transaction.
///
/// Writes in a transaction must be sorted in ascending order, and you may not write the same
/// key twice.
#[error("The key is not lexicographically larger than the last written key in this transaction.")]
NotSorted,
/// The key is larger than [`MAX_KEY_SIZE`](crate::config::MAX_KEY_SIZE)
#[error("The key is larger than max key size.")]
KeyTooBig,
/// The value is larger than [`MAX_VALUE_SIZE`](crate::config::MAX_VALUE_SIZE)
#[error("The value is larger than max value size.")]
ValueTooBig,
/// Transaction is canceled. See [`WriteTransaction`](crate::WriteTransaction) for details.
#[error("Transaction is canceled.")]
TransactionCanceled,
/// The database storage is full.
#[error("The database storage is full.")]
Full,
/// Database is corrupted, or not formatted yet.
#[error("Database is corrupted, or not formatted yet.")]
Corrupted,
/// Some operation on the underlying [`Flash`](crate::flash::Flash) failed.
#[error("Some operation on the underlying Flash failed: {0}")]
Flash(E),
}

impl<E> From<Error<E>> for WriteError<E> {
impl<E> From<Error<E>> for WriteError<E>
where
E: core::error::Error,
{
fn from(e: Error<E>) -> Self {
match e {
Error::Flash(e) => Self::Flash(e),
Expand All @@ -104,18 +149,24 @@ impl<E> From<Error<E>> for WriteError<E> {
}

/// Error returned by [`WriteTransaction::commit`](crate::WriteTransaction::commit).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CommitError<E> {
/// Transaction is canceled. See [`WriteTransaction`](crate::WriteTransaction) for details.
#[error("Transaction is canceled.")]
TransactionCanceled,
/// Database is corrupted, or not formatted yet.
#[error("Database is corrupted, or not formatted yet.")]
Corrupted,
/// Some operation on the underlying [`Flash`](crate::flash::Flash) failed.
#[error("Some operation on the underlying Flash failed: {0}")]
Flash(E),
}

impl<E> From<Error<E>> for CommitError<E> {
impl<E> From<Error<E>> for CommitError<E>
where
E: core::error::Error,
{
fn from(e: Error<E>) -> Self {
match e {
Error::Flash(e) => Self::Flash(e),
Expand All @@ -125,20 +176,27 @@ impl<E> From<Error<E>> for CommitError<E> {
}

/// Error returned by [`Cursor::next`](crate::Cursor::next).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CursorError<E> {
/// The provided buffer for the key was too small.
#[error("The provided buffer for the key was too small.")]
KeyBufferTooSmall,
/// The provided buffer for the value was too small.
#[error("The provided buffer for the value was too small.")]
ValueBufferTooSmall,
/// Database is corrupted, or not formatted yet.
#[error("Database is corrupted, or not formatted yet.")]
Corrupted,
/// Some operation on the underlying [`Flash`](crate::flash::Flash) failed.
#[error("Some operation on the underlying Flash failed: {0}")]
Flash(E),
}

impl<E> From<Error<E>> for CursorError<E> {
impl<E> From<Error<E>> for CursorError<E>
where
E: core::error::Error,
{
fn from(e: Error<E>) -> Self {
match e {
Error::Flash(e) => Self::Flash(e),
Expand All @@ -148,11 +206,15 @@ impl<E> From<Error<E>> for CursorError<E> {
}

/// Database is corrupted, or not formatted yet.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[error("Database is corrupted, or not formatted yet.")]
pub(crate) struct CorruptedError;

impl<E> From<CorruptedError> for Error<E> {
impl<E> From<CorruptedError> for Error<E>
where
E: core::error::Error,
{
fn from(_: CorruptedError) -> Self {
Self::Corrupted
}
Expand All @@ -164,13 +226,19 @@ impl<E> From<CorruptedError> for PageReadError<E> {
}
}

impl<E> From<CorruptedError> for ReadError<E> {
impl<E> From<CorruptedError> for ReadError<E>
where
E: core::error::Error,
{
fn from(_: CorruptedError) -> Self {
Self::Corrupted
}
}

impl<E> From<CorruptedError> for WriteError<E> {
impl<E> From<CorruptedError> for WriteError<E>
where
E: core::error::Error,
{
fn from(_: CorruptedError) -> Self {
Self::Corrupted
}
Expand All @@ -188,7 +256,10 @@ impl<E> From<CorruptedError> for CursorError<E> {
}
}

pub(crate) fn no_eof<T>(e: PageReadError<T>) -> Error<T> {
pub(crate) fn no_eof<T>(e: PageReadError<T>) -> Error<T>
where
T: core::error::Error,
{
match e {
PageReadError::Corrupted => Error::Corrupted,
#[cfg(not(feature = "_panic-on-corrupted"))]
Expand Down
10 changes: 8 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,10 @@ pub enum SearchSeekError<E> {
Corrupted,
}

impl<E> From<Error<E>> for SearchSeekError<E> {
impl<E> From<Error<E>> for SearchSeekError<E>
where
E: core::error::Error,
{
fn from(e: Error<E>) -> Self {
match e {
Error::Flash(e) => Self::Flash(e),
Expand All @@ -1015,7 +1018,10 @@ pub enum WriteError<E> {
Corrupted,
}

impl<E> From<Error<E>> for WriteError<E> {
impl<E> From<Error<E>> for WriteError<E>
where
E: core::error::Error,
{
fn from(e: Error<E>) -> Self {
match e {
Error::Flash(e) => Self::Flash(e),
Expand Down
4 changes: 1 addition & 3 deletions src/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//!
//! You must implement this trait for your flash storage to use it with `ekv`.
use core::fmt::Debug;

#[cfg(feature = "std")]
use crate::config::*;
pub use crate::types::PageID;
Expand All @@ -14,7 +12,7 @@ pub use crate::types::PageID;
/// Flash storage trait
pub trait Flash {
/// Error type for the flash operations.
type Error: Debug;
type Error: core::error::Error;

/// Get the page count of the flash storage.
fn page_count(&self) -> usize;
Expand Down
5 changes: 4 additions & 1 deletion src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ pub enum ReadError<E> {
Corrupted,
}

impl<E> From<Error<E>> for ReadError<E> {
impl<E> From<Error<E>> for ReadError<E>
where
E: core::error::Error,
{
fn from(err: Error<E>) -> Self {
match err {
Error::Flash(e) => Self::Flash(e),
Expand Down

0 comments on commit 9d557bf

Please sign in to comment.