lazy_panic/
lib.rs

1//! Provides lazy utilities to lazily set custom panic hook.
2
3///Formats ```PanicInfo``` payload into ```String```
4///
5///# Arguments
6///
7///* ```payload``` - ```PanicInfo``` payload message.
8///* ```p_type``` - Multiple number of types which payload can be. If not among these types then it
9///is formatted as ```{:?}```
10///
11///# Return
12///
13///Result of `write!` macro
14#[macro_export]
15macro_rules! write_payload {
16    ($writer:expr, $payload:expr, types: [$($p_type:ty),+]) => {{
17        $(
18            if let Some(result) = $payload.downcast_ref::<$p_type>() {
19                write!($writer, "{}", result)
20            }
21         )else+
22         else {
23             write!($writer, "{:?}", $payload)
24         }
25    }}
26}
27
28///Sets custom printer for panic.
29///
30///# Arguments
31///
32///* ```Config``` - panic formatter that implements [PanicFormat](formatter/trait.PanicFormat.html)
33#[macro_export]
34macro_rules! set_panic_message {
35    ($config:ty) => {{
36        use std::panic;
37        use $crate::formatter::PanicFormat;
38        type Printer = $config;
39        panic::set_hook(Box::new(move |info| {
40            Printer::print(&info);
41        }))
42    }}
43}
44
45pub mod formatter;