1- use crate :: attributes:: IntoPyWithAttribute ;
1+ use crate :: attributes:: { IntoPyWithAttribute , RenamingRule } ;
22use crate :: derive_attributes:: { ContainerAttributes , FieldAttributes } ;
3- use crate :: utils:: Ctx ;
3+ use crate :: utils:: { self , Ctx } ;
44use proc_macro2:: { Span , TokenStream } ;
55use quote:: { format_ident, quote, quote_spanned, ToTokens } ;
66use syn:: ext:: IdentExt ;
@@ -65,6 +65,7 @@ struct Container<'a, const REF: bool> {
6565 path : syn:: Path ,
6666 receiver : Option < Ident > ,
6767 ty : ContainerType < ' a > ,
68+ rename_rule : Option < RenamingRule > ,
6869}
6970
7071/// Construct a container based on fields, identifier and attributes.
@@ -79,6 +80,10 @@ impl<'a, const REF: bool> Container<'a, REF> {
7980 ) -> Result < Self > {
8081 let style = match fields {
8182 Fields :: Unnamed ( unnamed) if !unnamed. unnamed . is_empty ( ) => {
83+ ensure_spanned ! (
84+ options. rename_all. is_none( ) ,
85+ options. rename_all. span( ) => "`rename_all` is useless on tuple structs and variants."
86+ ) ;
8287 let mut tuple_fields = unnamed
8388 . unnamed
8489 . iter ( )
@@ -127,6 +132,10 @@ impl<'a, const REF: bool> Container<'a, REF> {
127132 attrs. getter. is_none( ) ,
128133 attrs. getter. unwrap( ) . span( ) => "`transparent` structs may not have `item` nor `attribute` for the inner field"
129134 ) ;
135+ ensure_spanned ! (
136+ options. rename_all. is_none( ) ,
137+ options. rename_all. span( ) => "`rename_all` is not permitted on `transparent` structs and variants"
138+ ) ;
130139 ensure_spanned ! (
131140 attrs. into_py_with. is_none( ) ,
132141 attrs. into_py_with. span( ) => "`into_py_with` is not permitted on `transparent` structs or variants"
@@ -169,6 +178,7 @@ impl<'a, const REF: bool> Container<'a, REF> {
169178 path,
170179 receiver,
171180 ty : style,
181+ rename_rule : options. rename_all . map ( |v| v. value . rule ) ,
172182 } ;
173183 Ok ( v)
174184 }
@@ -254,7 +264,10 @@ impl<'a, const REF: bool> Container<'a, REF> {
254264 . as_ref ( )
255265 . and_then ( |item| item. 0 . as_ref ( ) )
256266 . map ( |item| item. into_token_stream ( ) )
257- . unwrap_or_else ( || f. ident . unraw ( ) . to_string ( ) . into_token_stream ( ) ) ;
267+ . unwrap_or_else ( || {
268+ let name = f. ident . unraw ( ) . to_string ( ) ;
269+ self . rename_rule . map ( |rule| utils:: apply_renaming_rule ( rule, & name) ) . unwrap_or ( name) . into_token_stream ( )
270+ } ) ;
258271 let value = Ident :: new ( & format ! ( "arg{i}" ) , f. field . ty . span ( ) ) ;
259272
260273 if let Some ( expr_path) = f. into_py_with . as_ref ( ) . map ( |i|& i. value ) {
@@ -459,6 +472,9 @@ pub fn build_derive_into_pyobject<const REF: bool>(tokens: &DeriveInput) -> Resu
459472 if options. transparent . is_some ( ) {
460473 bail_spanned ! ( tokens. span( ) => "`transparent` is not supported at top level for enums" ) ;
461474 }
475+ if let Some ( rename_all) = options. rename_all {
476+ bail_spanned ! ( rename_all. span( ) => "`rename_all` is not supported at top level for enums" ) ;
477+ }
462478 let en = Enum :: < REF > :: new ( en, & tokens. ident ) ?;
463479 en. build ( ctx)
464480 }
0 commit comments