1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#![doc = include_str!("../README.md")]
#![no_std]

/// Assert that the given expression matches the given pattern, and introduce
/// any bindings from the pattern into the surrounding scope.
///
/// If the assertion fails, the expression before the comma is debug-printed as
/// part of the panic message.
#[macro_export]
macro_rules! assert_matches {
    ($e:expr, $pat:pat) => {
        let value = $e;
        let $pat = value else {
            ::core::panic!(
                "match assertion failed\n pattern: `{}`\n   value: `{:?}`",
                ::core::stringify!($pat), value,
            );
        };
    };
    ($e:expr, $pat:pat, $($arg:tt)*) => {
        let value = $e;
        let $pat = value else {
            ::core::panic!(
                "match assertion failed: {}\n pattern: `{}`\n   value: `{:?}`",
                ::core::format_args!($($arg)*), ::core::stringify!($pat), value,
            );
        };
    };
}

/// Alternative form of [`assert_matches!`] where the pattern comes first.
///
/// If the assertion fails, the expression after the `=` is debug-printed as
/// part of the panic message.
///
/// # Example
///
/// ```
/// use assert_matches2::assert_let;
/// use serde_json::{json, Value as JsonValue};
///
/// let val = json!({ "field": [1, 2, 3] });
/// assert_let!(JsonValue::Object(obj) = val);
/// assert_eq!(obj.len(), 1);
/// assert_let!(Some((field_name, JsonValue::Array(arr))) = obj.into_iter().next());
/// assert_eq!(field_name, "field");
/// assert_eq!(arr, [1, 2, 3]);
/// ```
#[macro_export]
macro_rules! assert_let {
    ($pat:pat = $e:expr) => {
        $crate::assert_matches!($e, $pat);
    };
    ($pat:pat = $e:expr, $($arg:tt)*) => {
        $crate::assert_matches!($e, $pat, $($arg)*);
    }
}