@@ -42,9 +42,40 @@ fn gen_clone_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
4242 let right = make:: ext:: ident_path ( & format ! ( "{}" , name) ) ;
4343 let variant_name = make:: path_concat ( left, right) ;
4444
45- let pattern = make:: path_pat ( variant_name. clone ( ) ) ;
46- let variant_expr = make:: expr_path ( variant_name) ;
47- arms. push ( make:: match_arm ( Some ( pattern. into ( ) ) , None , variant_expr) ) ;
45+ match variant. field_list ( ) {
46+ // => match self { Self::Name { x } => Self::Name { x: x.clone() } }
47+ Some ( ast:: FieldList :: RecordFieldList ( list) ) => {
48+ let mut pats = vec ! [ ] ;
49+ let mut fields = vec ! [ ] ;
50+ for field in list. fields ( ) {
51+ let field_name = field. name ( ) ?;
52+ let pat = make:: ident_pat ( false , false , field_name. clone ( ) ) ;
53+ pats. push ( pat. into ( ) ) ;
54+
55+ let path = make:: ext:: ident_path ( & field_name. to_string ( ) ) ;
56+ let method_call = gen_clone_call ( make:: expr_path ( path) ) ;
57+ let name_ref = make:: name_ref ( & field_name. to_string ( ) ) ;
58+ let field = make:: record_expr_field ( name_ref, Some ( method_call) ) ;
59+ fields. push ( field) ;
60+ }
61+ let pattern = make:: record_pat ( variant_name. clone ( ) , pats. into_iter ( ) ) ;
62+
63+ let fields = make:: record_expr_field_list ( fields) ;
64+ let record_expr = make:: record_expr ( variant_name, fields) . into ( ) ;
65+
66+ arms. push ( make:: match_arm ( Some ( pattern. into ( ) ) , None , record_expr) ) ;
67+ }
68+
69+ // => match self { Self::Name(arg1) => Self::Name(arg1.clone()) }
70+ Some ( ast:: FieldList :: TupleFieldList ( list) ) => todo ! ( ) ,
71+
72+ // => match self { Self::Name => Self::Name }
73+ None => {
74+ let pattern = make:: path_pat ( variant_name. clone ( ) ) ;
75+ let variant_expr = make:: expr_path ( variant_name) ;
76+ arms. push ( make:: match_arm ( Some ( pattern. into ( ) ) , None , variant_expr) ) ;
77+ }
78+ }
4879 }
4980
5081 let match_target = make:: expr_path ( make:: ext:: ident_path ( "self" ) ) ;
0 commit comments