@@ -7,7 +7,7 @@ use ide_db::{
77 helpers:: mod_path_to_ast,
88 imports:: insert_use:: { insert_use, ImportScope } ,
99 source_change:: SourceChangeBuilder ,
10- RootDatabase ,
10+ FxHashMap , RootDatabase ,
1111} ;
1212use itertools:: Itertools ;
1313use stdx:: { format_to, never} ;
@@ -22,15 +22,21 @@ use crate::{fix, Diagnostic, DiagnosticCode, DiagnosticsConfig, Severity};
2222#[ derive( Default ) ]
2323struct State {
2424 result : String ,
25- struct_counts : usize ,
2625 has_serialize : bool ,
2726 has_deserialize : bool ,
27+ names : FxHashMap < String , usize > ,
2828}
2929
3030impl State {
31- fn generate_new_name ( & mut self ) -> ast:: Name {
32- self . struct_counts += 1 ;
33- make:: name ( & format ! ( "Struct{}" , self . struct_counts) )
31+ fn generate_new_name ( & mut self , name : & str ) -> ast:: Name {
32+ let name = stdx:: to_camel_case ( name) ;
33+ let count = if let Some ( count) = self . names . get ( & name) {
34+ * count
35+ } else {
36+ self . names . insert ( name. clone ( ) , 1 ) ;
37+ 1
38+ } ;
39+ make:: name ( & format ! ( "{}{}" , name, count) )
3440 }
3541
3642 fn serde_derive ( & self ) -> String {
@@ -52,36 +58,42 @@ impl State {
5258 }
5359 }
5460
55- fn build_struct ( & mut self , value : & serde_json:: Map < String , serde_json:: Value > ) -> ast:: Type {
56- let name = self . generate_new_name ( ) ;
61+ fn build_struct (
62+ & mut self ,
63+ name : & str ,
64+ value : & serde_json:: Map < String , serde_json:: Value > ,
65+ ) -> ast:: Type {
66+ let name = self . generate_new_name ( name) ;
5767 let ty = make:: ty ( & name. to_string ( ) ) ;
5868 let strukt = make:: struct_ (
5969 None ,
6070 name,
6171 None ,
6272 make:: record_field_list ( value. iter ( ) . sorted_unstable_by_key ( |x| x. 0 ) . map (
63- |( name, value) | make:: record_field ( None , make:: name ( name) , self . type_of ( value) ) ,
73+ |( name, value) | {
74+ make:: record_field ( None , make:: name ( name) , self . type_of ( name, value) )
75+ } ,
6476 ) )
6577 . into ( ) ,
6678 ) ;
6779 format_to ! ( self . result, "{}{}\n " , self . serde_derive( ) , strukt) ;
6880 ty
6981 }
7082
71- fn type_of ( & mut self , value : & serde_json:: Value ) -> ast:: Type {
83+ fn type_of ( & mut self , name : & str , value : & serde_json:: Value ) -> ast:: Type {
7284 match value {
7385 serde_json:: Value :: Null => make:: ty_unit ( ) ,
7486 serde_json:: Value :: Bool ( _) => make:: ty ( "bool" ) ,
7587 serde_json:: Value :: Number ( it) => make:: ty ( if it. is_i64 ( ) { "i64" } else { "f64" } ) ,
7688 serde_json:: Value :: String ( _) => make:: ty ( "String" ) ,
7789 serde_json:: Value :: Array ( it) => {
7890 let ty = match it. iter ( ) . next ( ) {
79- Some ( x) => self . type_of ( x) ,
91+ Some ( x) => self . type_of ( name , x) ,
8092 None => make:: ty_placeholder ( ) ,
8193 } ;
8294 make:: ty ( & format ! ( "Vec<{ty}>" ) )
8395 }
84- serde_json:: Value :: Object ( x) => self . build_struct ( x) ,
96+ serde_json:: Value :: Object ( x) => self . build_struct ( name , x) ,
8597 }
8698 }
8799}
@@ -113,7 +125,7 @@ pub(crate) fn json_in_items(
113125 let serialize_resolved = scope_resolve ( "::serde::Serialize" ) ;
114126 state. has_deserialize = deserialize_resolved. is_some ( ) ;
115127 state. has_serialize = serialize_resolved. is_some ( ) ;
116- state. build_struct ( & it) ;
128+ state. build_struct ( "Root" , & it) ;
117129 edit. insert ( range. start ( ) , state. result ) ;
118130 acc. push (
119131 Diagnostic :: new (
@@ -218,7 +230,7 @@ mod tests {
218230 }
219231
220232 #[derive(Serialize)]
221- struct Struct1 { bar: f64, bay: i64, baz: (), r#box: bool, foo: String }
233+ struct Root1 { bar: f64, bay: i64, baz: (), r#box: bool, foo: String }
222234
223235 "# ,
224236 ) ;
@@ -237,9 +249,9 @@ mod tests {
237249 }
238250 "# ,
239251 r#"
240- struct Struct3 { }
241- struct Struct2 { kind: String, value: Struct3 }
242- struct Struct1 { bar: Struct2 , foo: String }
252+ struct Value1 { }
253+ struct Bar1 { kind: String, value: Value1 }
254+ struct Root1 { bar: Bar1 , foo: String }
243255
244256 "# ,
245257 ) ;
@@ -276,9 +288,9 @@ mod tests {
276288 use serde::Deserialize;
277289
278290 #[derive(Serialize, Deserialize)]
279- struct Struct2 { x: i64, y: i64 }
291+ struct OfObject1 { x: i64, y: i64 }
280292 #[derive(Serialize, Deserialize)]
281- struct Struct1 { empty: Vec<_>, nested: Vec<Vec<Vec<i64>>>, of_object: Vec<Struct2 >, of_string: Vec<String> }
293+ struct Root1 { empty: Vec<_>, nested: Vec<Vec<Vec<i64>>>, of_object: Vec<OfObject1 >, of_string: Vec<String> }
282294
283295 "# ,
284296 ) ;
0 commit comments