| 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/::{format_identrust-analyzer cargo quote 1.0.40 format/format_ident!, quoterust-analyzer cargo quote 1.0.40 quote!, quote_spannedrust-analyzer cargo quote 1.0.40 quote_spanned!, ToTokensrust-analyzer cargo quote 1.0.40 to_tokens/ToTokens#}; |
| 8 |
use synrust-analyzer cargo syn 2.0.101 crate/::{ |
| 9 |
spannedrust-analyzer cargo syn 2.0.101 spanned/::Spannedrust-analyzer cargo syn 2.0.101 spanned/Spanned#, Attributerust-analyzer cargo syn 2.0.101 attr/Attribute#, Datarust-analyzer cargo syn 2.0.101 derive/Data#, DataEnumrust-analyzer cargo syn 2.0.101 derive/DataEnum#, DataStructrust-analyzer cargo syn 2.0.101 derive/DataStruct#, DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#, Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#, FieldsNamedrust-analyzer cargo syn 2.0.101 data/FieldsNamed#, |
| 10 |
FieldsUnnamedrust-analyzer cargo syn 2.0.101 data/FieldsUnnamed#, Typerust-analyzer cargo syn 2.0.101 ty/Type#, TypePathrust-analyzer cargo syn 2.0.101 ty/TypePath#, Visibilityrust-analyzer cargo syn 2.0.101 restriction/Visibility#, |
| 11 |
}; |
| 12 |
|
| 13 |
pub(craterust-analyzer cargo libcst_derive 1.8.6 crate/) fn impl_into_pyrust-analyzer cargo libcst_derive 1.8.6 into_py/impl_into_py().(ast: &DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 14 |
match &ast.datarust-analyzer cargo syn 2.0.101 derive/DeriveInput#data. { |
| 15 |
Datarust-analyzer cargo syn 2.0.101 derive/Data#::Enumrust-analyzer cargo syn 2.0.101 derive/Data#Enum#(e) => impl_into_py_enumrust-analyzer cargo libcst_derive 1.8.6 into_py/impl_into_py_enum().(ast, e), |
| 16 |
Datarust-analyzer cargo syn 2.0.101 derive/Data#::Structrust-analyzer cargo syn 2.0.101 derive/Data#Struct#(s) => impl_into_py_structrust-analyzer cargo libcst_derive 1.8.6 into_py/impl_into_py_struct().(ast, s), |
| 17 |
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!! { |
| 18 |
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().() => |
| 19 |
compile_error!("Union type is not supported") |
| 20 |
} |
| 21 |
.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
fn impl_into_py_enumrust-analyzer cargo libcst_derive 1.8.6 into_py/impl_into_py_enum().(ast: &DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#, e: &DataEnumrust-analyzer cargo syn 2.0.101 derive/DataEnum#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 26 |
let mut toks = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 27 |
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().() { |
| 28 |
let varname = &var.identrust-analyzer cargo syn 2.0.101 data/Variant#ident.; |
| 29 |
match &var.fieldsrust-analyzer cargo syn 2.0.101 data/Variant#fields. { |
| 30 |
Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Namedrust-analyzer cargo syn 2.0.101 data/Fields#Named#(n) => { |
| 31 |
let mut fieldnames = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 32 |
for field in n.namedrust-analyzer cargo syn 2.0.101 data/FieldsNamed#named..iterrust-analyzer cargo syn 2.0.101 punctuated/impl#[`Punctuated<T, P>`]iter().() { |
| 33 |
if has_attrrust-analyzer cargo libcst_derive 1.8.6 into_py/has_attr().(&field.attrsrust-analyzer cargo syn 2.0.101 data/Field#attrs., "skip_py") { |
| 34 |
continue; |
| 35 |
} |
| 36 |
fieldnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(field.identrust-analyzer cargo syn 2.0.101 data/Field#ident..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 37 |
} |
| 38 |
let kwargs_toks = fields_to_kwargsrust-analyzer cargo libcst_derive 1.8.6 into_py/fields_to_kwargs().(&var.fieldsrust-analyzer cargo syn 2.0.101 data/Variant#fields., true); |
| 39 |
toks.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 40 |
Self::#varname { #(#fieldnames,)* .. } => { |
| 41 |
use pyo3::types::PyAnyMethods; |
| 42 |
|
| 43 |
let libcst = pyo3::types::PyModule::import(py, "libcst")?; |
| 44 |
let kwargs = #kwargs_toks ; |
| 45 |
Ok(libcst |
| 46 |
.getattr(stringify!(#varname)) |
| 47 |
.expect(stringify!(no #varname found in libcst)) |
| 48 |
.call((), Some(&kwargs))? |
| 49 |
.into()) |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
f @ Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Unitrust-analyzer cargo syn 2.0.101 data/Fields#Unit# => { |
| 54 |
return quote_spannedrust-analyzer cargo quote 1.0.40 quote_spanned!! { |
| 55 |
f.spanrust-analyzer cargo syn 2.0.101 spanned/impl#[T][Spanned]span().() => |
| 56 |
compile_error!("Empty enum variants not supported") |
| 57 |
} |
| 58 |
.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 59 |
} |
| 60 |
Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.101 data/Fields#Unnamed#(_) => { |
| 61 |
toks.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 62 |
Self::#varname(x, ..) => x.try_into_py(py), |
| 63 |
}); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
let ident = &ast.identrust-analyzer cargo syn 2.0.101 derive/DeriveInput#ident.; |
| 68 |
let generics = &ast.genericsrust-analyzer cargo syn 2.0.101 derive/DeriveInput#generics.; |
| 69 |
let gen = quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 70 |
use pyo3::types::IntoPyDict as _; |
| 71 |
#[automatically_derived] |
| •72 |
impl#generics crate::nodes::traits::py::TryIntoPy<pyo3::Py<pyo3::PyAny>> for #ident #generics { |
| 73 |
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<pyo3::PyAny>> { |
| 74 |
match self { |
| 75 |
#(#toks)* |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
}; |
| 80 |
gen.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 81 |
} |
| 82 |
|
| 83 |
fn impl_into_py_structrust-analyzer cargo libcst_derive 1.8.6 into_py/impl_into_py_struct().(ast: &DeriveInputrust-analyzer cargo syn 2.0.101 derive/DeriveInput#, e: &DataStructrust-analyzer cargo syn 2.0.101 derive/DataStruct#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 84 |
let kwargs_toks = fields_to_kwargsrust-analyzer cargo libcst_derive 1.8.6 into_py/fields_to_kwargs().(&e.fieldsrust-analyzer cargo syn 2.0.101 derive/DataStruct#fields., false); |
| 85 |
let ident = &ast.identrust-analyzer cargo syn 2.0.101 derive/DeriveInput#ident.; |
| 86 |
let generics = &ast.genericsrust-analyzer cargo syn 2.0.101 derive/DeriveInput#generics.; |
| 87 |
let gen = quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 88 |
use pyo3::types::IntoPyDict as _; |
| 89 |
#[automatically_derived] |
| •90 |
impl#generics crate::nodes::traits::py::TryIntoPy<pyo3::Py<pyo3::PyAny>> for #ident #generics { |
| 91 |
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<pyo3::PyAny>> { |
| 92 |
use pyo3::types::PyAnyMethods; |
| 93 |
let libcst = pyo3::types::PyModule::import(py, "libcst")?; |
| 94 |
let kwargs = #kwargs_toks ; |
| 95 |
Ok(libcst |
| 96 |
.getattr(stringify!(#ident)) |
| 97 |
.expect(stringify!(no #ident found in libcst)) |
| 98 |
.call((), Some(&kwargs))? |
| 99 |
.into()) |
| 100 |
} |
| 101 |
} |
| 102 |
}; |
| 103 |
gen.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 104 |
} |
| 105 |
|
| 106 |
fn fields_to_kwargsrust-analyzer cargo libcst_derive 1.8.6 into_py/fields_to_kwargs().(fields: &Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#, is_enum: bool) -> quoterust-analyzer cargo quote 1.0.40 crate/::__privaterust-analyzer cargo quote 1.0.40 __private/::TokenStreamrust-analyzer cargo quote 1.0.40 __private/TokenStream# { |
| 107 |
let mut empty_kwargs = false; |
| 108 |
let mut py_varnames = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 109 |
let mut rust_varnames = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 110 |
let mut optional_py_varnames = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 111 |
let mut optional_rust_varnames = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 112 |
match &fields { |
| 113 |
Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Namedrust-analyzer cargo syn 2.0.101 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.101 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.101 data/FieldsNamed#named., .. }) => { |
| 114 |
for field in named.iterrust-analyzer cargo syn 2.0.101 punctuated/impl#[`Punctuated<T, P>`]iter().() { |
| 115 |
if has_attrrust-analyzer cargo libcst_derive 1.8.6 into_py/has_attr().(&field.attrsrust-analyzer cargo syn 2.0.101 data/Field#attrs., "skip_py") { |
| 116 |
continue; |
| 117 |
} |
| 118 |
if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ident) = field.identrust-analyzer cargo syn 2.0.101 data/Field#ident..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().() { |
| 119 |
let include = if let Visibilityrust-analyzer cargo syn 2.0.101 restriction/Visibility#::Publicrust-analyzer cargo syn 2.0.101 restriction/Visibility#Public#(_) = field.visrust-analyzer cargo syn 2.0.101 data/Field#vis. { |
| 120 |
true |
| 121 |
} else { |
| 122 |
is_enum |
| 123 |
}; |
| 124 |
if include { |
| 125 |
let pyname = format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("{}", ident); |
| 126 |
let rustname = if is_enum { |
| 127 |
ident.to_token_streamrust-analyzer cargo quote 1.0.40 to_tokens/ToTokens#to_token_stream().() |
| 128 |
} else { |
| 129 |
quoterust-analyzer cargo quote 1.0.40 quote!! { self.#ident } |
| 130 |
}; |
| 131 |
if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().has_attrrust-analyzer cargo libcst_derive 1.8.6 into_py/has_attr().(&field.attrsrust-analyzer cargo syn 2.0.101 data/Field#attrs., "no_py_default") { |
| 132 |
if let Typerust-analyzer cargo syn 2.0.101 ty/Type#::Pathrust-analyzer cargo syn 2.0.101 ty/Type#Path#(TypePathrust-analyzer cargo syn 2.0.101 ty/TypePath# { pathrust-analyzer cargo syn 2.0.101 ty/TypePath#path., .. }) = &field.tyrust-analyzer cargo syn 2.0.101 data/Field#ty. { |
| 133 |
if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(first) = path.segmentsrust-analyzer cargo syn 2.0.101 path/Path#segments..firstrust-analyzer cargo syn 2.0.101 punctuated/impl#[`Punctuated<T, P>`]first().() { |
| 134 |
if first.identrust-analyzer cargo syn 2.0.101 path/PathSegment#ident. rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<T>`]eq().==rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<T>`]eq(). rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<T>`]eq()."Option" { |
| 135 |
optional_py_varnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(pyname); |
| 136 |
optional_rust_varnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(rustname); |
| 137 |
continue; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
py_varnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(pyname); |
| 143 |
rust_varnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(rustname); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
empty_kwargs = py_varnames.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() && optional_py_varnames.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() |
| 148 |
} |
| 149 |
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., .. }) => { |
| 150 |
if unnamed.firstrust-analyzer cargo syn 2.0.101 punctuated/impl#[`Punctuated<T, P>`]first().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 151 |
py_varnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("value")); |
| 152 |
rust_varnames.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(quoterust-analyzer cargo quote 1.0.40 quote!! { self.0 }); |
| 153 |
} else { |
| 154 |
empty_kwargs = true; |
| 155 |
} |
| 156 |
} |
| 157 |
Fieldsrust-analyzer cargo syn 2.0.101 data/Fields#::Unitrust-analyzer cargo syn 2.0.101 data/Fields#Unit# => { |
| 158 |
empty_kwargs = true; |
| 159 |
} |
| 160 |
}; |
| 161 |
let kwargs_pairs = quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 162 |
#(Some((stringify!(#py_varnames), #rust_varnames.try_into_py(py)?)),)* |
| 163 |
}; |
| 164 |
let optional_pairs = quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 165 |
#(#optional_rust_varnames.map(|x| x.try_into_py(py)).transpose()?.map(|x| (stringify!(#optional_py_varnames), x)),)* |
| 166 |
}; |
| 167 |
if empty_kwargs { |
| 168 |
quoterust-analyzer cargo quote 1.0.40 quote!! { pyo3::types::PyDict::new(py) } |
| 169 |
} else { |
| 170 |
quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 171 |
[ #kwargs_pairs #optional_pairs ] |
| 172 |
.iter() |
| 173 |
.filter(|x| x.is_some()) |
| 174 |
.map(|x| x.as_ref().unwrap()) |
| 175 |
.collect::<Vec<_>>() |
| 176 |
.into_py_dict(py)? |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
fn has_attrrust-analyzer cargo libcst_derive 1.8.6 into_py/has_attr().(attrs: &[Attributerust-analyzer cargo syn 2.0.101 attr/Attribute#], name: &'static str) -> bool { |
| 182 |
attrs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|attr| attr.pathrust-analyzer cargo syn 2.0.101 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.101 path/impl#[Path]is_ident().(name)) |
| 183 |
} |