Skip to main content

mdbook_plotly/
macros.rs

1/// Used to send critical errors.
2/// Will exit directly with exit code `1`.
3///
4/// NOTE: This macro is only useful after Rust version `1.79`.
5#[macro_export]
6macro_rules! fatal {
7    ($($arg:tt)*) => {{
8        // This line only compiles smoothly after version `1.79`.
9        // Compiling with older versions may result in temporary value errors.
10        //
11        // If you need some information, here it is:
12        // Issue: [#92698](https://github.com/rust-lang/rust/issues/92698)
13        // Tracing issues with RFC66: [#15023](https://github.com/rust-lang/rust/issues/15023)
14        // The std doc about `Arguments`: [`std::fmt::Arguments`](https://doc.rust-lang.org/stable/std/fmt/struct.Arguments.html)
15        let msg = format_args!($($arg)*);
16
17        log::error!("Critical error: {}", msg);
18        #[cfg(debug_assertions)]
19        {
20            log::debug!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
21        }
22        std::process::exit(1);
23    }};
24}
25
26/// Used to translate `serde_json::Value` into DataPack<T>.
27/// This macro avoids writing a lot of duplicate code.
28#[macro_export]
29macro_rules! translate {
30    ($target:expr, $value:expr, $map:expr, $(($method:ident, $ty:ty)),* $(,)?) => {{
31        use $crate::preprocessor::handlers::code_handler::until::DataPack;
32        let target = $target;
33        $(
34            let target = if let Some(v) = $value.get_mut(stringify!($method)) {
35                let data = serde_json::from_value::<DataPack<$ty>>(v.take())?;
36                target.$method(data.unwrap($map)?)
37            } else {
38                target
39            };
40        )*
41        Ok::<_, serde_json::Error>(target)
42    }};
43}