1#[macro_export]
6macro_rules! fatal {
7 ($($arg:tt)*) => {{
8 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#[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 .map_err(|e| ::anyhow::anyhow!("Failed to deserialize field '{}': {}", stringify!($method), e))?;
37 target.$method(data.unwrap($map)
38 .map_err(|e| ::anyhow::anyhow!("Failed to unwrap DataPack for field '{}': {}", stringify!($method), e))?)
39 } else {
40 target
41 };
42 )*
43 Ok::<_, ::anyhow::Error>(target)
44 }};
45}
46
47#[macro_export]
50macro_rules! translate_enum {
51 ($target:expr, $value:expr, $map:expr, $(
52 ($method:ident, { $($str_val:literal => $variant:expr),* $(,)? })
53 ),* $(,)?) => {{
54 use $crate::preprocessor::handlers::code_handler::until::DataPack;
55 let target = $target;
56 $(
57 let target = if let Some(v) = $value.get_mut(stringify!($method)) {
58 let data = serde_json::from_value::<DataPack<String>>(v.take())
59 .map_err(|e| ::anyhow::anyhow!("Failed to deserialize field '{}': {}", stringify!($method), e))?;
60 let s = data.unwrap($map)
61 .map_err(|e| ::anyhow::anyhow!("Failed to unwrap DataPack for field '{}': {}", stringify!($method), e))?;
62 match s.as_str() {
63 $($str_val => target.$method($variant),)*
64 unexpected => {
65 return Err(::anyhow::anyhow!(
66 "\"{}\" is not a valid value for `{}`",
67 unexpected,
68 stringify!($method),
69 ))
70 }
71 }
72 } else {
73 target
74 };
75 )*
76 Ok::<_, ::anyhow::Error>(target)
77 }};
78}