errlanes-derive 0.18.1

Derive macros for errlanes (Rejection, Lift, Failure)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use convert_case::{Case, Casing};
use darling::{FromDeriveInput, FromVariant, ast};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Field, Ident, Path};

use crate::classify;
use crate::std_error::{self, DisplayDefault, Unit};

#[derive(Debug, FromDeriveInput)]
#[darling(attributes(rejection), supports(enum_any, struct_any))]
struct RejectionInput {
    ident: Ident,
    data: ast::Data<RejectionVariant, ()>,
    #[darling(default)]
    code_prefix: Option<String>,
    /// Required for a struct: there is no variant to carry a leaf code.
    #[darling(default)]
    code: Option<String>,
    #[darling(default)]
    level: Option<String>,
    /// Overrides the catalogue description, which otherwise is the first
    /// paragraph of the `///` doc comment, else `None`.
    #[darling(default)]
    description: Option<String>,
    /// Emits `From<Payload>`, with the payload also `source()`'s default.
    #[darling(default)]
    from: bool,
    /// `error = manual`: this type's `Display`/`Error` come from elsewhere
    /// (`thiserror`, or by hand); errlanes emits neither.
    #[darling(default)]
    error: Option<Path>,
}

impl RejectionInput {
    fn error_manual(&self) -> darling::Result<bool> {
        match &self.error {
            None => Ok(false),
            Some(p) if p.is_ident("manual") => Ok(true),
            Some(p) => Err(darling::Error::custom(
                "unknown `error` value, expected `error = manual`",
            )
            .with_span(p)),
        }
    }
}

fn level_expr(level: &Option<String>, span: &Ident) -> darling::Result<TokenStream> {
    Ok(match level.as_deref() {
        Some("trace") => quote! { errlanes::Level::Trace },
        Some("debug") => quote! { errlanes::Level::Debug },
        Some("info") => quote! { errlanes::Level::Info },
        Some("warn") | None => quote! { errlanes::Level::Warn },
        Some("error") => quote! { errlanes::Level::Error },
        Some(other) => {
            return Err(darling::Error::custom(format!(
                "unknown rejection level `{other}`, expected one of trace/debug/info/warn/error"
            ))
            .with_span(span));
        }
    })
}

#[derive(Debug, FromVariant)]
#[darling(attributes(rejection))]
struct RejectionVariant {
    ident: Ident,
    fields: ast::Fields<Field>,
    #[darling(default)]
    code: Option<String>,
    #[darling(default)]
    delegate: bool,
    #[darling(default)]
    code_and_level_from: Option<Path>,
    #[darling(default)]
    #[allow(dead_code)]
    origin: Option<String>,
    #[darling(default)]
    level: Option<String>,
    /// Leaf only: overrides the catalogue description, which otherwise is the
    /// first paragraph of the `///` doc comment, else `None`.
    #[darling(default)]
    description: Option<String>,
    /// Emits `From<Payload>`, with the payload also `source()`'s default.
    #[darling(default)]
    from: bool,
}

/// `Some("..")` / `None` as tokens for `CodeInfo::description`.
fn description_tokens(description: &Option<String>) -> TokenStream {
    match description {
        Some(d) => quote! { Some(#d) },
        None => quote! { None },
    }
}

/// `description = ".."`, else the first paragraph of the leaf's `///` doc
/// comment, else nothing. The `#[error("..")]` literal is a `Display`
/// template, never catalogue prose.
fn leaf_description(description: &Option<String>, attrs: &[syn::Attribute]) -> Option<String> {
    description.clone().or_else(|| doc_summary(attrs))
}

/// The first paragraph of a `///` doc comment, its lines joined by a space.
fn doc_summary(attrs: &[syn::Attribute]) -> Option<String> {
    let mut lines = Vec::new();
    for attr in attrs.iter().filter(|a| a.path().is_ident("doc")) {
        let syn::Meta::NameValue(syn::MetaNameValue {
            value:
                syn::Expr::Lit(syn::ExprLit {
                    lit: syn::Lit::Str(s),
                    ..
                }),
            ..
        }) = &attr.meta
        else {
            continue;
        };
        // A bare `///` is `#[doc = ""]`, which has no `lines()` at all, so
        // treat the empty value itself as one blank line.
        let value = s.value();
        for line in value.lines().chain(value.is_empty().then_some("")) {
            let line = line.trim();
            if !line.is_empty() {
                lines.push(line.to_string());
            } else if !lines.is_empty() {
                return Some(lines.join(" "));
            }
        }
    }
    (!lines.is_empty()).then(|| lines.join(" "))
}

impl RejectionVariant {
    fn is_delegating(&self) -> bool {
        self.delegate
    }

    fn delegate_ty(&self) -> Option<&syn::Type> {
        self.is_delegating().then(|| &self.fields.fields[0].ty)
    }

    fn level_expr(&self) -> TokenStream {
        match self.level.as_deref() {
            Some("trace") => quote! { errlanes::Level::Trace },
            Some("debug") => quote! { errlanes::Level::Debug },
            Some("info") => quote! { errlanes::Level::Info },
            Some("warn") | None => quote! { errlanes::Level::Warn },
            Some("error") => quote! { errlanes::Level::Error },
            Some(other) => {
                let msg = format!(
                    "unknown rejection level `{other}`, expected one of trace/debug/info/warn/error"
                );
                quote! { compile_error!(#msg) }
            }
        }
    }

    fn leaf_code(&self, prefix: &Option<String>) -> String {
        match &self.code {
            Some(c) => c.clone(),
            None => {
                let base = self.ident.to_string().to_case(Case::Constant);
                match prefix {
                    Some(p) => format!("{p}{base}"),
                    None => base,
                }
            }
        }
    }
}

pub fn derive(ast: &syn::DeriveInput) -> darling::Result<TokenStream> {
    let input = RejectionInput::from_derive_input(ast)?;
    let ident = &input.ident;
    let code_ident = quote::format_ident!("{}Code", ident);

    let variants = match &input.data {
        ast::Data::Enum(v) => v,
        ast::Data::Struct(_) => {
            // A struct is one outcome: no variant to carry a leaf code, no
            // composition schema (that is enum-only — a struct cannot be a
            // diamond's source or destination).
            let code = input.code.clone().ok_or_else(|| {
                darling::Error::custom(
                    "a rejection struct requires #[rejection(code = \"..\")]: there is no \
                     variant to derive one from",
                )
                .with_span(&input.ident)
            })?;
            let level = level_expr(&input.level, ident)?;
            let error_manual = input.error_manual()?;
            let syn::Data::Struct(raw) = &ast.data else {
                unreachable!()
            };

            // With `error = manual` the `#[error]` attribute belongs to
            // `thiserror`, not us: no literal to read.
            let error = if error_manual {
                None
            } else {
                std_error::take_error_lit(&ast.attrs).map_err(darling::Error::from)?
            };
            let description = description_tokens(&leaf_description(&input.description, &ast.attrs));

            let mut out = TokenStream::new();
            let from_payload = if input.from {
                let payload =
                    classify::payload_field(&raw.fields, ident, "`#[rejection(from)]` on a struct")
                        .map_err(darling::Error::from)?;
                out.extend(
                    classify::from_impl(ident, &ast.generics, None, &payload, ident)
                        .map_err(darling::Error::from)?,
                );
                Some(payload)
            } else {
                None
            };

            out.extend(quote! {
                #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
                pub enum #code_ident { #ident }

                impl #code_ident {
                    pub const ALL: &'static [&'static str] = &[#code];
                }

                impl errlanes::RejectionCode for #code_ident {
                    const CODES: &'static [errlanes::CodeInfo] = &[errlanes::CodeInfo {
                        code: #code,
                        description: #description,
                    }];
                }

                impl std::fmt::Display for #code_ident {
                    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                        f.write_str((*self).into())
                    }
                }

                impl From<#code_ident> for &'static str {
                    fn from(code: #code_ident) -> &'static str {
                        match code {
                            #code_ident::#ident => #code,
                        }
                    }
                }

                impl errlanes::Rejection for #ident {
                    type Code = #code_ident;

                    fn code(&self) -> #code_ident {
                        #code_ident::#ident
                    }

                    fn level(&self) -> errlanes::Level {
                        #level
                    }
                }
            });

            if !error_manual {
                std_error::reject_from_field_attr(&raw.fields).map_err(darling::Error::from)?;
                let source = match &from_payload {
                    Some(p) => {
                        let name = std_error::field_binding(&raw.fields, p.index);
                        Some(quote! { #name })
                    }
                    None => std_error::marked_source(&raw.fields, ident)
                        .map_err(darling::Error::from)?,
                };
                let pat = std_error::bind_pattern(quote! { Self }, &raw.fields);
                // `Rejection`'s own impl doesn't infer generic bounds the
                // way `Classify`'s does (that's `classify.rs`'s doing, not
                // this derive's), so the `extra` bounds `emit` returns
                // alongside its tokens have nowhere to go here.
                let (display_tokens, _extra) = std_error::emit(
                    ident,
                    &ast.generics,
                    DisplayDefault::Code,
                    &[Unit {
                        pat,
                        fields: &raw.fields,
                        error,
                        name: ident.clone(),
                        source,
                    }],
                )
                .map_err(darling::Error::from)?;
                out.extend(display_tokens);
            }

            return Ok(out);
        }
    };
    let schema = crate::composition::schema(ast).map_err(darling::Error::from)?;
    let error_manual = input.error_manual()?;

    let mut code_variants = Vec::new();
    let mut into_str_arms = Vec::new();
    let mut code_match_arms = Vec::new();
    let mut level_match_arms = Vec::new();
    let mut code_parts = Vec::new();
    let mut from_impls = TokenStream::new();
    let mut display_units = Vec::new();

    let syn::Data::Enum(raw) = &ast.data else {
        unreachable!()
    };
    let mut metadata = TokenStream::new();
    for (v, raw) in variants.iter().zip(&raw.variants) {
        if let Some(source) = &v.code_and_level_from {
            if source.segments.len() < 2 {
                return Err(darling::Error::custom(
                    "code_and_level_from requires a qualified source variant: Source::Variant",
                )
                .with_span(source));
            }
            if v.code.is_some() || v.level.is_some() || v.delegate {
                return Err(darling::Error::custom(
                    "code_and_level_from conflicts with code, level, and delegate; choose source metadata or local metadata",
                )
                .with_span(&v.ident));
            }
        }
        if v.delegate
            && (v.code.is_some()
                || v.level.is_some()
                || v.fields.style != ast::Style::Tuple
                || v.fields.fields.len() != 1)
        {
            return Err(darling::Error::custom(
                "delegate requires exactly one tuple field and conflicts with leaf code/level",
            )
            .with_span(&v.ident));
        }
        let variant_ident = &v.ident;
        let id = crate::composition::variant_id(&variant_ident.to_string());
        let fields: Vec<_> = v
            .fields
            .fields
            .iter()
            .enumerate()
            .map(|(i, f)| {
                f.ident
                    .clone()
                    .unwrap_or_else(|| quote::format_ident!("field_{i}"))
            })
            .collect();
        let types: Vec<_> = v.fields.fields.iter().map(|f| &f.ty).collect();
        let pat = match v.fields.style {
            ast::Style::Unit => quote!(Self::#variant_ident),
            ast::Style::Tuple => quote!(Self::#variant_ident(#(#fields),*)),
            ast::Style::Struct => quote!(Self::#variant_ident { #(#fields),* }),
        };

        let payload = if v.delegate || v.from {
            Some(
                classify::payload_field(
                    &raw.fields,
                    variant_ident,
                    if v.delegate {
                        "`delegate`"
                    } else {
                        "`#[rejection(from)]` on a variant"
                    },
                )
                .map_err(darling::Error::from)?,
            )
        } else {
            None
        };
        if v.from {
            from_impls.extend(
                classify::from_impl(
                    ident,
                    &ast.generics,
                    Some(variant_ident),
                    payload.as_ref().unwrap(),
                    variant_ident,
                )
                .map_err(darling::Error::from)?,
            );
        }
        // `error = manual` hands `#[error]` to `thiserror`: nothing to read.
        let error = if error_manual {
            None
        } else {
            std_error::reject_from_field_attr(&raw.fields).map_err(darling::Error::from)?;
            std_error::take_error_lit(&raw.attrs).map_err(darling::Error::from)?
        };
        if !error_manual {
            let source = match &payload {
                Some(p) => {
                    let name = std_error::field_binding(&raw.fields, p.index);
                    Some(quote! { #name })
                }
                None => std_error::marked_source(&raw.fields, variant_ident)
                    .map_err(darling::Error::from)?,
            };
            display_units.push(Unit {
                pat: pat.clone(),
                fields: &raw.fields,
                error: error.clone(),
                name: variant_ident.clone(),
                source,
            });
        }

        let mut code_and_level_from = v.code_and_level_from.clone();
        // A whole-value `#[lift(Payload)]` (one segment: a struct source, no
        // variant to take code and level from) identifies its metadata source the same
        // way `delegate` does — by the field's own `Rejection` impl, not by
        // a variant's `RejectionMetadata<ID>` — since there is no variant ID
        // to look one up by.
        let mut whole_value_source: Option<Path> = None;
        // Lift owns conversion generation. Its mapping also identifies the
        // default metadata source; no Lift implementation is required here.
        if code_and_level_from.is_none() && v.code.is_none() && !v.delegate {
            let mappings: Vec<_> = raw
                .attrs
                .iter()
                .filter(|a| a.path().is_ident("lift"))
                .map(|a| a.parse_args::<crate::lift::Mapping>())
                .collect::<syn::Result<_>>()
                .map_err(darling::Error::from)?;
            // Converted fields cannot stand in for the source variant's fields
            // when evaluating metadata, even if only the level is overridden.
            if mappings.iter().any(|mapping| mapping.into) {
                return Err(darling::Error::custom(
                    "an `into` lift requires an explicit rejection code or \
                     `#[rejection(delegate)]`",
                )
                .with_span(&v.ident));
            }
            if v.level.is_none() && mappings.len() > 1 {
                return Err(darling::Error::custom(
                    "multiple source variants require an explicit canonical rejection code",
                )
                .with_span(&v.ident));
            }
            if let Some(mapping) = mappings.into_iter().next() {
                // A `field =` projection, like a `with =` mapper, breaks the
                // assumption metadata forwarding relies on: that the
                // destination's own fields mirror the source variant's
                // fields exactly, so they can stand in for them when asking
                // the source `RejectionMetadata` for its code/level. A
                // projection keeps only the one named field, not the whole
                // source payload, so there is nothing of the right shape to
                // `code_and_level_from` with — same restriction, same message family.
                if v.level.is_none() {
                    if mapping.with.is_none() && mapping.field.is_none() {
                        if mapping.case.segments.len() >= 2 {
                            code_and_level_from = Some(mapping.case);
                        } else {
                            whole_value_source = Some(mapping.case);
                        }
                    } else {
                        return Err(darling::Error::custom(
                            "a payload mapper or a `field` projection requires an explicit \
                             rejection code",
                        )
                        .with_span(&v.ident));
                    }
                }
            }
        }
        let forwards_to = |target: &dyn std::fmt::Display| {
            darling::Error::custom(format!(
                "`description` belongs on the leaf that owns the code; this variant forwards \
                 to `{}`",
                target.to_string().replace(' ', "")
            ))
            .with_span(&v.ident)
        };
        let (code, level, variant_codes) = if let Some(mut source) = code_and_level_from {
            if v.description.is_some() {
                return Err(forwards_to(&quote!(#source)));
            }
            let source_variant = source.segments.pop().unwrap().ident;
            source.segments.pop_punct();
            let source_id = crate::composition::variant_id(&source_variant.to_string());
            code_variants.push(quote!(#variant_ident(<#source as errlanes::Rejection>::Code)));
            into_str_arms.push(quote!(#code_ident::#variant_ident(inner) => inner.into()));
            (
                quote!(#code_ident::#variant_ident(<#source as errlanes::RejectionMetadata<#source_id>>::field_code((#(#fields,)*)))),
                quote!(<#source as errlanes::RejectionMetadata<#source_id>>::field_level((#(#fields,)*))),
                quote!(<#source as errlanes::RejectionMetadata<#source_id>>::CODES),
            )
        } else if let Some(source) = &whole_value_source {
            if v.description.is_some() {
                return Err(forwards_to(&quote!(#source)));
            }
            let Some(inner) = fields.first() else {
                return Err(darling::Error::custom(
                    "a whole-value `#[lift(Source)]` (naming a registered source with no \
                     variant) needs exactly one field to hold the source value",
                )
                .with_span(&v.ident));
            };
            code_variants.push(quote!(#variant_ident(<#source as errlanes::Rejection>::Code)));
            into_str_arms.push(quote!(#code_ident::#variant_ident(inner) => inner.into()));
            (
                quote!(#code_ident::#variant_ident(<#source as errlanes::Rejection>::code(#inner))),
                quote!(<#source as errlanes::Rejection>::level(#inner)),
                quote!(<<#source as errlanes::Rejection>::Code as errlanes::RejectionCode>::CODES),
            )
        } else if let Some(inner_ty) = v.delegate_ty() {
            if v.description.is_some() {
                return Err(forwards_to(&quote!(#inner_ty)));
            }
            let inner = &fields[0];
            code_variants.push(quote!(#variant_ident(<#inner_ty as errlanes::Rejection>::Code)));
            into_str_arms.push(quote!(#code_ident::#variant_ident(inner) => inner.into()));
            (
                quote!(#code_ident::#variant_ident(<#inner_ty as errlanes::Rejection>::code(#inner))),
                quote!(<#inner_ty as errlanes::Rejection>::level(#inner)),
                quote!(<<#inner_ty as errlanes::Rejection>::Code as errlanes::RejectionCode>::CODES),
            )
        } else {
            code_variants.push(quote!(#variant_ident));
            let leaf = v.leaf_code(&input.code_prefix);
            into_str_arms.push(quote!(#code_ident::#variant_ident => #leaf));
            let description = description_tokens(&leaf_description(&v.description, &raw.attrs));
            (
                quote!(#code_ident::#variant_ident),
                v.level_expr(),
                quote!(&[errlanes::CodeInfo { code: #leaf, description: #description }]),
            )
        };
        let part = quote!(<#ident as errlanes::RejectionMetadata<#id>>::CODES);
        code_parts.push(part);
        code_match_arms.push(quote!(#pat => #code));
        level_match_arms.push(quote!(#pat => #level));
        metadata.extend(quote! {
            impl errlanes::RejectionMetadata<#id> for #ident {
                type Fields<'a> = (#(&'a #types,)*);
                const CODES: &'static [errlanes::CodeInfo] = #variant_codes;
                #[allow(unused_variables, unused_assignments)]
                fn field_code(fields: <Self as errlanes::RejectionMetadata<#id>>::Fields<'_>) -> #code_ident {
                    let (#(#fields,)*) = fields;
                    #code
                }
                #[allow(unused_variables, unused_assignments)]
                fn field_level(fields: <Self as errlanes::RejectionMetadata<#id>>::Fields<'_>) -> errlanes::Level {
                    let (#(#fields,)*) = fields;
                    #level
                }
            }
        });
    }

    // Private helper consts, named after the (unique-in-module) type.
    let parts_ident = quote::format_ident!("__errlanes_code_parts_{}", ident);
    let len_ident = quote::format_ident!("__errlanes_code_len_{}", ident);
    let mut tokens = quote! {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum #code_ident {
            #(#code_variants),*
        }

        #[allow(non_upper_case_globals)]
        const #parts_ident: &[&[errlanes::CodeInfo]] = &[#(#code_parts),*];
        #[allow(non_upper_case_globals)]
        const #len_ident: usize = errlanes::__catalogue::catalogue_len(#parts_ident);

        impl errlanes::RejectionCode for #code_ident {
            const CODES: &'static [errlanes::CodeInfo] =
                &errlanes::__catalogue::catalogue::<#len_ident>(#parts_ident);
        }

        impl #code_ident {
            pub const ALL: &'static [&'static str] = &errlanes::__catalogue::codes_of::<#len_ident>(
                <#code_ident as errlanes::RejectionCode>::CODES,
            );
        }

        impl std::fmt::Display for #code_ident {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str((*self).into())
            }
        }

        impl From<#code_ident> for &'static str {
            fn from(code: #code_ident) -> &'static str {
                match code {
                    #(#into_str_arms),*
                }
            }
        }

        impl errlanes::Rejection for #ident {
            type Code = #code_ident;

            #[allow(unused_variables, unused_assignments)]
            fn code(&self) -> #code_ident {
                match self {
                    #(#code_match_arms),*
                }
            }

            #[allow(unused_variables, unused_assignments)]
            fn level(&self) -> errlanes::Level {
                match self {
                    #(#level_match_arms),*
                }
            }
        }
    };

    tokens.extend(metadata);
    tokens.extend(schema);
    tokens.extend(from_impls);
    if !error_manual {
        // See the sibling call site above: `Rejection`'s own impl doesn't
        // infer generic bounds, so `emit`'s returned `extra` bounds have
        // nowhere to go here.
        let (display_tokens, _extra) =
            std_error::emit(ident, &ast.generics, DisplayDefault::Code, &display_units)
                .map_err(darling::Error::from)?;
        tokens.extend(display_tokens);
    }
    Ok(tokens)
}