alien-error-derive 1.7.1

Procedural macros for alien-error
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Procedural macro that powers `alien-error`.
//!
//! Usage:
//! ```rust
//! use alien_error::AlienErrorData;
//! #[derive(Debug, AlienErrorData)]
//! enum MyError {
//!     #[error(code = "SOMETHING_WRONG", message = "something went wrong", retryable = "false", internal = "false", http_status_code = 420)]
//!     Oops,
//! }
//! ```
//! The `error(...)` attribute supplies compile-time metadata:
//! • `code`              – short machine friendly identifier (defaults to variant name).
//! • `message`           – human-readable error message with field interpolation.
//! • `retryable`         – flag set to `true` if the operation can be retried.
//! • `internal`          – flag set to `true` if this error should not be exposed.
//! • `http_status_code`  – HTTP status code for this error (defaults to 500).
//!
//! The macro also auto-implements `AlienErrorData` including a `context()` method
//! that serialises variant fields into a JSON map for diagnostic payloads.

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Attribute, Data, DeriveInput};

#[proc_macro_derive(AlienErrorData, attributes(error))]
pub fn derive_alien_error(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let (
        code_match_arms,
        retryable_match_arms,
        internal_match_arms,
        http_status_code_match_arms,
        context_match_arms,
        message_match_arms,
        hint_match_arms,
        retryable_inherit_match_arms,
        internal_inherit_match_arms,
        http_status_code_inherit_match_arms,
        human_layer_presentation_match_arms,
    ) = match input.data {
        Data::Enum(ref data_enum) => {
            let mut code_arms = Vec::new();
            let mut retryable_arms = Vec::new();
            let mut internal_arms = Vec::new();
            let mut http_status_code_arms = Vec::new();
            let mut context_arms = Vec::new();
            let mut message_arms = Vec::new();
            let mut hint_arms = Vec::new();
            let mut retryable_inherit_arms = Vec::new();
            let mut internal_inherit_arms = Vec::new();
            let mut http_status_code_inherit_arms = Vec::new();
            let mut human_layer_presentation_arms = Vec::new();

            for variant in &data_enum.variants {
                let ident = &variant.ident;

                let (
                    code_val,
                    retryable_val,
                    internal_val,
                    http_status_code_val,
                    message_val,
                    hint_val,
                    retryable_inherit,
                    internal_inherit,
                    http_status_code_inherit,
                    human_layer_presentation,
                ) = parse_error_attrs(&variant.attrs, ident.to_string());

                let matcher = if variant.fields.is_empty() {
                    quote! { #name::#ident }
                } else {
                    quote! { #name::#ident { .. } }
                };

                let code_lit = code_val;
                let retry_bool = retryable_val;
                let internal_bool = internal_val;
                let http_status_code_u16 = http_status_code_val;

                code_arms.push(quote! { #matcher => #code_lit });
                retryable_arms.push(quote! { #matcher => #retry_bool });
                internal_arms.push(quote! { #matcher => #internal_bool });
                http_status_code_arms.push(quote! { #matcher => #http_status_code_u16 });
                retryable_inherit_arms.push(quote! { #matcher => #retryable_inherit });
                internal_inherit_arms.push(quote! { #matcher => #internal_inherit });
                http_status_code_inherit_arms
                    .push(quote! { #matcher => #http_status_code_inherit });
                human_layer_presentation_arms
                    .push(quote! { #matcher => #human_layer_presentation });

                // Generate message arm with field interpolation
                match &variant.fields {
                    syn::Fields::Named(fields_named) if !fields_named.named.is_empty() => {
                        // SAFETY: Named fields are guaranteed to have identifiers.
                        // The Option exists only for compatibility with tuple struct fields.
                        let field_idents: Vec<_> = fields_named
                            .named
                            .iter()
                            .map(|f| f.ident.as_ref().unwrap())
                            .collect();
                        let matcher = quote! { #name::#ident { #( ref #field_idents ),* } };

                        // Generate message with field interpolation
                        let interpolated_message =
                            generate_message_interpolation(&message_val, &field_idents);
                        message_arms.push(quote! { #matcher => #interpolated_message });
                        let interpolated_hint =
                            generate_optional_interpolation(&hint_val, &field_idents);
                        hint_arms.push(quote! { #matcher => #interpolated_hint });

                        context_arms.push(quote! { #matcher => {
                            let mut map = serde_json::Map::new();
                            #( map.insert(
                                stringify!(#field_idents).to_string(),
                                serde_json::to_value(#field_idents)
                                    .expect(&format!("Failed to serialize field '{}' to JSON. This field must implement Serialize correctly.", stringify!(#field_idents)))
                            ); )*
                            Some(serde_json::Value::Object(map))
                        } });
                    }
                    _ => {
                        let matcher = if variant.fields.is_empty() {
                            quote! { #name::#ident }
                        } else {
                            quote! { #name::#ident { .. } }
                        };
                        message_arms.push(quote! { #matcher => #message_val.to_string() });
                        let interpolated_hint = generate_optional_interpolation(&hint_val, &[]);
                        hint_arms.push(quote! { #matcher => #interpolated_hint });
                        context_arms.push(quote! { #matcher => None });
                    }
                }
            }
            (
                code_arms,
                retryable_arms,
                internal_arms,
                http_status_code_arms,
                context_arms,
                message_arms,
                hint_arms,
                retryable_inherit_arms,
                internal_inherit_arms,
                http_status_code_inherit_arms,
                human_layer_presentation_arms,
            )
        }
        _ => {
            return quote! { compile_error!("AlienErrorData can only be derived for enums"); }
                .into();
        }
    };

    let expanded = quote! {
        impl alien_error::AlienErrorData for #name {
            fn code(&self) -> &'static str {
                match self {
                    #(#code_match_arms),*
                }
            }
            fn retryable(&self) -> bool {
                match self {
                    #(#retryable_match_arms),*
                }
            }
            fn internal(&self) -> bool {
                match self {
                    #(#internal_match_arms),*
                }
            }
            fn http_status_code(&self) -> u16 {
                match self {
                    #(#http_status_code_match_arms),*
                }
            }
            fn message(&self) -> String {
                match self {
                    #(#message_match_arms),*
                }
            }
            fn hint(&self) -> Option<String> {
                match self {
                    #(#hint_match_arms),*
                }
            }
            fn context(&self) -> Option<serde_json::Value> {
                match self {
                    #(#context_match_arms),*
                }
            }
            fn retryable_inherit(&self) -> Option<bool> {
                match self {
                    #(#retryable_inherit_match_arms),*
                }
            }
            fn internal_inherit(&self) -> Option<bool> {
                match self {
                    #(#internal_inherit_match_arms),*
                }
            }
            fn http_status_code_inherit(&self) -> Option<u16> {
                match self {
                    #(#http_status_code_inherit_match_arms),*
                }
            }
            fn human_layer_presentation(&self) -> alien_error::HumanLayerPresentation {
                match self {
                    #(#human_layer_presentation_match_arms),*
                }
            }
        }

        impl std::fmt::Display for #name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.message())
            }
        }
    };

    TokenStream::from(expanded)
}

fn parse_error_attrs(
    attrs: &[Attribute],
    default_code: String,
) -> (
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
    String,
    Option<String>,
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
) {
    let mut code = default_code;
    let mut retryable: Option<String> = None;
    let mut internal: Option<String> = None;
    let mut http_status_code: Option<String> = None;
    let mut message: Option<String> = None;
    let mut hint: Option<String> = None;
    let mut human: Option<String> = None;

    for attr in attrs {
        if !attr.path().is_ident("error") {
            continue;
        }
        if let Err(e) = attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("code") {
                let lit: syn::LitStr = meta.value()?.parse()?;
                code = lit.value();
                Ok(())
            } else if meta.path.is_ident("retryable") {
                let lit: syn::LitStr = meta.value()?.parse()?;
                retryable = Some(lit.value());
                Ok(())
            } else if meta.path.is_ident("internal") {
                let lit: syn::LitStr = meta.value()?.parse()?;
                internal = Some(lit.value());
                Ok(())
            } else if meta.path.is_ident("http_status_code") {
                // Parse the value as a literal (either string or int)
                let value = meta.value()?;

                // Try to parse as a literal expression
                let lit: syn::Lit = value.parse()?;

                match lit {
                    syn::Lit::Str(lit_str) => {
                        // String literal like "inherit" or "404"
                        http_status_code = Some(lit_str.value());
                    }
                    syn::Lit::Int(lit_int) => {
                        // Integer literal like 404
                        let parsed_value = lit_int.base10_parse::<u16>()?;
                        http_status_code = Some(parsed_value.to_string());
                    }
                    _ => {
                        return Err(
                            meta.error("http_status_code must be a string or integer literal")
                        );
                    }
                }
                Ok(())
            } else if meta.path.is_ident("message") {
                let lit: syn::LitStr = meta.value()?.parse()?;
                message = Some(lit.value());
                Ok(())
            } else if meta.path.is_ident("hint") {
                let lit: syn::LitStr = meta.value()?.parse()?;
                hint = Some(lit.value());
                Ok(())
            } else if meta.path.is_ident("human") {
                let lit: syn::LitStr = meta.value()?.parse()?;
                human = Some(lit.value());
                Ok(())
            } else {
                Err(meta.error("unsupported error attribute key"))
            }
        }) {
            // Re-emit the actual syn error instead of a generic message
            return (
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
                String::new(),
                None,
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
                syn::Error::new(e.span(), e.to_string()).to_compile_error(),
            );
        }
    }

    // ensure all required fields are specified
    macro_rules! parse_flag {
        ($val:expr,$name:expr) => {
            match $val {
                Some(ref s) if s == "true" => quote! { true },
                Some(ref s) if s == "false" => quote! { false },
                Some(ref s) if s == "inherit" => quote! { false }, // For backward compatibility in the main method
                Some(ref _other) => syn::Error::new(proc_macro2::Span::call_site(), format!("{} must be \"true\", \"false\" or \"inherit\"", $name)).to_compile_error(),
                None => syn::Error::new(proc_macro2::Span::call_site(), format!("{}=\"...\" is required in #[error(...)]", $name)).to_compile_error(),
            }
        };
    }

    // Parse inheritance flags from the same values
    macro_rules! parse_inherit_flag {
        ($val:expr) => {
            match $val {
                Some(ref s) if s == "inherit" => quote! { None },
                Some(ref s) if s == "true" => quote! { Some(true) },
                Some(ref s) if s == "false" => quote! { Some(false) },
                Some(_) => quote! { Some(false) }, // fallback for any other value
                None => syn::Error::new(proc_macro2::Span::call_site(), "flag is required")
                    .to_compile_error(),
            }
        };
    }

    let retry_ts = parse_flag!(retryable.clone(), "retryable");
    let internal_ts = parse_flag!(internal.clone(), "internal");
    let retryable_inherit_ts = parse_inherit_flag!(retryable);
    let internal_inherit_ts = parse_inherit_flag!(internal);

    let code_ts = {
        let lit = syn::LitStr::new(&code, proc_macro2::Span::call_site());
        quote! { #lit }
    };

    // Parse HTTP status code with inheritance support
    let (http_status_code_ts, http_status_code_inherit_ts) = match http_status_code {
        Some(ref s) if s == "inherit" => {
            // When inherit is specified, use 500 as the default but return None for inherit
            (quote! { 500 }, quote! { None })
        }
        Some(ref s) => {
            // Parse as number
            match s.parse::<u16>() {
                Ok(status_code) => (quote! { #status_code }, quote! { Some(#status_code) }),
                Err(_) => (
                    syn::Error::new(
                        proc_macro2::Span::call_site(),
                        "http_status_code must be a number or \"inherit\"",
                    )
                    .to_compile_error(),
                    syn::Error::new(
                        proc_macro2::Span::call_site(),
                        "http_status_code must be a number or \"inherit\"",
                    )
                    .to_compile_error(),
                ),
            }
        }
        None => {
            // Default to 500
            (quote! { 500 }, quote! { Some(500) })
        }
    };

    let message_str = message.unwrap_or_else(|| code.clone());
    let human_ts = match human.as_deref() {
        None | Some("normal") => quote! { alien_error::HumanLayerPresentation::Normal },
        Some("transparent") => quote! { alien_error::HumanLayerPresentation::Transparent },
        Some(other) => syn::Error::new(
            proc_macro2::Span::call_site(),
            format!(
                "human must be \"normal\" or \"transparent\", got \"{}\"",
                other
            ),
        )
        .to_compile_error(),
    };

    (
        code_ts,
        retry_ts,
        internal_ts,
        http_status_code_ts,
        message_str,
        hint,
        retryable_inherit_ts,
        internal_inherit_ts,
        http_status_code_inherit_ts,
        human_ts,
    )
}

fn generate_message_interpolation(
    message_template: &str,
    field_idents: &[&syn::Ident],
) -> proc_macro2::TokenStream {
    // Let Rust's format! macro handle the parsing - just pass the template and fields directly
    // This leverages Rust's built-in format string parsing which handles all cases correctly

    if field_idents.is_empty() {
        quote! { #message_template.to_string() }
    } else {
        // Find which fields are actually used in the message template
        let used_fields: Vec<&syn::Ident> = field_idents
            .iter()
            .filter(|field| {
                let field_name = field.to_string();
                message_template.contains(&format!("{{{}", field_name))
            })
            .cloned()
            .collect();

        if used_fields.is_empty() {
            // No fields are referenced in the template
            quote! { #message_template.to_string() }
        } else {
            // Use named parameters - only pass fields that are actually used
            quote! { format!(#message_template, #(#used_fields = #used_fields),*) }
        }
    }
}

fn generate_optional_interpolation(
    template: &Option<String>,
    field_idents: &[&syn::Ident],
) -> proc_macro2::TokenStream {
    match template {
        Some(template) => {
            let interpolated = generate_message_interpolation(template, field_idents);
            quote! { Some(#interpolated) }
        }
        None => quote! { None },
    }
}