mirror of
https://github.com/haraldh/chainerror.git
synced 2025-01-31 00:56:41 +01:00
feat: cleanup names
- ResultTrait -> Context - ChainErrorDown -> ErrorDown - derive_err_kind -> err_kind - derive_str_context -> str_context - add `prelude::v2` with only the traits Signed-off-by: Harald Hoyer <harald@hoyer.xyz>
This commit is contained in:
parent
cb2e1509e5
commit
3ab04cca25
66
src/lib.rs
66
src/lib.rs
|
@ -12,11 +12,19 @@ pub mod prelude {
|
||||||
//! convenience prelude
|
//! convenience prelude
|
||||||
pub mod v1 {
|
pub mod v1 {
|
||||||
//! convenience prelude
|
//! convenience prelude
|
||||||
pub use super::super::ChainErrorDown as _;
|
pub use super::super::Context as _;
|
||||||
|
pub use super::super::Context as ResultTrait;
|
||||||
pub use super::super::Error as ChainError;
|
pub use super::super::Error as ChainError;
|
||||||
|
pub use super::super::ErrorDown as _;
|
||||||
|
pub use super::super::ErrorDown as ChainErrorDown;
|
||||||
pub use super::super::Result as ChainResult;
|
pub use super::super::Result as ChainResult;
|
||||||
pub use super::super::ResultTrait as _;
|
pub use crate::err_kind as derive_err_kind;
|
||||||
pub use crate::{derive_err_kind, derive_str_context};
|
pub use crate::str_context as derive_str_context;
|
||||||
|
}
|
||||||
|
pub mod v2 {
|
||||||
|
//! convenience prelude
|
||||||
|
pub use super::super::Context as _;
|
||||||
|
pub use super::super::ErrorDown as _;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +63,8 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use chainerror::prelude::v1::*;
|
/// use chainerror::Context as _;
|
||||||
|
/// use chainerror::ErrorDown as _;
|
||||||
/// use std::error::Error;
|
/// use std::error::Error;
|
||||||
/// use std::io;
|
/// use std::io;
|
||||||
///
|
///
|
||||||
|
@ -64,7 +73,7 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// derive_str_context!(Func2Error);
|
/// chainerror::str_context!(Func2Error);
|
||||||
///
|
///
|
||||||
/// fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
|
/// fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
/// let filename = "foo.txt";
|
/// let filename = "foo.txt";
|
||||||
|
@ -72,7 +81,7 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// derive_str_context!(Func1Error);
|
/// chainerror::str_context!(Func1Error);
|
||||||
///
|
///
|
||||||
/// fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
/// fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
/// func2().context(Func1Error("func1 error".into()))?;
|
/// func2().context(Func1Error("func1 error".into()))?;
|
||||||
|
@ -107,9 +116,8 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use chainerror::prelude::v1::*;
|
/// # chainerror::str_context!(FooError);
|
||||||
/// # derive_str_context!(FooError);
|
/// # let err = chainerror::Error::new(String::new(), None, None);
|
||||||
/// # let err = ChainError::new(String::new(), None, None);
|
|
||||||
/// // Instead of writing
|
/// // Instead of writing
|
||||||
/// err.find_cause::<chainerror::Error<FooError>>();
|
/// err.find_cause::<chainerror::Error<FooError>>();
|
||||||
///
|
///
|
||||||
|
@ -130,9 +138,8 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use chainerror::prelude::v1::*;
|
/// # chainerror::str_context!(FooErrorKind);
|
||||||
/// # derive_str_context!(FooErrorKind);
|
/// # let err = chainerror::Error::new(String::new(), None, None);
|
||||||
/// # let err = ChainError::new(String::new(), None, None);
|
|
||||||
/// // Instead of writing
|
/// // Instead of writing
|
||||||
/// err.find_cause::<chainerror::Error<FooErrorKind>>();
|
/// err.find_cause::<chainerror::Error<FooErrorKind>>();
|
||||||
/// // and/or
|
/// // and/or
|
||||||
|
@ -159,7 +166,7 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use chainerror::prelude::v1::*;
|
/// use chainerror::Context as _;
|
||||||
/// use std::error::Error;
|
/// use std::error::Error;
|
||||||
/// use std::io;
|
/// use std::io;
|
||||||
///
|
///
|
||||||
|
@ -168,7 +175,7 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// derive_str_context!(Func2Error);
|
/// chainerror::str_context!(Func2Error);
|
||||||
///
|
///
|
||||||
/// fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
|
/// fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
/// let filename = "foo.txt";
|
/// let filename = "foo.txt";
|
||||||
|
@ -225,7 +232,7 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience methods for `Result<>` to turn the error into a decorated [`Error`](Error)
|
/// Convenience methods for `Result<>` to turn the error into a decorated [`Error`](Error)
|
||||||
pub trait ResultTrait<O, E: Into<Box<dyn StdError + 'static + Send + Sync>>> {
|
pub trait Context<O, E: Into<Box<dyn StdError + 'static + Send + Sync>>> {
|
||||||
/// Decorate the error with a `kind` of type `T` and the source `Location`
|
/// Decorate the error with a `kind` of type `T` and the source `Location`
|
||||||
fn context<T: 'static + Display + Debug>(self, kind: T) -> std::result::Result<O, Error<T>>;
|
fn context<T: 'static + Display + Debug>(self, kind: T) -> std::result::Result<O, Error<T>>;
|
||||||
|
|
||||||
|
@ -236,7 +243,7 @@ pub trait ResultTrait<O, E: Into<Box<dyn StdError + 'static + Send + Sync>>> {
|
||||||
) -> std::result::Result<O, Error<T>>;
|
) -> std::result::Result<O, Error<T>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O, E: Into<Box<dyn StdError + 'static + Send + Sync>>> ResultTrait<O, E>
|
impl<O, E: Into<Box<dyn StdError + 'static + Send + Sync>>> Context<O, E>
|
||||||
for std::result::Result<O, E>
|
for std::result::Result<O, E>
|
||||||
{
|
{
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
|
@ -298,7 +305,7 @@ impl<T: 'static + Display + Debug> std::ops::Deref for Error<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience trait to hide the [`Error<T>`](Error) implementation internals
|
/// Convenience trait to hide the [`Error<T>`](Error) implementation internals
|
||||||
pub trait ChainErrorDown {
|
pub trait ErrorDown {
|
||||||
/// Test if of type `Error<T>`
|
/// Test if of type `Error<T>`
|
||||||
fn is_chain<T: 'static + Display + Debug>(&self) -> bool;
|
fn is_chain<T: 'static + Display + Debug>(&self) -> bool;
|
||||||
/// Downcast to a reference of `Error<T>`
|
/// Downcast to a reference of `Error<T>`
|
||||||
|
@ -311,7 +318,7 @@ pub trait ChainErrorDown {
|
||||||
fn downcast_inner_mut<T: 'static + StdError>(&mut self) -> Option<&mut T>;
|
fn downcast_inner_mut<T: 'static + StdError>(&mut self) -> Option<&mut T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U: 'static + Display + Debug> ChainErrorDown for Error<U> {
|
impl<U: 'static + Display + Debug> ErrorDown for Error<U> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
||||||
TypeId::of::<T>() == TypeId::of::<U>()
|
TypeId::of::<T>() == TypeId::of::<U>()
|
||||||
|
@ -369,7 +376,7 @@ impl<U: 'static + Display + Debug> ChainErrorDown for Error<U> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChainErrorDown for dyn StdError + 'static {
|
impl ErrorDown for dyn StdError + 'static {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
||||||
self.is::<Error<T>>()
|
self.is::<Error<T>>()
|
||||||
|
@ -402,7 +409,7 @@ impl ChainErrorDown for dyn StdError + 'static {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChainErrorDown for dyn StdError + 'static + Send {
|
impl ErrorDown for dyn StdError + 'static + Send {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
||||||
self.is::<Error<T>>()
|
self.is::<Error<T>>()
|
||||||
|
@ -435,7 +442,7 @@ impl ChainErrorDown for dyn StdError + 'static + Send {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChainErrorDown for dyn StdError + 'static + Send + Sync {
|
impl ErrorDown for dyn StdError + 'static + Send + Sync {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
fn is_chain<T: 'static + Display + Debug>(&self) -> bool {
|
||||||
self.is::<Error<T>>()
|
self.is::<Error<T>>()
|
||||||
|
@ -549,7 +556,8 @@ where
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use chainerror::prelude::v1::*;
|
/// # use chainerror::Context as _;
|
||||||
|
/// # use chainerror::ErrorDown as _;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
/// # use std::result::Result;
|
/// # use std::result::Result;
|
||||||
|
@ -557,7 +565,7 @@ where
|
||||||
/// # Err(io::Error::from(io::ErrorKind::NotFound))?;
|
/// # Err(io::Error::from(io::ErrorKind::NotFound))?;
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// derive_str_context!(Func2Error);
|
/// chainerror::str_context!(Func2Error);
|
||||||
///
|
///
|
||||||
/// fn func2() -> chainerror::Result<(), Func2Error> {
|
/// fn func2() -> chainerror::Result<(), Func2Error> {
|
||||||
/// let filename = "foo.txt";
|
/// let filename = "foo.txt";
|
||||||
|
@ -565,7 +573,7 @@ where
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// derive_str_context!(Func1Error);
|
/// chainerror::str_context!(Func1Error);
|
||||||
///
|
///
|
||||||
/// fn func1() -> Result<(), Box<dyn Error>> {
|
/// fn func1() -> Result<(), Box<dyn Error>> {
|
||||||
/// func2().context(Func1Error("func1 error".into()))?;
|
/// func2().context(Func1Error("func1 error".into()))?;
|
||||||
|
@ -573,7 +581,7 @@ where
|
||||||
/// }
|
/// }
|
||||||
/// # if let Err(e) = func1() {
|
/// # if let Err(e) = func1() {
|
||||||
/// # if let Some(f1err) = e.downcast_chain_ref::<Func1Error>() {
|
/// # if let Some(f1err) = e.downcast_chain_ref::<Func1Error>() {
|
||||||
/// # assert!(f1err.find_cause::<ChainError<Func2Error>>().is_some());
|
/// # assert!(f1err.find_cause::<chainerror::Error<Func2Error>>().is_some());
|
||||||
/// # assert!(f1err.find_chain_cause::<Func2Error>().is_some());
|
/// # assert!(f1err.find_chain_cause::<Func2Error>().is_some());
|
||||||
/// # } else {
|
/// # } else {
|
||||||
/// # panic!();
|
/// # panic!();
|
||||||
|
@ -583,7 +591,7 @@ where
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! derive_str_context {
|
macro_rules! str_context {
|
||||||
($e:ident) => {
|
($e:ident) => {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct $e(pub String);
|
pub struct $e(pub String);
|
||||||
|
@ -612,7 +620,7 @@ macro_rules! derive_str_context {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use chainerror::prelude::v1::*;
|
/// use chainerror::Context as _;
|
||||||
/// use std::io;
|
/// use std::io;
|
||||||
///
|
///
|
||||||
/// fn do_some_io(_f: &str) -> std::result::Result<(), io::Error> {
|
/// fn do_some_io(_f: &str) -> std::result::Result<(), io::Error> {
|
||||||
|
@ -626,7 +634,7 @@ macro_rules! derive_str_context {
|
||||||
/// Unknown,
|
/// Unknown,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// derive_err_kind!(Error, ErrorKind);
|
/// chainerror::err_kind!(Error, ErrorKind);
|
||||||
///
|
///
|
||||||
/// impl std::fmt::Display for ErrorKind {
|
/// impl std::fmt::Display for ErrorKind {
|
||||||
/// fn fmt(&self, f: &mut ::std::fmt::Formatter) -> std::fmt::Result {
|
/// fn fmt(&self, f: &mut ::std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
@ -666,7 +674,7 @@ macro_rules! derive_str_context {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! derive_err_kind {
|
macro_rules! err_kind {
|
||||||
($e:ident, $k:ident) => {
|
($e:ident, $k:ident) => {
|
||||||
pub struct $e($crate::Error<$k>);
|
pub struct $e($crate::Error<$k>);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue