# ast.rs - source
Source of the Rust file `/home/justine/.cargo/registry/src/github.com-1ecc6299db9ec823/thiserror-impl-1.0.38/src/ast.rs`.
Keywords: rust, rustlang, rust-lang

    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

    use crate::attr::{self, Attrs};
    use crate::generics::ParamsInScope;
    use proc_macro2::Span;
    use syn::{
        Data, DataEnum, DataStruct, DeriveInput, Error, Fields, Generics, Ident, Index, Member, Result,
        Type,
    };

    pub enum Input<'a> {
        Struct(Struct<'a>),
        Enum(Enum<'a>),
    }

    pub struct Struct<'a> {
        pub original: &'a DeriveInput,
        pub attrs: Attrs<'a>,
        pub ident: Ident,
        pub generics: &'a Generics,
        pub fields: Vec<Field<'a>>,
    }

    pub struct Enum<'a> {
        pub original: &'a DeriveInput,
        pub attrs: Attrs<'a>,
        pub ident: Ident,
        pub generics: &'a Generics,
        pub variants: Vec<Variant<'a>>,
    }

    pub struct Variant<'a> {
        pub original: &'a syn::Variant,
        pub attrs: Attrs<'a>,
        pub ident: Ident,
        pub fields: Vec<Field<'a>>,
    }

    pub struct Field<'a> {
        pub original: &'a syn::Field,
        pub attrs: Attrs<'a>,
        pub member: Member,
        pub ty: &'a Type,
        pub contains_generic: bool,
    }

    impl<'a> Input<'a> {
        pub fn from_syn(node: &'a DeriveInput) -> Result<Self> {
            match &node.data {
                Data::Struct(data) => Struct::from_syn(node, data).map(Input::Struct),
                Data::Enum(data) => Enum::from_syn(node, data).map(Input::Enum),
                Data::Union(_) => Err(Error::new_spanned(
                    node,
                    "union as errors are not supported",
                )),
            }
        }
    }

    impl<'a> Struct<'a> {
        fn from_syn(node: &'a DeriveInput, data: &'a DataStruct) -> Result<Self> {
            let mut attrs = attr::get(&node.attrs)?;
            let scope = ParamsInScope::new(&node.generics);
            let span = attrs.span().unwrap_or_else(Span::call_site);
            let fields = Field::multiple_from_syn(&data.fields, &scope, span)?;
            if let Some(display) = &mut attrs.display {
                display.expand_shorthand(&fields);
            }
            Ok(Struct {
                original: node,
                attrs,
                ident: node.ident.clone(),
                generics: &node.generics,
                fields,
            })
        }
    }

    impl<'a> Enum<'a> {
        fn from_syn(node: &'a DeriveInput, data: &'a DataEnum) -> Result<Self> {
            let attrs = attr::get(&node.attrs)?;
            let scope = ParamsInScope::new(&node.generics);
            let span = attrs.span().unwrap_or_else(Span::call_site);
            let variants = data
                .variants
                .iter()
                .map(|node| {
                    let mut variant = Variant::from_syn(node, &scope, span)?;
                    if let display @ None = &mut variant.attrs.display {
                        *display = attrs.display.clone();
                    }
                    if let Some(display) = &mut variant.attrs.display {
                        display.expand_shorthand(&variant.fields);
                    } else if variant.attrs.transparent.is_none() {
                        variant.attrs.transparent = attrs.transparent;
                    }
                    Ok(variant)
                })
                .collect::<Result<_>>()?;
            Ok(Enum {
                original: node,
                attrs,
                ident: node.ident.clone(),
                generics: &node.generics,
                variants,
            })
        }
    }

    impl<'a> Variant<'a> {
        fn from_syn(node: &'a syn::Variant, scope: &ParamsInScope<'a>, span: Span) -> Result<Self> {
            let attrs = attr::get(&node.attrs)?;
            let span = attrs.span().unwrap_or(span);
            Ok(Variant {
                original: node,
                attrs,
                ident: node.ident.clone(),
                fields: Field::multiple_from_syn(&node.fields, scope, span)?,
            })
        }
    }

    impl<'a> Field<'a> {
        fn multiple_from_syn(
            fields: &'a Fields,
            scope: &ParamsInScope<'a>,
            span: Span,
        ) -> Result<Vec<Self>> {
            fields
                .iter()
                .enumerate()
                .map(|(i, field)| Field::from_syn(i, field, scope, span))
                .collect()
        }

        fn from_syn(
            i: usize,
            node: &'a syn::Field,
            scope: &ParamsInScope<'a>,
            span: Span,
        ) -> Result<Self> {
            Ok(Field {
                original: node,
                attrs: attr::get(&node.attrs)?,
                member: node.ident.clone().map(Member::Named).unwrap_or_else(|| {
                    Member::Unnamed(Index {
                        index: i as u32,
                        span,
                    })
                }),
                ty: &node.ty,
                contains_generic: scope.intersects(&node.ty),
            })
        }
    }

    impl Attrs<'_> {
        pub fn span(&self) -> Option<Span> {
            if let Some(display) = &self.display {
                Some(display.fmt.span())
            } else if let Some(transparent) = &self.transparent {
                Some(transparent.span)
            } else {
                None
            }
        }
    }