| 1 | // Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | // |
| 3 | // This source code is licensed under the MIT license found in the |
| 4 | // LICENSE file in the root directory of this source tree |
| 5 | |
| 6 | use proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#; |
| 7 | use quoterust-analyzer cargo quote 1.0.40 crate/::{quoterust-analyzer cargo quote 1.0.40 quote!, quote_spannedrust-analyzer cargo quote 1.0.40 quote_spanned!}; |
| 8 | use synrust-analyzer cargo syn 2.0.101 crate/::{ |
| 9 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/, spannedrust-analyzer cargo syn 2.0.101 spanned/::Spannedrust-analyzer cargo syn 2.0.101 spanned/Spanned#, Datarust-analyzer cargo syn 2.0.101 derive/Data#, DataEnumrust-analyzer cargo syn 2.0.101 derive/DataEnum#, DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#, Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#, FieldsUnnamedrust-analyzer cargo syn 2.0.101 data/FieldsUnnamed#, Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, |
| 10 | }; |
| 11 | |
| 12 | pub(craterust-analyzer cargo libcst_derive 1.8.6 crate/) fn impl_parenthesized_noderust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/impl_parenthesized_node().(ast: &DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#, deflated: bool) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 13 | match &ast.datarust-analyzer cargo syn 2.0.101 derive/DeriveInput#data. { |
| 14 | Datarust-analyzer cargo syn 2.0.101 derive/Data#::Enumrust-analyzer cargo syn 2.0.101 derive/Data#Enum#(e) => impl_enumrust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/impl_enum().(ast, e, deflated), |
| 15 | Datarust-analyzer cargo syn 2.0.101 derive/Data#::Structrust-analyzer cargo syn 2.0.101 derive/Data#Struct#(_) => impl_structrust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/impl_struct().(ast, deflated), |
| 16 | Datarust-analyzer cargo syn 2.0.101 derive/Data#::Unionrust-analyzer cargo syn 2.0.101 derive/Data#Union#(u) => quote_spannedrust-analyzer cargo quote 1.0.40 quote_spanned!! { |
| 17 | u.union_tokenrust-analyzer cargo syn 2.0.101 derive/DataUnion#union_token..spanrust-analyzer cargo syn 2.0.101 spanned/impl#[T][Spanned]span().() => |
| 18 | compile_error!("Union type is not supported") |
| 19 | } |
| 20 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | fn identsrust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/idents().(deflated: bool) -> (Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, Identrust-analyzer cargo proc-macro2 1.0.95 Ident#) { |
| 25 | let treyt: Identrust-analyzer cargo proc-macro2 1.0.95 Ident# = if deflated { |
| 26 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(ParenthesizedDeflatedNode) |
| 27 | } else { |
| 28 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(ParenthesizedNode) |
| 29 | }; |
| 30 | let leftparen: Identrust-analyzer cargo proc-macro2 1.0.95 Ident# = if deflated { |
| 31 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(DeflatedLeftParen) |
| 32 | } else { |
| 33 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(LeftParen) |
| 34 | }; |
| 35 | let rightparen: Identrust-analyzer cargo proc-macro2 1.0.95 Ident# = if deflated { |
| 36 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(DeflatedRightParen) |
| 37 | } else { |
| 38 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(RightParen) |
| 39 | }; |
| 40 | (treyt, leftparen, rightparen) |
| 41 | } |
| 42 | |
| 43 | fn impl_structrust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/impl_struct().(ast: &DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#, deflated: bool) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 44 | let ident = &ast.identrust-analyzer cargo syn 2.0.101 derive/DeriveInput#ident.; |
| 45 | let generics = if deflated { |
| 46 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(<'r, 'a>) |
| 47 | } else { |
| 48 | ast.genericsrust-analyzer cargo syn 2.0.101 derive/DeriveInput#generics..clonerust-analyzer cargo syn 2.0.101 gen/clone/impl#[`crate::Generics`][Clone]clone().() |
| 49 | }; |
| 50 | |
| 51 | let (treyt, leftparen, rightparen) = identsrust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/idents().(deflated); |
| 52 | let gen = quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 53 | impl#generics #treyt#generics for #ident #generics { |
| 54 | fn lpar(&self) -> &Vec<#leftparen#generics> { |
| 55 | &self.lpar |
| 56 | } |
| 57 | fn rpar(&self) -> &Vec<#rightparen#generics> { |
| 58 | &self.rpar |
| 59 | } |
| 60 | fn with_parens(self, left: #leftparen#generics, right: #rightparen#generics) -> Self { |
| 61 | let mut lpar = self.lpar; |
| 62 | let mut rpar = self.rpar; |
| 63 | lpar.insert(0, left); |
| 64 | rpar.push(right); |
| 65 | #[allow(clippy::needless_update)] |
| 66 | Self { lpar, rpar, ..self } |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | gen.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 71 | } |
| 72 | |
| 73 | fn impl_enumrust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/impl_enum().(ast: &DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#, e: &DataEnumrust-analyzer cargo syn 2.0.101 derive/DataEnum#, deflated: bool) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 74 | let mut varnames = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 75 | for var in e.variantsrust-analyzer cargo syn 2.0.101 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.101 punctuated/impl#[`Punctuated<T, P>`]iter().() { |
| 76 | match &var.fieldsrust-analyzer cargo syn 2.0.101 data/Variant#fields. { |
| 77 | Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Namedrust-analyzer cargo syn 2.0.101 data/Fields#Named#(n) => { |
| 78 | return quote_spannedrust-analyzer cargo quote 1.0.40 quote_spanned!! { |
| 79 | n.spanrust-analyzer cargo syn 2.0.101 spanned/impl#[T][Spanned]span().() => |
| 80 | compile_error!("Named enum fields not supported") |
| 81 | } |
| 82 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 83 | } |
| 84 | f @ Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Unitrust-analyzer cargo syn 2.0.101 data/Fields#Unit# => { |
| 85 | return quote_spannedrust-analyzer cargo quote 1.0.40 quote_spanned!! { |
| 86 | f.spanrust-analyzer cargo syn 2.0.101 spanned/impl#[T][Spanned]span().() => |
| 87 | compile_error!("Empty enum variants not supported") |
| 88 | } |
| 89 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 90 | } |
| 91 | Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.101 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.101 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.101 data/FieldsUnnamed#unnamed., .. }) => { |
| 92 | if unnamed.lenrust-analyzer cargo syn 2.0.101 punctuated/impl#[`Punctuated<T, P>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 { |
| 93 | return quote_spannedrust-analyzer cargo quote 1.0.40 quote_spanned!! { |
| 94 | unnamed.spanrust-analyzer cargo syn 2.0.101 spanned/impl#[T][Spanned]span().() => |
| 95 | compile_error!("Multiple unnamed fields not supported") |
| 96 | } |
| 97 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 98 | } |
| 99 | varnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(&var.identrust-analyzer cargo syn 2.0.101 data/Variant#ident.); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | let ident = &ast.identrust-analyzer cargo syn 2.0.101 derive/DeriveInput#ident.; |
| 104 | let generics = if deflated { |
| 105 | parse_quoterust-analyzer cargo syn 2.0.101 parse_quote/parse_quote!!(<'r, 'a>) |
| 106 | } else { |
| 107 | ast.genericsrust-analyzer cargo syn 2.0.101 derive/DeriveInput#generics..clonerust-analyzer cargo syn 2.0.101 gen/clone/impl#[`crate::Generics`][Clone]clone().() |
| 108 | }; |
| 109 | let (treyt, leftparen, rightparen) = identsrust-analyzer cargo libcst_derive 1.8.6 parenthesized_node/idents().(deflated); |
| 110 | let gen = quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 111 | impl#generics #treyt#generics for #ident #generics { |
| 112 | fn lpar(&self) -> &Vec<#leftparen#generics> { |
| 113 | match self { |
| 114 | #(Self::#varnames(x) => x.lpar(),)* |
| 115 | } |
| 116 | } |
| 117 | fn rpar(&self) -> &Vec<#rightparen#generics> { |
| 118 | match self { |
| 119 | #(Self::#varnames(x) => x.rpar(),)* |
| 120 | } |
| 121 | } |
| 122 | fn with_parens(self, left: #leftparen#generics, right: #rightparen#generics) -> Self { |
| 123 | match self { |
| 124 | #(Self::#varnames(x) => Self::#varnames(x.with_parens(left, right)),)* |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | }; |
| 129 | gen.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 130 | } |