Skip to content

Commit

Permalink
Tuple Support (#4)
Browse files Browse the repository at this point in the history
* added tuple support

* updated version
  • Loading branch information
blkmlk authored Aug 6, 2024
1 parent fa1a9e5 commit 0131cb1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "match_err"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
homepage = "https://github.com/blkmlk/match_err"
documentation = "https://docs.rs/match_err"
Expand Down
56 changes: 41 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,32 @@
/// NotFound,
/// #[error("custom: {0}")]
/// Custom(String),
/// #[error("tuple error")]
/// Tuple((i32, i32))
/// }
///
/// let err = anyhow!(Error::NotFound);
///
/// match_err!(err, Error, {
/// NotFound => assert!(true),
/// Custom(msg) => assert!(false),
/// Custom(ref msg) => assert!(false),
/// Tuple((x, y)) => assert!(x == y),
/// _ => assert!(false)
/// })
/// });
///
/// match_err!(err, Error, {
/// NotFound => assert!(true),
/// Custom(msg) => assert!(false),
/// Tuple((x, y)) => assert!(x == y)
/// });
/// ```
#[macro_export]
macro_rules! match_err {
( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:ident),* ) )? => $arm:expr ),*, _ => $default:expr } ) => (
( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:tt)+ ))? => $arm:expr ),*, _ => $default:expr } ) => (
if let Some(e) = $any.downcast_ref::<$ty>() {
match e {
$(
$ty::$variant $( ( $(ref $inner),* ) )? => $arm,
$ty::$variant $( ( $($inner)+ ) )? => $arm,
)*
_ => $default
}
Expand All @@ -75,8 +84,8 @@ macro_rules! match_err {
}
);

( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:ident),* ) )? => $arm:expr ),* $(,)? }) => (
match_err!($any, $ty, { $( $variant $( ( $($inner),* ) )? => $arm ),*, _ => {} })
( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:tt)+ ) )? => $arm:expr ),* $(,)? }) => (
match_err!($any, $ty, { $( $variant $( ( $($inner)+ ) )? => $arm ),*, _ => {} })
);
}

Expand All @@ -93,28 +102,37 @@ macro_rules! match_err {
/// NotFound,
/// #[error("custom: {0}")]
/// Custom(String),
/// #[error("tuple error")]
/// Tuple((i32, i32))
/// }
///
/// let err: Result<(), _> = Err(anyhow!(Error::NotFound));
///
/// match_if_err!(err, Error, {
/// NotFound => assert!(true),
/// Custom(msg) => assert!(false),
/// Tuple((x, y)) => assert!(x == y),
/// _ => assert!(false)
/// })
/// });
///
/// match_if_err!(err, Error, {
/// NotFound => assert!(true),
/// Custom(msg) => assert!(false),
/// Tuple((x, y)) => assert!(x == y),
/// });
/// ```
#[macro_export]
macro_rules! match_if_err {
( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:ident),* ) )? => $arm:expr ),*, _ => $default:expr } ) => (
if let Err(e) = $any {
match_err!(e, $ty, { $( $variant $( ( $($inner),* ) )? => $arm ),*, _ => $default })
( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:tt)+ ) )? => $arm:expr ),*, _ => $default:expr } ) => (
if let Err(ref e) = $any {
match_err!(e, $ty, { $( $variant $( ( $($inner)+ ) )? => $arm ),*, _ => $default })
} else {
$default
}
);

( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:ident),* ) )? => $arm:expr ),* $(,)? }) => (
match_if_err!($any, $ty, { $( $variant $( ( $($inner),* ) )? => $arm ),*, _ => {} })
( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:tt)+ ) )? => $arm:expr ),* $(,)? }) => (
match_if_err!($any, $ty, { $( $variant $( ( $($inner)+ ) )? => $arm ),*, _ => {} })
);
}

Expand All @@ -132,16 +150,20 @@ macro_rules! match_if_err {
/// NotFound,
/// #[error("custom: {0}")]
/// Custom(String),
/// #[error("tuple error")]
/// Tuple((i32, i32))
/// }
///
/// let err: Result<(), _> = Err(anyhow!(Error::Custom(String::from("internal"))));
/// assert_if_error!(err, Error, Custom(String::from("internal")));
///
/// assert_if_error!(err, Error, Custom(String::from("internal")), "invalid");
/// let err: Result<(), _> = Err(anyhow!(Error::Tuple((1,1))));
/// assert_if_error!(err, Error, Tuple((1,1)), "error message");
/// ```
#[macro_export]
macro_rules! assert_if_error {
($var:expr, $ty:ty, $variant:ident $( ( $inner:expr ) )? $(, $($arg:tt)+)? ) => (
if let Err(err) = $var {
if let Err(ref err) = $var {
assert_error!(err, $ty, $variant $( ( $inner ) )? $(, $($arg)+)? );
} else {
assert!(false, "not an error")
Expand All @@ -163,11 +185,15 @@ macro_rules! assert_if_error {
/// NotFound,
/// #[error("custom: {0}")]
/// Custom(String),
/// #[error("tuple error")]
/// Tuple((i32, i32))
/// }
///
/// let err = anyhow!(Error::Custom(String::from("internal")));
///
/// assert_error!(err, Error, Custom(String::from("internal")));
///
/// let err = anyhow!(Error::Tuple((1,1)));
/// assert_error!(err, Error, Tuple((1,1)), "error message");
/// ```
#[macro_export]
macro_rules! assert_error {
Expand Down

0 comments on commit 0131cb1

Please sign in to comment.