use core::fmt;
use alloc::boxed::Box;
#[derive(Debug)]
pub enum Error {
Merge(module::merge::Error),
Other(Box<dyn core::error::Error + Send + Sync + 'static>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Merge(x) => fmt::Display::fmt(x, f),
Self::Other(x) => {
if f.alternate() {
f.write_str("other error")
} else {
fmt::Display::fmt(x, f)
}
}
}
}
}
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Merge(x) => Some(x),
Self::Other(x) => Some(&**x),
}
}
}
impl Error {
#[must_use]
pub fn other<E>(error: E) -> Self
where
E: core::error::Error + Send + Sync + 'static,
{
Self::Other(Box::new(error))
}
#[inline]
#[must_use]
pub fn is_merge(&self) -> bool {
matches!(self, Self::Merge(_))
}
#[must_use]
pub fn as_merge(&self) -> Option<&module::merge::Error> {
match self {
Self::Merge(x) => Some(x),
_ => None,
}
}
#[must_use]
pub fn as_merge_mut(&mut self) -> Option<&mut module::merge::Error> {
match self {
Self::Merge(x) => Some(x),
_ => None,
}
}
#[inline]
#[must_use]
pub fn is_other(&self) -> bool {
matches!(self, Self::Other(_))
}
#[must_use]
pub fn as_other(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Other(x) => Some(&**x),
_ => None,
}
}
#[must_use]
pub fn as_other_mut(&mut self) -> Option<&mut (dyn core::error::Error + 'static)> {
match self {
Self::Other(x) => Some(&mut **x),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Error>();
}
}