derive_more-impl 2.1.1

Internal implementation of `derive_more` crate
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
//! Implementation of a [`FromStr`] derive macro.

#[cfg(doc)]
use std::str::FromStr;
use std::{collections::HashMap, iter};

use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{parse::Parse, parse_quote, spanned::Spanned as _};

use crate::utils::{
    attr::{self, ParseMultiple as _},
    Either, GenericsSearch, Spanning,
};

/// Expands a [`FromStr`] derive macro.
pub fn expand(input: &syn::DeriveInput, _: &'static str) -> syn::Result<TokenStream> {
    match &input.data {
        syn::Data::Struct(data) => Ok(if data.fields.is_empty() {
            FlatExpansion::try_from(input)?.into_token_stream()
        } else {
            ForwardExpansion::try_from(input)?.into_token_stream()
        }),
        syn::Data::Enum(_) => Ok(FlatExpansion::try_from(input)?.into_token_stream()),
        syn::Data::Union(data) => Err(syn::Error::new(
            data.union_token.span(),
            "`FromStr` cannot be derived for unions",
        )),
    }
}

/// Expansion of a macro for generating a forwarding [`FromStr`] implementation of a struct.
struct ForwardExpansion<'i> {
    /// [`syn::Ident`] and [`syn::Generics`] of the struct.
    ///
    /// [`syn::Ident`]: struct@syn::Ident
    self_ty: (&'i syn::Ident, &'i syn::Generics),

    /// [`syn::Field`] representing the wrapped type to forward implementation on.
    inner: &'i syn::Field,

    /// Optional [`attr::Error`] enabling conversion into a custom error type.
    custom_error: Option<attr::Error>,
}

impl<'i> TryFrom<&'i syn::DeriveInput> for ForwardExpansion<'i> {
    type Error = syn::Error;

    fn try_from(input: &'i syn::DeriveInput) -> syn::Result<Self> {
        let syn::Data::Struct(data) = &input.data else {
            return Err(syn::Error::new(
                input.span(),
                "expected a struct for forward `FromStr` derive",
            ));
        };

        // TODO: Unite these two conditions via `&&` once MSRV is bumped to 1.88 or above.
        if data.fields.len() != 1 {
            return Err(syn::Error::new(
                data.fields.span(),
                "only structs with single field can derive `FromStr`",
            ));
        }
        let Some(inner) = data.fields.iter().next() else {
            return Err(syn::Error::new(
                data.fields.span(),
                "only structs with single field can derive `FromStr`",
            ));
        };

        let custom_error =
            attr::Error::parse_attrs(&input.attrs, &format_ident!("from_str"))?
                .map(Spanning::into_inner);

        Ok(Self {
            self_ty: (&input.ident, &input.generics),
            inner,
            custom_error,
        })
    }
}

impl ToTokens for ForwardExpansion<'_> {
    /// Expands a forwarding [`FromStr`] implementations for a struct.
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let inner_ty = &self.inner.ty;
        let ty = self.self_ty.0;

        let generics_search = GenericsSearch::from(self.self_ty.1);
        let mut generics = self.self_ty.1.clone();
        if generics_search.any_in(inner_ty) {
            generics.make_where_clause().predicates.push(parse_quote! {
                #inner_ty: derive_more::core::str::FromStr
            });
        }
        let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

        let constructor = self.inner.self_constructor([parse_quote! { v }]);

        let mut error_ty =
            quote! { <#inner_ty as derive_more::core::str::FromStr>::Err };
        let mut error_conv = quote! {};
        if let Some(custom_error) = &self.custom_error {
            error_ty = custom_error.ty.to_token_stream();
            error_conv = custom_error.conv.as_ref().map_or_else(
                || quote! { .map_err(derive_more::core::convert::Into::into) },
                |conv| quote! { .map_err(#conv) },
            );
        }

        quote! {
            #[automatically_derived]
            impl #impl_generics derive_more::core::str::FromStr for #ty #ty_generics #where_clause {
                type Err = #error_ty;

                #[inline]
                fn from_str(s: &str) -> derive_more::core::result::Result<Self, Self::Err> {
                    derive_more::core::str::FromStr::from_str(s).map(|v| #constructor)#error_conv
                }
            }
        }.to_tokens(tokens);
    }
}

/// Expansion of a macro for generating a flat [`FromStr`] implementation of an enum or a struct.
struct FlatExpansion<'i> {
    /// [`syn::Ident`] and [`syn::Generics`] of the enum/struct.
    ///
    /// [`syn::Ident`]: struct@syn::Ident
    self_ty: (&'i syn::Ident, &'i syn::Generics),

    /// [`syn::Ident`]s along with the matched values (enum variants or struct itself), and
    /// a value-specific [`attr::RenameAll`] overriding [`FlatExpansion::rename_all`], if any.
    ///
    /// [`syn::Ident`]: struct@syn::Ident
    matches: Vec<(
        &'i syn::Ident,
        Either<&'i syn::DataStruct, &'i syn::Variant>,
        Option<attr::RenameAll>,
    )>,

    /// [`FlatExpansion::matches`] grouped by its similar representation for detecting whether their
    /// case-insensitivity should be disabled.
    similar_matches: HashMap<String, Vec<&'i syn::Ident>>,

    /// Optional [`attr::RenameAll`] indicating the case conversion to be applied to all the matched
    /// values (enum variants or struct itself).
    rename_all: Option<attr::RenameAll>,

    /// Optional [`attr::Error`] enabling conversion into a custom error type.
    custom_error: Option<attr::Error>,
}

impl<'i> TryFrom<&'i syn::DeriveInput> for FlatExpansion<'i> {
    type Error = syn::Error;

    fn try_from(input: &'i syn::DeriveInput) -> syn::Result<Self> {
        let attr_ident = &format_ident!("from_str");

        let matches = match &input.data {
            syn::Data::Struct(data) => {
                if !data.fields.is_empty() {
                    return Err(syn::Error::new(
                        data.fields.span(),
                        "only structs with no fields can derive `FromStr`",
                    ));
                }
                vec![(&input.ident, Either::Left(data), None)]
            }
            syn::Data::Enum(data) => data
                .variants
                .iter()
                .map(|variant| {
                    if !variant.fields.is_empty() {
                        return Err(syn::Error::new(
                            variant.fields.span(),
                            "only enums with no fields can derive `FromStr`",
                        ));
                    }
                    let attr =
                        attr::RenameAll::parse_attrs(&variant.attrs, attr_ident)?
                            .map(Spanning::into_inner);
                    Ok((&variant.ident, Either::Right(variant), attr))
                })
                .collect::<syn::Result<_>>()?,
            syn::Data::Union(_) => {
                return Err(syn::Error::new(
                    input.span(),
                    "expected an enum or a struct for flat `FromStr` derive",
                ))
            }
        };

        let FlatContainerAttributes {
            rename_all,
            error: custom_error,
        } = FlatContainerAttributes::parse_attrs(&input.attrs, attr_ident)?
            .map(Spanning::into_inner)
            .unwrap_or_default();

        let mut similar_matches = <HashMap<_, Vec<_>>>::new();
        if rename_all.is_none() {
            for (ident, _, renaming) in &matches {
                let name = ident.to_string();
                let lowercased = name.to_lowercase();
                if let Some(rename) = renaming {
                    let renamed_lowercased = rename.convert_case(&name);
                    if renamed_lowercased != lowercased {
                        similar_matches
                            .entry(renamed_lowercased)
                            .or_default()
                            .push(*ident);
                    }
                }
                similar_matches.entry(lowercased).or_default().push(*ident);
            }
        }

        let mut exact_matches = <HashMap<String, Vec<String>>>::new();
        for (ident, _, renaming) in &matches {
            let name = ident.to_string();
            let exact = if let Some(default_renaming) = &rename_all {
                renaming
                    .as_ref()
                    .unwrap_or(default_renaming)
                    .convert_case(&name)
            } else if let Some(renaming) = renaming {
                renaming.convert_case(&name)
            } else {
                let lowercased = name.to_lowercase();
                if similar_matches[&lowercased].len() > 1 {
                    name.clone()
                } else {
                    lowercased
                }
            };
            exact_matches.entry(exact).or_default().push(name);
        }
        if let Some((string, variants)) =
            exact_matches.into_iter().find(|(_, vs)| vs.len() > 1)
        {
            return Err(syn::Error::new(
                input.ident.span(),
                format!(
                    "`{}` variants cannot have the same \"{string}\" string representation",
                    variants.join("`, `"),
                ),
            ));
        }

        Ok(Self {
            self_ty: (&input.ident, &input.generics),
            matches,
            similar_matches,
            rename_all,
            custom_error,
        })
    }
}

impl ToTokens for FlatExpansion<'_> {
    /// Expands a flat [`FromStr`] implementations for an enum.
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let ty = self.self_ty.0;
        let (impl_generics, ty_generics, where_clause) =
            self.self_ty.1.split_for_impl();
        let ty_name = ty.to_string();

        let scrutinee_lowercased = self
            .rename_all
            .is_none()
            .then(|| quote! { .to_lowercase().as_str() });
        let match_arms = if let Some(default_renaming) = self.rename_all {
            self.matches
                .iter()
                .map(|(ident, value, renaming)| {
                    let converted = renaming
                        .unwrap_or(default_renaming)
                        .convert_case(&ident.to_string());
                    let constructor = value.self_constructor_empty();

                    quote! { #converted => #constructor, }
                })
                .collect::<Vec<_>>()
        } else {
            self.matches
                .iter()
                .map(|(ident, value, renaming)| {
                    let name = ident.to_string();
                    let constructor = value.self_constructor_empty();
                    if let Some(rename) = renaming {
                        let exact_name = rename.convert_case(&name);

                        quote! { _ if s == #exact_name => #constructor, }
                    } else {
                        let lowercased = name.to_lowercase();
                        let exact_guard = (self.similar_matches[&lowercased].len() > 1)
                            .then(|| quote! { if s == #name });

                        quote! { #lowercased #exact_guard => #constructor, }
                    }
                })
                .collect()
        };

        let default_error = quote! { derive_more::FromStrError::new(#ty_name) };
        let mut error_ty = quote! { derive_more::FromStrError };
        let mut error_val = default_error.clone();
        if let Some(custom_error) = &self.custom_error {
            error_ty = custom_error.ty.to_token_stream();
            error_val = custom_error.conv.as_ref().map_or_else(
                || quote! { derive_more::core::convert::Into::into(#default_error) },
                |conv| quote! { (#conv)(#default_error) },
            );
        }

        quote! {
            #[allow(unreachable_code)] // for empty enums
            #[automatically_derived]
            impl #impl_generics derive_more::core::str::FromStr for #ty #ty_generics #where_clause {
                type Err = #error_ty;

                fn from_str(s: &str) -> derive_more::core::result::Result<
                    Self, <Self as derive_more::core::str::FromStr>::Err,
                > {
                    derive_more::core::result::Result::Ok(match s #scrutinee_lowercased {
                        #( #match_arms )*
                        _ => return derive_more::core::result::Result::Err(#error_val),
                    })
                }
            }
        }.to_tokens(tokens);
    }
}

/// Extension of [`syn::Fields`] used by this expansion.
trait FieldsExt {
    /// Generates a `name`d constructor with the provided `values` assigned to these
    /// [`syn::Fields`].
    ///
    /// # Panics
    ///
    /// If number of provided `values` doesn't match number of these [`syn::Fields`].
    fn constructor(
        &self,
        name: &syn::Path,
        values: impl IntoIterator<Item = syn::Ident>,
    ) -> TokenStream;

    /// Generates a `Self` type constructor with the provided `values` assigned to these
    /// [`syn::Fields`].
    ///
    /// # Panics
    ///
    /// If number of provided `values` doesn't match number of these [`syn::Fields`].
    fn self_constructor(
        &self,
        values: impl IntoIterator<Item = syn::Ident>,
    ) -> TokenStream {
        self.constructor(&self.self_ty(), values)
    }

    /// Generates a `Self` type constructor with no fields.
    ///
    /// # Panics
    ///
    /// If these [`syn::Fields`] are not [empty].
    ///
    /// [empty]: syn::Fields::is_empty
    fn self_constructor_empty(&self) -> TokenStream {
        self.self_constructor(iter::empty())
    }

    /// Returns a [`syn::Path`] representing a `Self` type of these [`syn::Fields`].
    fn self_ty(&self) -> syn::Path {
        parse_quote! { Self }
    }
}

impl FieldsExt for syn::Fields {
    fn constructor(
        &self,
        name: &syn::Path,
        values: impl IntoIterator<Item = syn::Ident>,
    ) -> TokenStream {
        let values = values.into_iter();
        let fields = match self {
            Self::Named(fields) => {
                let initializers = fields.named.iter().zip(values).map(|(f, value)| {
                    let ident = &f.ident;
                    quote! { #ident: #value }
                });
                Some(quote! { { #( #initializers, )*} })
            }
            Self::Unnamed(_) => Some(quote! { ( #( #values, )* ) }),
            Self::Unit => None,
        };
        quote! { #name #fields }
    }
}

impl FieldsExt for syn::Field {
    fn constructor(
        &self,
        name: &syn::Path,
        values: impl IntoIterator<Item = syn::Ident>,
    ) -> TokenStream {
        let mut values = values.into_iter();
        let value = values.next().expect("expected a single value");
        if values.next().is_some() {
            panic!("expected a single value");
        }

        if let Some(ident) = &self.ident {
            quote! { #name { #ident: #value } }
        } else {
            quote! { #name(#value) }
        }
    }
}

impl FieldsExt for syn::Variant {
    fn constructor(
        &self,
        name: &syn::Path,
        values: impl IntoIterator<Item = syn::Ident>,
    ) -> TokenStream {
        self.fields.constructor(name, values)
    }

    fn self_ty(&self) -> syn::Path {
        let variant = &self.ident;

        parse_quote! { Self::#variant }
    }
}

impl FieldsExt for syn::DataStruct {
    fn constructor(
        &self,
        name: &syn::Path,
        values: impl IntoIterator<Item = syn::Ident>,
    ) -> TokenStream {
        self.fields.constructor(name, values)
    }
}

impl<L: FieldsExt, R: FieldsExt> FieldsExt for Either<&L, &R> {
    fn constructor(
        &self,
        name: &syn::Path,
        values: impl IntoIterator<Item = syn::Ident>,
    ) -> TokenStream {
        match self {
            Self::Left(l) => l.constructor(name, values),
            Self::Right(r) => r.constructor(name, values),
        }
    }

    fn self_ty(&self) -> syn::Path {
        match self {
            Self::Left(l) => l.self_ty(),
            Self::Right(r) => r.self_ty(),
        }
    }
}

/// Representation of possible [`FromStr`] derive macro attributes placed on an enum or a struct for
/// a [`FlatExpansion`].
///
/// ```rust,ignore
/// #[<attribute>(rename_all = "<casing>")]
/// #[<attribute>(error(<ty>))]
/// #[<attribute>(error(<ty>, <conv>))]
/// ```
///
/// Both `#[<attribute>(rename_all = "<casing>")]` and `#[<attribute>(error(<ty>[, <conv>]))]` can
/// be specified only once.
#[derive(Default)]
struct FlatContainerAttributes {
    /// [`attr::RenameAll`] for case conversion.
    rename_all: Option<attr::RenameAll>,

    /// [`attr::Error`] for conversion into a custom error type.
    error: Option<attr::Error>,
}

impl Parse for FlatContainerAttributes {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        mod ident {
            use syn::custom_keyword;

            custom_keyword!(error);
            custom_keyword!(rename_all);
        }

        let ahead = input.lookahead1();
        if ahead.peek(ident::error) {
            Ok(Self {
                error: Some(input.parse()?),
                ..Default::default()
            })
        } else if ahead.peek(ident::rename_all) {
            Ok(Self {
                rename_all: Some(input.parse()?),
                ..Default::default()
            })
        } else {
            Err(ahead.error())
        }
    }
}

impl attr::ParseMultiple for FlatContainerAttributes {
    fn merge_attrs(
        prev: Spanning<Self>,
        new: Spanning<Self>,
        name: &syn::Ident,
    ) -> syn::Result<Spanning<Self>> {
        let Spanning {
            span: prev_span,
            item: mut prev,
        } = prev;
        let Spanning {
            span: new_span,
            item: new,
        } = new;

        if new
            .rename_all
            .and_then(|n| prev.rename_all.replace(n))
            .is_some()
        {
            return Err(syn::Error::new(
                new_span,
                format!("multiple `#[{name}(rename_all=\"...\")]` attributes aren't allowed"),
            ));
        }

        if prev.error.is_some() && new.error.is_some() {
            return Err(syn::Error::new(
                new_span,
                format!(
                    "multiple `#[{name}(error(\"...\")]` attributes aren't allowed",
                ),
            ));
        }

        prev.error = prev.error.or(new.error);

        Ok(Spanning::new(
            prev,
            prev_span.join(new_span).unwrap_or(prev_span),
        ))
    }
}