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