use core::fmt::{Display, Formatter, Result as FmtResult};
use crate::page::PageId;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Error {
UnsupportedVersion { found: u32, expected: u32 },
LinkLengthMismatch {
expected: u32,
found: usize,
},
LinkOutOfBounds {
offset: u32,
length: u32,
capacity: usize,
},
PageOverflow {
page: PageId,
needed: usize,
capacity: usize,
},
Corrupt {
what: &'static str,
},
Encode,
Io {
kind: nagoya::io::ErrorKind,
},
UnapplicableEvent,
}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
match self {
Self::UnsupportedVersion { found, expected } => write!(
formatter,
"unsupported page format v{found}; this build requires v{expected}; recreate the store or convert it with an explicit migration tool"
),
Self::LinkLengthMismatch { expected, found } => write!(
formatter,
"a {found} byte write does not match its {expected} byte link"
),
Self::LinkOutOfBounds {
offset,
length,
capacity,
} => write!(
formatter,
"a link at {offset} running {length} bytes leaves a {capacity} byte page"
),
Self::PageOverflow {
page,
needed,
capacity,
} => write!(
formatter,
"page {page:?} needs {needed} bytes of a {capacity} byte page"
),
Self::Corrupt { what } => write!(formatter, "torn or corrupt {what}"),
Self::Encode => write!(formatter, "a value would not archive"),
Self::Io { kind } => write!(formatter, "the file failed: {kind:?}"),
Self::UnapplicableEvent => write!(
formatter,
"events of `SplitNode`, `CreateNode` or `RemoveNode` cannot be applied to a page"
),
}
}
}
impl core::error::Error for Error {}
impl From<nagoya::io::Error> for Error {
fn from(error: nagoya::io::Error) -> Self {
Self::Io { kind: error.kind() }
}
}
impl From<rkyv::rancor::Error> for Error {
fn from(_: rkyv::rancor::Error) -> Self {
Self::Encode
}
}
impl From<crate::page::PageOverflowError> for Error {
fn from(error: crate::page::PageOverflowError) -> Self {
Self::PageOverflow {
page: error.page_id,
needed: error.data_length,
capacity: error.capacity,
}
}
}
pub mod eyre {
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt::{self, Debug, Display, Formatter};
pub type Result<T, E = Report> = core::result::Result<T, E>;
pub struct Report {
inner: Box<dyn core::error::Error + Send + Sync + 'static>,
context: Vec<String>,
}
impl Report {
pub fn new<E>(error: E) -> Self
where
E: core::error::Error + Send + Sync + 'static,
{
Self {
inner: Box::new(error),
context: Vec::new(),
}
}
pub fn msg(message: impl Display) -> Self {
#[derive(Debug)]
struct MessageError(String);
impl Display for MessageError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl core::error::Error for MessageError {}
Self {
inner: Box::new(MessageError(message.to_string())),
context: Vec::new(),
}
}
pub fn wrap_err(mut self, message: impl Display) -> Self {
self.context.push(message.to_string());
self
}
pub fn is<T: core::error::Error + 'static>(&self) -> bool {
self.inner.is::<T>()
}
pub fn downcast_ref<T: core::error::Error + 'static>(&self) -> Option<&T> {
self.inner.downcast_ref::<T>()
}
}
impl Display for Report {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(last) = self.context.last() {
if f.alternate() {
write!(f, "{last}: {}", self.inner)
} else {
f.write_str(last)
}
} else {
Display::fmt(&*self.inner, f)
}
}
}
impl Debug for Report {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<E> From<E> for Report
where
E: core::error::Error + Send + Sync + 'static,
{
fn from(error: E) -> Self {
Self::new(error)
}
}
pub trait WrapErr<T, E> {
fn wrap_err<D: Display>(self, msg: D) -> Result<T>;
fn with_wrap_err<D: Display, F: FnOnce() -> D>(self, f: F) -> Result<T>;
}
impl<T, E> WrapErr<T, E> for core::result::Result<T, E>
where
E: Into<Report>,
{
fn wrap_err<D: Display>(self, msg: D) -> Result<T> {
self.map_err(|err| err.into().wrap_err(msg))
}
fn with_wrap_err<D: Display, F: FnOnce() -> D>(self, f: F) -> Result<T> {
self.map_err(|err| err.into().wrap_err(f()))
}
}
pub trait Context<T, E> {
fn context<D: Display>(self, msg: D) -> Result<T>;
fn with_context<D: Display, F: FnOnce() -> D>(self, f: F) -> Result<T>;
}
impl<T, E> Context<T, E> for core::result::Result<T, E>
where
E: Into<Report>,
{
fn context<D: Display>(self, msg: D) -> Result<T> {
self.map_err(|err| err.into().wrap_err(msg))
}
fn with_context<D: Display, F: FnOnce() -> D>(self, f: F) -> Result<T> {
self.map_err(|err| err.into().wrap_err(f()))
}
}
#[macro_export]
macro_rules! eyre {
($msg:literal $(,)?) => {
$crate::eyre::Report::msg(format_args!($msg))
};
($err:expr $(,)?) => {
$crate::eyre::Report::from($err)
};
($fmt:expr, $($arg:tt)*) => {
$crate::eyre::Report::msg(format_args!($fmt, $($arg)*))
};
}
#[macro_export]
macro_rules! bail {
($($arg:tt)*) => {
return Err($crate::eyre!($($arg)*))
};
}
#[macro_export]
macro_rules! ensure {
($cond:expr, $($arg:tt)*) => {
if !$cond {
$crate::bail!($($arg)*);
}
};
}
pub use crate::{bail, ensure, eyre};
}
pub use eyre as no_std_report;