Skip to content

Commit

Permalink
Assert Macros (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
blkmlk authored Aug 5, 2024
1 parent ef14557 commit 132bac3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 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.7"
version = "0.1.8"
edition = "2021"
homepage = "https://github.com/blkmlk/match_err"
documentation = "https://docs.rs/match_err"
Expand Down
68 changes: 65 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! # match_err
//!
//! Macro for quick matching errors against enum-like error types
//! Macro for quick matching and asserting errors against enum-like error types
//!
//! Helps to avoid writing long and tedious structures like:
//! ```rust
//! if let Err(e) = err {
//! if let Some(e) = e.downcast_ref::<Error>() {
//! match e {
//! ...
//! ...
//! }
//! }
//! }
Expand All @@ -17,6 +17,7 @@
//!
//! ```rust
//! use match_err::*;
//! use anyhow::anyhow;
//!
//! #[derive(thiserror::Error, Debug)]
//! enum Error {
Expand All @@ -36,7 +37,7 @@
//! ```


/// Matches an error against an enum-like error type by hiding the usage of downcast_ref method
/// Matches the error against an enum-like error type by hiding the usage of downcast_ref method
///
/// # Examples
/// ```
Expand Down Expand Up @@ -116,3 +117,64 @@ macro_rules! match_if_err {
match_if_err!($any, $ty, { $( $variant $( ( $($inner),* ) )? => $arm ),*, _ => {} })
);
}

/// Asserts the variable is an error and then asserts it against an enum-like error type by hiding the usage of downcast_ref method
/// The error is required to implement PartialEq
///
/// # Examples
/// ```
/// use match_err::*;
/// use anyhow::anyhow;
///
/// #[derive(thiserror::Error, Debug, PartialEq)]
/// enum Error {
/// #[error("not found")]
/// NotFound,
/// #[error("custom: {0}")]
/// Custom(String),
/// }
///
/// let err: Result<(), _> = Err(anyhow!(Error::Custom(String::from("internal"))));
///
/// assert_if_error!(err, Error, Custom(String::from("internal")), "invalid");
/// ```
#[macro_export]
macro_rules! assert_if_error {
($var:expr, $ty:ty, $variant:ident $( ( $inner:expr ) )? $(, $($arg:tt)+)? ) => (
if let Err(err) = $var {
assert_error!(err, $ty, $variant $( ( $inner ) )? $(, $($arg)+)? );
} else {
assert!(false, "not an error")
}
)
}

/// Asserts the error against an enum-like error type by hiding the usage of downcast_ref method
/// The error is required to implement PartialEq
///
/// # Examples
/// ```
/// use match_err::*;
/// use anyhow::anyhow;
///
/// #[derive(thiserror::Error, Debug, PartialEq)]
/// enum Error {
/// #[error("not found")]
/// NotFound,
/// #[error("custom: {0}")]
/// Custom(String),
/// }
///
/// let err = anyhow!(Error::Custom(String::from("internal")));
///
/// assert_error!(err, Error, Custom(String::from("internal")));
/// ```
#[macro_export]
macro_rules! assert_error {
($var:expr, $ty:ty, $variant:ident $( ( $inner:expr ) )? $(, $($arg:tt)+)? ) => (
match $var.downcast_ref::<$ty>() {
Some(e) if e == &<$ty>::$variant $( ( $inner ) )? => assert!(true),
_ => assert!(false $(, $($arg)+)? ),
}
)
}

0 comments on commit 132bac3

Please sign in to comment.