1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#[macro_export]
macro_rules! create_messages {
(@step $code:expr,) => {
#[inline(always)]
pub fn num_exit_codes() -> i32 {
$code
}
};
($(#[$error_type_docs:meta])* $type_:ident, code_mask: $code_mask:expr, code_prefix: $code_prefix:expr, $($(#[$docs:meta])* @$formatted_or_backtraced_list:ident $names:ident { args: ($($arg_names:ident: $arg_types:ty$(,)?)*), msg: $messages:expr, help: $helps:expr, })*) => {
#[allow(unused_imports)] use $crate::{Backtraced, Formatted, LeoMessageCode};
use backtrace::Backtrace;
#[derive(Clone, Debug, Error)]
$(#[$error_type_docs])*
pub enum $type_ {
#[error(transparent)]
Formatted(#[from] Formatted),
#[error(transparent)]
Backtraced(#[from] Backtraced),
}
impl LeoMessageCode for $type_ {
#[inline(always)]
fn exit_code(&self) -> i32 {
match self {
Self::Formatted(formatted) => formatted.exit_code(),
Self::Backtraced(backtraced) => backtraced.exit_code()
}
}
#[inline(always)]
fn error_code(&self) -> String {
match self {
Self::Formatted(formatted) => formatted.error_code(),
Self::Backtraced(backtraced) => backtraced.error_code()
}
}
#[inline(always)]
fn warning_code(&self) -> String {
match self {
Self::Formatted(formatted) => formatted.warning_code(),
Self::Backtraced(backtraced) => backtraced.warning_code()
}
}
#[inline(always)]
fn code_mask() -> i32 {
$code_mask
}
#[inline(always)]
fn message_type() -> String {
$code_prefix.to_string()
}
#[inline(always)]
fn is_error() -> bool {
stringify!($type_).contains("Error")
}
}
impl $type_ {
create_messages!(@step 0i32, $(($(#[$docs])* $formatted_or_backtraced_list, $names($($arg_names: $arg_types,)*), $messages, $helps),)*);
}
};
(@step $code:expr, ($(#[$error_func_docs:meta])* formatted, $name:ident($($arg_names:ident: $arg_types:ty,)*), $message:expr, $help:expr), $(($(#[$docs:meta])* $formatted_or_backtraced_tail:ident, $names:ident($($tail_arg_names:ident: $tail_arg_types:ty,)*), $messages:expr, $helps:expr),)*) => {
$(#[$error_func_docs])*
pub fn $name($($arg_names: $arg_types,)* span: leo_span::Span) -> Self {
Self::Formatted(
Formatted::new_from_span(
$message,
$help,
$code + Self::code_mask(),
Self::code_identifier(),
Self::message_type(),
Self::is_error(),
span,
Backtrace::new(),
)
)
}
create_messages!(@step $code + 1i32, $(($(#[$docs])* $formatted_or_backtraced_tail, $names($($tail_arg_names: $tail_arg_types,)*), $messages, $helps),)*);
};
(@step $code:expr, ($(#[$error_func_docs:meta])* backtraced, $name:ident($($arg_names:ident: $arg_types:ty,)*), $message:expr, $help:expr), $(($(#[$docs:meta])* $formatted_or_backtraced_tail:ident, $names:ident($($tail_arg_names:ident: $tail_arg_types:ty,)*), $messages:expr, $helps:expr),)*) => {
$(#[$error_func_docs])*
pub fn $name($($arg_names: $arg_types,)*) -> Self {
Self::Backtraced(
Backtraced::new_from_backtrace(
$message,
$help,
$code + Self::code_mask(),
Self::code_identifier(),
Self::message_type(),
Self::is_error(),
Backtrace::new(),
)
)
}
create_messages!(@step $code + 1i32, $(($(#[$docs])* $formatted_or_backtraced_tail, $names($($tail_arg_names: $tail_arg_types,)*), $messages, $helps),)*);
};
}