From 0131cb1541a9783d2c042811caa2190442ca0bb3 Mon Sep 17 00:00:00 2001 From: Islam Bekbuzarov Date: Tue, 6 Aug 2024 09:30:22 +0300 Subject: [PATCH] Tuple Support (#4) * added tuple support * updated version --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2701c0a..819885f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "match_err" -version = "0.1.8" +version = "0.1.9" dependencies = [ "anyhow", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 92839bc..eaad884 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 1b01653..39fb194 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 } @@ -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 ),*, _ => {} }) ); } @@ -93,6 +102,8 @@ macro_rules! match_err { /// NotFound, /// #[error("custom: {0}")] /// Custom(String), +/// #[error("tuple error")] +/// Tuple((i32, i32)) /// } /// /// let err: Result<(), _> = Err(anyhow!(Error::NotFound)); @@ -100,21 +111,28 @@ macro_rules! match_err { /// 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 ),*, _ => {} }) ); } @@ -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") @@ -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 {