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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/// Asserts that expression returns [`Some(T)`] variant.
///
/// ## Uses
///
/// Assertions are always checked in both debug and release builds, and cannot be disabled.
/// See [`debug_assert_some!`] for assertions that are not enabled in release builds by default.
///
/// ## Custom messages
///
/// This macro has a second form, where a custom panic message can be provided
/// with or without arguments for formatting. See [`std::fmt`] for syntax for this form.
///
/// ## Examples
///
/// ```rust
/// # #[macro_use] extern crate claim;
/// # fn main() {
/// let maybe = Some(42);
///
/// assert_some!(maybe);
///
/// // With custom messages
/// assert_some!(maybe, "Found it at {:?}", maybe);
/// # }
/// ```
///
/// `None` variant will cause panic:
///
/// ```rust,should_panic
/// # #[macro_use] extern crate claim;
/// # fn main() {
/// let maybe = None;
///
/// assert_some!(maybe);  // Will panic
/// # }
/// ```
///
/// [`Some(T)`]: https://doc.rust-lang.org/core/option/enum.Option.html#variant.Some
/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
/// [`debug_assert_some!`]: ./macro.debug_assert_some.html
#[macro_export]
macro_rules! assert_some {
    ($cond:expr,) => {
        $crate::assert_some!($cond,);
    };
    ($cond:expr) => {
        match $cond {
            Some(t) => t,
            None => {
                panic!("assertion failed, expected Some(..), got None");
            }
        }
    };
    ($cond:expr, $($arg:tt)+) => {
        match $cond {
            Some(t) => t,
            None => {
                panic!("assertion failed, expected Some(..), got None: {}", format_args!($($arg)+));
            }
        }
    };
}

/// Asserts that expression returns [`Some(T)`] variant in runtime.
///
/// Like [`assert_some!`], this macro also has a second version,
/// where a custom panic message can be provided.
///
/// ## Uses
///
/// See [`debug_assert!`] documentation for possible use cases.
/// The same applies to this macro.
///
/// [`Some(T)`]: https://doc.rust-lang.org/core/option/enum.Option.html#variant.Some
/// [`debug_assert!`]: https://doc.rust-lang.org/std/macro.debug_assert.html
/// [`assert_some!`]: ./macro.assert_some.html
#[macro_export]
macro_rules! debug_assert_some {
    ($($arg:tt)*) => (if core::cfg!(debug_assertions) { $crate::assert_some!($($arg)*); })
}

#[cfg(test)]
#[cfg(not(has_private_in_public_issue))]
mod tests {
    #[test]
    #[should_panic(expected = "assertion failed, expected Some(..), got None")]
    fn default_panic_message() {
        let maybe: Option<i32> = None;
        let _ = assert_some!(maybe);
    }

    #[test]
    #[should_panic(
        expected = "assertion failed, expected Some(..), got None: we are checking if there was an error with None"
    )]
    fn custom_panic_message() {
        let maybe: Option<i32> = None;
        let _ = assert_some!(
            maybe,
            "we are checking if there was an error with {:?}",
            maybe
        );
    }

    #[test]
    fn does_not_require_some_debug() {
        enum Foo {
            Bar,
        }

        let res: Option<Foo> = Some(Foo::Bar);
        let _ = assert_some!(res);
    }
}