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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! # impass
//!
//! `impass` is a utility crate that provides the `fatal!` macro for handling
//! unrecoverable errors in a declarative and safe way. It is built on the
//! philosophy that some errors are not meant to be handled gracefully, but
//! rather signal a critical failure in program logic or state.
//!
//! The `fatal!` macro provides a clear, concise, and idiomatic way to express
//! this "fail-fast" intent. It is analogous to the `assert!` macro, where a
//! failed assertion indicates a bug that should crash the program.
//!
//! ### Why `fatal!`?
//!
//! The idiomatic way to handle a `Result` that is expected to always be `Ok` is
//! to use `.unwrap()` or `.expect()`. While this works, it can become verbose
//! when chaining multiple fallible operations.
//!
//! This macro provides a single, cohesive block for this "fail-fast" behavior,
//! making your code more readable and your intent explicit.
//!
//! ### Quick Start
//! You can simply use the macro to wrap a block of code where you expect all
//! operations to succeed:
//!
//! ```rust,should_panic
//! use thiserror::Error;
//! use impass::fatal;
//!
//! // Declare an error type for demonstration purposes.
//! #[derive(Error, Debug)]
//! pub enum MyError {
//! #[error("This operation failed")]
//! OperationFailed
//! }
//!
//! // A fallible function for demonstration.
//! fn might_fail(value: i32) -> Result<i32, MyError> {
//! if value < 10 {
//! Err(MyError::OperationFailed)
//! } else {
//! Ok(value * 2)
//! }
//! }
//!
//! fn main() {
//! // Usage with a custom message using the `#![reason]` attribute.
//! let final_value = fatal! {
//! #![reason("The crucial calculation failed. Program state is unrecoverable")]
//! let value = might_fail(15)?;
//! Ok(value)
//! };
//!
//! println!("Execution succeeded. Final value: {}", final_value);
//!
//! // An example that will cause a panic.
//! fatal! {
//! let result = might_fail(5)?;
//! Ok(())
//! }
//! }
//! ```
//!
//! ---
extern crate proc_macro;
use TokenStream;
use TokenStream as TokenStream2;
use ;
use parse_macro_input;
use ;
use parse2;
/// A declarative macro for handling critical, unrecoverable errors.
///
/// The `fatal!` macro wraps a block of code, allowing for a clean and
/// expressive "fail-fast" pattern. It provides a highly visible boundary for
/// code that is expected to be infallible, and it signals a critical bug if a
/// `Result::Err` is returned.
///
/// This macro is a more ergonomic and readable alternative to
/// ```rust,ignore
/// (|| -> Result<_, anyhow::Error> { ... })().unwrap()
/// ```
///
/// ### Behavior
///
/// 1. **Allows `?` Operator:** The macro wraps the provided block in a
/// closure, enabling the use of the `?` operator for seamless error
/// propagation.
/// 2. **Detailed Panic:** If the block returns an `Err`, the macro catches the
/// error, prints a detailed error message which includes any context. For
/// this reason it is recommended to use the `context` function from
/// `anyhow`.
/// 3. **Returns a Value:** The macro returns the inner value of the `Ok`
/// variant, allowing it to be used in assignments.
///
/// ### Usage
///
/// The macro accepts a code block that must return a `Result` type.
///
/// ```rust,should_panic
/// use thiserror::Error;
/// use impass::fatal;
///
/// // Declare an error type for demonstration purposes.
/// #[derive(Error, Debug)]
/// pub enum MyError {
/// #[error("This operation failed")]
/// OperationFailed
/// }
///
/// // A fallible function for demonstration.
/// fn might_fail(value: i32) -> Result<i32, MyError> {
/// if value < 10 {
/// Err(MyError::OperationFailed)
/// } else {
/// Ok(value * 2)
/// }
/// }
///
/// fn main() {
/// // Simple usage without a custom message.
/// let final_value = fatal! {
/// let value = might_fail(15)?;
/// Ok(value)
/// };
/// println!("Successfully computed: {}", final_value);
///
/// // Usage with a custom message using the `#![reason]` attribute.
/// fatal! {
/// #![reason("Failed to initialize system-critical component.")]
/// let result = might_fail(5)?; // This will cause a panic.
/// Ok(())
/// }
/// }
/// ```
/// Handles the parsing of the `fatal!` macro's input.
/// An attribute macro that wraps a function's body in the `fatal!` macro.
///
/// This macro allows you to specify an optional reason for the fatal error
/// using the attribute argument.
///
/// ### Example
/// ```rust
/// use thiserror::Error;
/// use impass::{fatal, fatal_fn};
///
/// // Declare an error type for demonstration purposes.
/// #[derive(Error, Debug)]
/// pub enum MyError {
/// #[error("This operation failed")]
/// OperationFailed
/// }
///
/// // A fallible function for demonstration.
/// fn might_fail(value: i32) -> Result<i32, MyError> {
/// if value < 10 {
/// Err(MyError::OperationFailed)
/// } else {
/// Ok(value * 2)
/// }
/// }
///
/// // Using the `fatal_fn` macro to wrap a function.
/// #[fatal_fn(reason = "Critical failure in function execution")]
/// fn example_function() -> i32 {
/// let value = might_fail(15)?;
/// Ok(value)
/// }
///
/// // The above function is equivalent to:
/// fn example_function_actual() -> i32 {
/// fatal! {
/// #![reason("Critical failure in function execution")]
/// let value = might_fail(15)?;
/// Ok(value)
/// }
/// }
/// ```