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, $map_eval:expr, $(($method:ident, $ty:ty)),* $(,)?) => {{
31        $crate::translate_with_config!($target, $value, $map, $map_eval, $(($method, $ty)),*)
32    }};
33}
34
35#[macro_export]
36macro_rules! translate_with_config {
37    ($target:expr, $value:expr, $map:expr, $map_eval:expr, $(($method:ident, $ty:ty)),* $(,)?) => {{
38        use $crate::code_handler::until::DataPack;
39        let target = $target;
40        $(
41            let target = if let Some(v) = $value.get_mut(stringify!($method)) {
42                let data = serde_json::from_value::<DataPack<$ty>>(v.take())
43                    .map_err(|e| ::anyhow::anyhow!("Failed to deserialize field '{}': {}", stringify!($method), e))?;
44                target.$method(data.unwrap($map, $map_eval)
45                    .map_err(|e| ::anyhow::anyhow!("Failed to unwrap DataPack for field '{}': {}", stringify!($method), e))?)
46            } else {
47                target
48            };
49        )*
50        anyhow::Ok(target)
51    }};
52}
53
54/// Used to translate string values in `serde_json::Value` into enum variants
55/// via `DataPack<String>`, avoiding a lot of duplicate match-and-build code.
56#[macro_export]
57macro_rules! translate_enum {
58    ($target:expr, $value:expr, $map:expr, $map_eval:expr, $(
59        ($method:ident, { $($str_val:literal => $variant:expr),* $(,)? })
60    ),* $(,)?) => {{
61        $crate::translate_enum_with_config!($target, $value, $map, $map_eval, $(($method, { $($str_val => $variant),* })),*)
62    }};
63}
64
65#[macro_export]
66macro_rules! translate_enum_with_config {
67    ($target:expr, $value:expr, $map:expr, $map_eval:expr, $(
68        ($method:ident, { $($str_val:literal => $variant:expr),* $(,)? })
69    ),* $(,)?) => {{
70        use $crate::code_handler::until::DataPack;
71        let target = $target;
72        $(
73            let target = if let Some(v) = $value.get_mut(stringify!($method)) {
74                let data = serde_json::from_value::<DataPack<String>>(v.take())
75                    .map_err(|e| ::anyhow::anyhow!("Failed to deserialize field '{}': {}", stringify!($method), e))?;
76                let s = data.unwrap($map, $map_eval)
77                    .map_err(|e| ::anyhow::anyhow!("Failed to unwrap DataPack for field '{}': {}", stringify!($method), e))?;
78                match s.as_str() {
79                    $($str_val => target.$method($variant),)*
80                    unexpected => {
81                        return Err(::anyhow::anyhow!(
82                            "\"{}\" is not a valid value for `{}`",
83                            unexpected,
84                            stringify!($method),
85                        ))
86                    }
87                }
88            } else {
89                target
90            };
91        )*
92        anyhow::Ok(target)
93    }};
94}