diff --git a/examples/simple.rs b/examples/simple.rs index 5faa214..83d3c89 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,6 +1,6 @@ use std::io; -use n0_error::{StackError, e, meta}; +use n0_error::{Err, StackError, e, meta}; use self::error::CopyError; use crate::error::{InvalidArgsError, OperationError}; @@ -27,6 +27,11 @@ fn main() { print(err); } +fn _some_fn() -> Result<(), CopyError> { + // Err! macro works like e! but wraps in Err + Err!(CopyError::Read, io::Error::other("yada")) +} + fn operation() -> Result<(), OperationError> { let res = copy(); res?; @@ -67,13 +72,6 @@ pub mod error { Copy { source: CopyError }, } - #[macro_export] - macro_rules! CopyError { - ($variant:ident { $($rest:tt)* }) => { - e!(CopyError::$variant { $($rest)* }) - }; - } - #[n0_error::add_meta] #[derive(n0_error::Error)] pub enum CopyError { diff --git a/src/macros.rs b/src/macros.rs index bb2a660..bc931a4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -21,6 +21,16 @@ macro_rules! e { }; } +/// Constructs an error enum/struct value and wraps it in `Err(err)`. +/// +/// See [`e`] for supported syntax. +#[macro_export] +macro_rules! Err { + ($($tt:tt)*) => { + Err(e!($($tt)*)) + } +} + /// Propagates an error, adding formatted context. /// /// - `whatever!("msg")` returns `Err(format_err!(...))`.