errlanes-derive 0.16.13

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
use proc_macro2::TokenStream;
use quote::{ToTokens, format_ident, quote};
use syn::{Fields, Path, Token, parse::Parse};

pub(crate) struct Registration {
    pub(crate) source: Path,
    partial: bool,
}
impl Parse for Registration {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let source = input.parse()?;
        let mut partial = false;
        if input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
            let mode: syn::Ident = input.parse()?;
            if mode == "unhandled" {
                input.parse::<Token![=]>()?;
                let fatal: syn::Ident = input.parse()?;
                if fatal != "fatal" {
                    return Err(syn::Error::new_spanned(fatal, "expected fatal"));
                }
                partial = true;
            } else if mode != "strict" {
                return Err(syn::Error::new_spanned(
                    mode,
                    "expected strict or unhandled = fatal",
                ));
            }
        }
        Ok(Self { source, partial })
    }
}
pub(crate) struct Mapping {
    pub case: Path,
    pub with: Option<Path>,
    pub field: Option<syn::Ident>,
    pub into: bool,
}
impl Parse for Mapping {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let case = input.parse()?;
        let mut with = None;
        let mut field = None;
        let mut into = false;
        while input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
            let key: syn::Ident = input.parse()?;
            if key == "into" {
                if into {
                    return Err(syn::Error::new_spanned(key, "duplicate `into`"));
                }
                if with.is_some() || field.is_some() {
                    return Err(syn::Error::new_spanned(
                        key,
                        "`into` cannot be combined with `field` or `with`",
                    ));
                }
                into = true;
                continue;
            }
            if into && (key == "field" || key == "with") {
                return Err(syn::Error::new_spanned(
                    key,
                    "`into` cannot be combined with `field` or `with`",
                ));
            }
            input.parse::<Token![=]>()?;
            if key == "with" {
                if field.is_some() {
                    return Err(syn::Error::new_spanned(
                        key,
                        "`field` and `with` cannot be combined",
                    ));
                }
                with = Some(input.parse()?);
            } else if key == "field" {
                if with.is_some() {
                    return Err(syn::Error::new_spanned(
                        key,
                        "`field` and `with` cannot be combined",
                    ));
                }
                field = Some(input.parse()?);
            } else {
                return Err(syn::Error::new_spanned(
                    key,
                    "expected `with = mapper`, `field = name`, or `into`",
                ));
            }
        }
        Ok(Self {
            case,
            with,
            field,
            into,
        })
    }
}
pub fn derive(input: &syn::DeriveInput) -> syn::Result<TokenStream> {
    if let syn::Data::Struct(data) = &input.data {
        return derive_struct(input, data);
    }
    let syn::Data::Enum(data) = &input.data else {
        return Err(syn::Error::new_spanned(
            input,
            "Lift can only be derived for enums or structs",
        ));
    };
    let mut registrations = Vec::new();
    for attr in &input.attrs {
        if attr.path().is_ident("lift") {
            registrations.push(attr.parse_args::<Registration>()?);
        }
    }
    let mut mappings = Vec::new();
    let mut seen = std::collections::HashSet::new();
    for variant in &data.variants {
        for field in &variant.fields {
            if let Some(attr) = field.attrs.iter().find(|a| a.path().is_ident("lift")) {
                return Err(syn::Error::new_spanned(
                    attr,
                    "field lift mappings are only supported on struct destinations",
                ));
            }
        }
        for attr in &variant.attrs {
            if !attr.path().is_ident("lift") {
                continue;
            }
            let mapping = attr.parse_args::<Mapping>()?;
            if !seen.insert(mapping.case.to_token_stream().to_string()) {
                return Err(syn::Error::new_spanned(
                    attr,
                    "source variant mapped more than once",
                ));
            }
            // `#[lift(Payload)]` (the registered source itself, no variant
            // suffix) is a whole-value arm: `Payload` is a struct source,
            // and the entire value becomes the one field of the destination
            // variant. `#[lift(Source::Variant)]` is the per-variant
            // forwarding arm. A bare source matching a registration wins the
            // whole-value reading; otherwise the last segment is popped off
            // and matched as a variant, as before.
            let case_key = mapping.case.to_token_stream().to_string();
            let whole_value = registrations
                .iter()
                .any(|r| r.source.to_token_stream().to_string() == case_key);
            let owner = if whole_value {
                mapping.case.clone()
            } else {
                let mut owner = mapping.case.clone();
                owner.segments.pop();
                owner.segments.pop_punct();
                owner
            };
            if !registrations.iter().any(|r| {
                r.source.to_token_stream().to_string() == owner.to_token_stream().to_string()
            }) {
                return Err(syn::Error::new_spanned(
                    attr,
                    "source family requires enum-level #[lift(Source)] registration",
                ));
            }
            mappings.push((owner, whole_value, variant, mapping));
        }
    }
    let mut out = TokenStream::new();
    let mut owners = std::collections::HashSet::new();
    for registration in registrations {
        let source = registration.source;
        let key = source.to_token_stream().to_string();
        if !owners.insert(key.clone()) {
            return Err(syn::Error::new_spanned(source, "duplicate lift source"));
        }
        let mut arms = Vec::new();
        for (owner, whole_value, variant, mapping) in &mappings {
            if owner.to_token_stream().to_string() != key {
                continue;
            }
            let case = &mapping.case;
            let dest = &variant.ident;
            let cfg: Vec<_> = variant
                .attrs
                .iter()
                .filter(|a| a.path().is_ident("cfg"))
                .collect();
            let arm = if let Some(mapper) = &mapping.with {
                // The mapper consumes the whole selected source; supports genuine shape changes.
                quote! { value @ #case { .. } => Ok(#mapper(value)) }
            } else if mapping.into {
                if *whole_value {
                    return Err(syn::Error::new_spanned(
                        case,
                        "an `into` lift requires a source variant (`Source::Variant`)",
                    ));
                }
                // Only the payload is converted. The tuple pattern makes rustc
                // check that the source also has exactly one unnamed field.
                let converted = quote!(::core::convert::Into::into(payload));
                match &variant.fields {
                    Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
                        quote! { #case(payload) => Ok(Self::#dest(#converted)) }
                    }
                    Fields::Named(fields) if fields.named.len() == 1 => {
                        let field = &fields.named[0].ident;
                        quote! { #case(payload) => Ok(Self::#dest { #field: #converted }) }
                    }
                    _ => {
                        return Err(syn::Error::new_spanned(
                            &variant.ident,
                            "an `into` lift needs exactly one destination field",
                        ));
                    }
                }
            } else if let Some(field) = &mapping.field {
                // A projection of one field out of the source variant's
                // payload. `#case(payload)` is a tuple-destructuring
                // pattern, not `#case { .. }`: the source variant must be a
                // single-field tuple variant, and a named-field or unit
                // source variant fails right here as an ordinary type
                // error — the derive cannot know a foreign variant's field
                // names, so it cannot validate the shape ahead of time.
                match &variant.fields {
                    Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
                        quote! { #case(payload) => Ok(Self::#dest(payload.#field)) }
                    }
                    Fields::Named(fields) if fields.named.len() == 1 => {
                        let dest_field = &fields.named[0].ident;
                        quote! {
                            #case(payload) => Ok(Self::#dest { #dest_field: payload.#field })
                        }
                    }
                    _ => {
                        return Err(syn::Error::new_spanned(
                            &variant.ident,
                            "a `field` lift needs exactly one destination field",
                        ));
                    }
                }
            } else if *whole_value {
                // `Payload` is a struct source: the whole value becomes the
                // one field of the destination variant.
                match &variant.fields {
                    Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
                        quote! { value @ #case { .. } => Ok(Self::#dest(value)) }
                    }
                    Fields::Named(fields) if fields.named.len() == 1 => {
                        let field = &fields.named[0].ident;
                        quote! { value @ #case { .. } => Ok(Self::#dest { #field: value }) }
                    }
                    _ => {
                        return Err(syn::Error::new_spanned(
                            &variant.ident,
                            "a whole-value lift (`#[lift(Source)]` with no variant) needs \
                             exactly one destination field to hold the source value",
                        ));
                    }
                }
            } else {
                match &variant.fields {
                    Fields::Unit => quote! { #case => Ok(Self::#dest) },
                    Fields::Unnamed(fields) => {
                        let args: Vec<_> = (0..fields.unnamed.len())
                            .map(|i| format_ident!("field_{i}"))
                            .collect();
                        quote! { #case(#(#args),*) => Ok(Self::#dest(#(#args),*)) }
                    }
                    Fields::Named(fields) => {
                        let args: Vec<_> = fields.named.iter().map(|f| &f.ident).collect();
                        quote! { #case { #(#args),* } => Ok(Self::#dest { #(#args),* }) }
                    }
                }
            };
            arms.push(quote! { #(#cfg)* #arm });
        }
        out.extend(conversion_impl(input, &source, registration.partial, &arms));
    }
    Ok(out)
}

fn conversion_impl(
    input: &syn::DeriveInput,
    source: &Path,
    partial: bool,
    arms: &[TokenStream],
) -> TokenStream {
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    // Strict mode emits only `From`: errlanes' blanket `impl<X, P: From<X>>
    // Lift<X> for P` supplies the `Lift` view with `Unmapped = Infallible`,
    // so one call-site method (`widen`) covers strict and partial alike.
    // Emitting both here would collide with that blanket (E0119).
    if partial {
        quote! {
            impl #impl_generics errlanes::Lift<#source> for #name #ty_generics #where_clause {
                type Unmapped = #source;
                fn lift(source: #source) -> Result<Self, Self::Unmapped> {
                    match source { #(#arms,)* unhandled => Err(unhandled) }
                }
            }
        }
    } else {
        quote! {
            impl #impl_generics From<#source> for #name #ty_generics #where_clause {
                fn from(source: #source) -> Self {
                    let mapped: Result<Self, core::convert::Infallible> =
                        match source { #(#arms,)* };
                    match mapped {
                        Ok(mapped) => mapped, Err(never) => match never {},
                    }
                }
            }
        }
    }
}

/// A struct maps exactly one named-field source variant into its own fields.
/// Keep this grammar separate from enum registrations: `variant` is meaningful
/// only on a struct destination, and enum mappings retain their existing syntax.
struct StructRegistration {
    source: Path,
    variant: syn::Ident,
    partial: bool,
}

impl Parse for StructRegistration {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let source: Path = input.parse()?;
        let mut variant = None;
        let mut partial = None;
        while input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
            if input.is_empty() {
                break;
            }
            let key: syn::Ident = input.parse()?;
            match key.to_string().as_str() {
                "variant" => {
                    if variant.is_some() {
                        return Err(syn::Error::new_spanned(key, "duplicate `variant`"));
                    }
                    input.parse::<Token![=]>()?;
                    variant = Some(input.parse()?);
                }
                "strict" | "unhandled" => {
                    if partial.is_some() {
                        return Err(syn::Error::new_spanned(
                            key,
                            "choose either `strict` or `unhandled = fatal` once",
                        ));
                    }
                    partial = Some(key == "unhandled");
                    if key == "unhandled" {
                        input.parse::<Token![=]>()?;
                        let fatal: syn::Ident = input.parse()?;
                        if fatal != "fatal" {
                            return Err(syn::Error::new_spanned(fatal, "expected fatal"));
                        }
                    }
                }
                _ => {
                    return Err(syn::Error::new_spanned(
                        key,
                        "expected `variant = Case`, `strict`, or `unhandled = fatal`",
                    ));
                }
            }
        }
        let variant = variant.ok_or_else(|| {
            syn::Error::new_spanned(&source, "a struct lift requires `variant = Case`")
        })?;
        Ok(Self {
            source,
            variant,
            partial: partial.unwrap_or(false),
        })
    }
}

struct SourceField(syn::Ident);

impl Parse for SourceField {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let key: syn::Ident = input.parse()?;
        if key != "from" {
            return Err(syn::Error::new_spanned(
                key,
                "expected `from = source_field`",
            ));
        }
        input.parse::<Token![=]>()?;
        let field = input.parse()?;
        if input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
        }
        Ok(Self(field))
    }
}

fn derive_struct(input: &syn::DeriveInput, data: &syn::DataStruct) -> syn::Result<TokenStream> {
    if let Fields::Unnamed(fields) = &data.fields {
        return derive_newtype(input, fields);
    }
    let Fields::Named(fields) = &data.fields else {
        return Err(syn::Error::new_spanned(
            input,
            "a struct lift requires named fields or a single-field tuple struct",
        ));
    };
    let mut attrs = input.attrs.iter().filter(|a| a.path().is_ident("lift"));
    let attr = attrs.next().ok_or_else(|| {
        syn::Error::new_spanned(
            &input.ident,
            "a struct lift requires #[lift(Source, variant = Case)]",
        )
    })?;
    if let Some(extra) = attrs.next() {
        return Err(syn::Error::new_spanned(
            extra,
            "a struct lift supports exactly one source registration",
        ));
    }
    let StructRegistration {
        source,
        variant,
        partial,
    } = attr.parse_args()?;
    let mut case = source.clone();
    // Infer the enum's arguments from the match scrutinee. Explicit lifetime
    // arguments are not permitted on variant patterns.
    case.segments.last_mut().expect("source path").arguments = syn::PathArguments::None;
    case.segments.push(variant.into());
    let mut bindings = Vec::new();
    let mut assignments = Vec::new();
    let mut sources = std::collections::HashSet::new();
    for (i, field) in fields.named.iter().enumerate() {
        let dest = field.ident.as_ref().expect("named field");
        let mut attrs = field.attrs.iter().filter(|a| a.path().is_ident("lift"));
        let from = match attrs.next() {
            Some(attr) => attr.parse_args::<SourceField>()?.0,
            None => dest.clone(),
        };
        if let Some(extra) = attrs.next() {
            return Err(syn::Error::new_spanned(
                extra,
                "duplicate field lift mapping",
            ));
        }
        if !sources.insert(from.to_string().trim_start_matches("r#").to_owned()) {
            return Err(syn::Error::new_spanned(
                from,
                "source field mapped more than once",
            ));
        }
        let binding = format_ident!("__lift_field_{i}");
        bindings.push(quote! { #from: #binding });
        assignments.push(quote! { #dest: #binding });
    }
    // No `..`: rustc checks the complete source payload, including missing
    // fields and shape/type mismatches. No fields are silently discarded.
    let arm = quote! { #case { #(#bindings),* } => Ok(Self { #(#assignments),* }) };
    Ok(conversion_impl(input, &source, partial, &[arm]))
}

/// A newtype explicitly projects one field from a source struct. Unlike the
/// named-field enum-variant mapping, this intentionally drops the other fields.
struct NewtypeRegistration {
    source: Path,
    field: syn::Ident,
}

impl Parse for NewtypeRegistration {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let source: Path = input.parse()?;
        let mut field = None;
        while input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
            if input.is_empty() {
                break;
            }
            let key: syn::Ident = input.parse()?;
            if key != "field" {
                return Err(syn::Error::new_spanned(
                    key,
                    "a tuple struct lift supports only `field = name`",
                ));
            }
            if field.is_some() {
                return Err(syn::Error::new_spanned(key, "duplicate `field`"));
            }
            input.parse::<Token![=]>()?;
            field = Some(input.parse()?);
        }
        let field = field.ok_or_else(|| {
            syn::Error::new_spanned(&source, "a tuple struct lift requires `field = name`")
        })?;
        Ok(Self { source, field })
    }
}

fn derive_newtype(
    input: &syn::DeriveInput,
    fields: &syn::FieldsUnnamed,
) -> syn::Result<TokenStream> {
    if fields.unnamed.len() != 1 {
        return Err(syn::Error::new_spanned(
            fields,
            "a tuple struct lift requires exactly one field",
        ));
    }
    for field in &fields.unnamed {
        if let Some(attr) = field.attrs.iter().find(|a| a.path().is_ident("lift")) {
            return Err(syn::Error::new_spanned(
                attr,
                "put the field projection on the struct: #[lift(Source, field = name)]",
            ));
        }
    }
    let mut attrs = input.attrs.iter().filter(|a| a.path().is_ident("lift"));
    let attr = attrs.next().ok_or_else(|| {
        syn::Error::new_spanned(
            &input.ident,
            "a tuple struct lift requires #[lift(Source, field = name)]",
        )
    })?;
    if let Some(extra) = attrs.next() {
        return Err(syn::Error::new_spanned(
            extra,
            "a struct lift supports exactly one source registration",
        ));
    }
    let NewtypeRegistration { source, field } = attr.parse_args()?;
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    Ok(quote! {
        impl #impl_generics From<#source> for #name #ty_generics #where_clause {
            fn from(source: #source) -> Self {
                Self(source.#field)
            }
        }
    })
}