Skip to content

Commit e2b132a

Browse files
authored
Merge pull request #4 from n0-computer/Frando/Err-macro
feat: add Err macro
2 parents c8caee0 + 73eb1ea commit e2b132a

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

examples/simple.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io;
22

3-
use n0_error::{StackError, e, meta};
3+
use n0_error::{Err, StackError, e, meta};
44

55
use self::error::CopyError;
66
use crate::error::{InvalidArgsError, OperationError};
@@ -27,6 +27,11 @@ fn main() {
2727
print(err);
2828
}
2929

30+
fn _some_fn() -> Result<(), CopyError> {
31+
// Err! macro works like e! but wraps in Err
32+
Err!(CopyError::Read, io::Error::other("yada"))
33+
}
34+
3035
fn operation() -> Result<(), OperationError> {
3136
let res = copy();
3237
res?;
@@ -67,13 +72,6 @@ pub mod error {
6772
Copy { source: CopyError },
6873
}
6974

70-
#[macro_export]
71-
macro_rules! CopyError {
72-
($variant:ident { $($rest:tt)* }) => {
73-
e!(CopyError::$variant { $($rest)* })
74-
};
75-
}
76-
7775
#[n0_error::add_meta]
7876
#[derive(n0_error::Error)]
7977
pub enum CopyError {

src/macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ macro_rules! e {
2121
};
2222
}
2323

24+
/// Constructs an error enum/struct value and wraps it in `Err(err)`.
25+
///
26+
/// See [`e`] for supported syntax.
27+
#[macro_export]
28+
macro_rules! Err {
29+
($($tt:tt)*) => {
30+
Err(e!($($tt)*))
31+
}
32+
}
33+
2434
/// Propagates an error, adding formatted context.
2535
///
2636
/// - `whatever!("msg")` returns `Err(format_err!(...))`.

0 commit comments

Comments
 (0)