@@ -361,9 +361,9 @@ pub fn all_schemas_resolved(spec: &Spec) -> Result<Vec<(RefKey, SchemaGen)>> {
361
361
}
362
362
363
363
pub enum ModelCode {
364
- Struct ( TokenStream ) ,
364
+ Struct ( StructCode ) ,
365
365
Enum ( StructFieldCode ) ,
366
- VecAlias ( TokenStream ) ,
366
+ VecAlias ( VecAliasCode ) ,
367
367
TypeAlias ( TypeAliasCode ) ,
368
368
}
369
369
@@ -519,12 +519,7 @@ fn create_enum(
519
519
for enum_value in & enum_values {
520
520
let value = & enum_value. value ;
521
521
let nm = value. to_camel_case_ident ( ) ?;
522
- let doc_comment = match & enum_value. description {
523
- Some ( description) => {
524
- quote ! { #[ doc = #description] }
525
- }
526
- None => quote ! { } ,
527
- } ;
522
+ let doc_comment = DocCommentCode :: from ( & enum_value. description ) ;
528
523
let lower = value. to_lowercase ( ) ;
529
524
let rename = if & nm. to_string ( ) == value {
530
525
quote ! { }
@@ -642,12 +637,7 @@ fn create_enum(
642
637
quote ! { }
643
638
} ;
644
639
645
- let doc_comment = match & property. schema . common . description {
646
- Some ( description) => {
647
- quote ! { #[ doc = #description] }
648
- }
649
- None => quote ! { } ,
650
- } ;
640
+ let doc_comment = DocCommentCode :: from ( & property. schema . common . description ) ;
651
641
652
642
let code = quote ! {
653
643
#doc_comment
@@ -667,11 +657,96 @@ fn create_enum(
667
657
} )
668
658
}
669
659
670
- fn create_vec_alias ( schema : & SchemaGen ) -> Result < TokenStream > {
660
+ pub struct VecAliasCode {
661
+ pub id : Ident ,
662
+ pub value : TypeNameCode ,
663
+ }
664
+
665
+ impl ToTokens for VecAliasCode {
666
+ fn to_tokens ( & self , tokens : & mut TokenStream ) {
667
+ let id = & self . id ;
668
+ let value = & self . value ;
669
+ tokens. extend ( quote ! {
670
+ pub type #id = Vec <#value>;
671
+ } ) ;
672
+ }
673
+ }
674
+
675
+ fn create_vec_alias ( schema : & SchemaGen ) -> Result < VecAliasCode > {
671
676
let items = schema. array_items ( ) ?;
672
- let typ = schema. name ( ) ?. to_camel_case_ident ( ) ?;
673
- let items_typ = TypeNameCode :: new ( & get_type_name_for_schema_ref ( items) ?) ?;
674
- Ok ( quote ! { pub type #typ = Vec <#items_typ>; } )
677
+ let id = schema. name ( ) ?. to_camel_case_ident ( ) ?;
678
+ let value = TypeNameCode :: new ( & get_type_name_for_schema_ref ( items) ?) ?;
679
+ Ok ( VecAliasCode { id, value } )
680
+ }
681
+
682
+ pub struct StructCode {
683
+ doc_comment : DocCommentCode ,
684
+ struct_name_code : Ident ,
685
+ default_code : TokenStream ,
686
+ props : Vec < StructPropCode > ,
687
+ continuable : TokenStream ,
688
+ implement_default : bool ,
689
+ new_fn_params : Vec < TokenStream > ,
690
+ new_fn_body : TokenStream ,
691
+ mod_code : TokenStream ,
692
+ ns : Ident ,
693
+ }
694
+
695
+ impl ToTokens for StructCode {
696
+ fn to_tokens ( & self , tokens : & mut TokenStream ) {
697
+ let StructCode {
698
+ doc_comment,
699
+ struct_name_code,
700
+ default_code,
701
+ props,
702
+ continuable,
703
+ implement_default,
704
+ new_fn_params,
705
+ new_fn_body,
706
+ mod_code,
707
+ ns,
708
+ } = self ;
709
+
710
+ let struct_code = quote ! {
711
+ #doc_comment
712
+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
713
+ #default_code
714
+ pub struct #struct_name_code {
715
+ #( #props) *
716
+ }
717
+ #continuable
718
+ } ;
719
+ tokens. extend ( struct_code) ;
720
+
721
+ tokens. extend ( if * implement_default {
722
+ quote ! {
723
+ impl #struct_name_code {
724
+ pub fn new( ) -> Self {
725
+ Self :: default ( )
726
+ }
727
+ }
728
+ }
729
+ } else {
730
+ quote ! {
731
+ impl #struct_name_code {
732
+ pub fn new( #( #new_fn_params) , * ) -> Self {
733
+ Self {
734
+ #new_fn_body
735
+ }
736
+ }
737
+ }
738
+ }
739
+ } ) ;
740
+
741
+ if !mod_code. is_empty ( ) {
742
+ tokens. extend ( quote ! {
743
+ pub mod #ns {
744
+ use super :: * ;
745
+ #mod_code
746
+ }
747
+ } ) ;
748
+ }
749
+ }
675
750
}
676
751
677
752
fn create_struct (
@@ -680,10 +755,9 @@ fn create_struct(
680
755
struct_name : & str ,
681
756
pageable : Option < & MsPageable > ,
682
757
mut needs_boxing : HashSet < String > ,
683
- ) -> Result < TokenStream > {
684
- let mut code = TokenStream :: new ( ) ;
758
+ ) -> Result < StructCode > {
685
759
let mut mod_code = TokenStream :: new ( ) ;
686
- let mut props = TokenStream :: new ( ) ;
760
+ let mut props = Vec :: new ( ) ;
687
761
let mut new_fn_params: Vec < TokenStream > = Vec :: new ( ) ;
688
762
let mut new_fn_body = TokenStream :: new ( ) ;
689
763
let ns = struct_name. to_snake_case_ident ( ) ?;
@@ -697,9 +771,11 @@ fn create_struct(
697
771
let schema_name = schema. name ( ) ?;
698
772
let type_name = schema_name. to_camel_case_ident ( ) ?;
699
773
let field_name = schema_name. to_snake_case_ident ( ) ?;
700
- props. extend ( quote ! {
701
- #[ serde( flatten) ]
702
- pub #field_name: #type_name,
774
+ props. push ( StructPropCode {
775
+ doc_comments : Vec :: new ( ) ,
776
+ serde : SerdeCode :: flatten ( ) ,
777
+ field_name : field_name. clone ( ) ,
778
+ field_type : type_name. clone ( ) . into ( ) ,
703
779
} ) ;
704
780
if schema. implement_default ( ) {
705
781
new_fn_body. extend ( quote ! { #field_name: #type_name:: default ( ) , } ) ;
@@ -738,9 +814,10 @@ fn create_struct(
738
814
needs_boxing. clone ( ) ,
739
815
) ?;
740
816
mod_code. extend ( field_code. into_token_stream ( ) ) ;
817
+ let mut doc_comments = Vec :: new ( ) ;
741
818
// uncomment the next two lines to help identify entries that need boxed
742
819
// let prop_nm_str = format!("{} , {} , {}", prop_nm.file_path, prop_nm.schema_name, property_name);
743
- // props.extend(quote! { #[doc = # prop_nm_str ]} );
820
+ // doc_comments.push(DocCommentCode::from(&Some( prop_nm_str)) );
744
821
745
822
let mut boxed = false ;
746
823
if needs_boxing. contains ( & type_name. to_string ( ) . to_camel_case_ident ( ) ?. to_string ( ) ) {
@@ -795,27 +872,21 @@ fn create_struct(
795
872
serde_attrs. push ( quote ! { with = "azure_core::xml::text_content" } ) ;
796
873
}
797
874
}
798
- let serde = if !serde_attrs. is_empty ( ) {
799
- quote ! { #[ serde( #( #serde_attrs) , * ) ] }
800
- } else {
801
- quote ! { }
802
- } ;
875
+ let serde = SerdeCode :: new ( serde_attrs) ;
803
876
804
877
// see if a field should be wrapped in a Box
805
878
if cg. should_box_property ( prop_nm) {
806
879
boxed = true ;
807
880
}
808
881
type_name = type_name. boxed ( boxed) ;
809
882
810
- let doc_comment = match & property. schema . schema . common . description {
811
- Some ( description) => quote ! { #[ doc = #description] } ,
812
- None => quote ! { } ,
813
- } ;
883
+ doc_comments. push ( DocCommentCode :: from ( & property. schema . schema . common . description ) ) ;
814
884
815
- props. extend ( quote ! {
816
- #doc_comment
817
- #serde
818
- pub #field_name: #type_name,
885
+ props. push ( StructPropCode {
886
+ doc_comments,
887
+ serde,
888
+ field_name : field_name. clone ( ) ,
889
+ field_type : type_name. clone ( ) ,
819
890
} ) ;
820
891
821
892
if is_required {
@@ -839,10 +910,7 @@ fn create_struct(
839
910
quote ! { }
840
911
} ;
841
912
842
- let doc_comment = match & schema. schema . common . description {
843
- Some ( description) => quote ! { #[ doc = #description] } ,
844
- None => quote ! { } ,
845
- } ;
913
+ let doc_comment = DocCommentCode :: from ( & schema. schema . common . description ) ;
846
914
847
915
let mut continuable = quote ! { } ;
848
916
if let Some ( pageable) = pageable {
@@ -906,47 +974,108 @@ fn create_struct(
906
974
}
907
975
}
908
976
909
- let struct_code = quote ! {
910
- #doc_comment
911
- #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
912
- #default_code
913
- pub struct #struct_name_code {
914
- #props
977
+ Ok ( StructCode {
978
+ doc_comment,
979
+ struct_name_code,
980
+ default_code,
981
+ props,
982
+ continuable,
983
+ implement_default : schema. implement_default ( ) ,
984
+ new_fn_params,
985
+ new_fn_body,
986
+ mod_code,
987
+ ns,
988
+ } )
989
+ }
990
+
991
+ pub struct StructPropCode {
992
+ pub doc_comments : Vec < DocCommentCode > ,
993
+ pub serde : SerdeCode ,
994
+ pub field_name : Ident ,
995
+ pub field_type : TypeNameCode ,
996
+ }
997
+
998
+ impl StructPropCode {
999
+ pub fn new ( field_name : Ident , field_type : TypeNameCode ) -> Self {
1000
+ Self {
1001
+ doc_comments : Vec :: new ( ) ,
1002
+ serde : SerdeCode :: default ( ) ,
1003
+ field_name,
1004
+ field_type,
915
1005
}
916
- #continuable
917
- } ;
918
- code. extend ( struct_code) ;
1006
+ }
1007
+ }
919
1008
920
- code. extend ( if schema. implement_default ( ) {
921
- quote ! {
922
- impl #struct_name_code {
923
- pub fn new( ) -> Self {
924
- Self :: default ( )
925
- }
926
- }
1009
+ impl ToTokens for StructPropCode {
1010
+ fn to_tokens ( & self , tokens : & mut TokenStream ) {
1011
+ let doc_comments = & self . doc_comments ;
1012
+ let serde = & self . serde ;
1013
+ let field_name = & self . field_name ;
1014
+ let field_type = & self . field_type ;
1015
+ tokens. extend ( quote ! {
1016
+ #( #doc_comments) *
1017
+ #serde
1018
+ pub #field_name: #field_type,
1019
+ } ) ;
1020
+ }
1021
+ }
1022
+
1023
+ #[ derive( Default ) ]
1024
+ pub struct SerdeCode {
1025
+ pub attributes : Vec < TokenStream > ,
1026
+ }
1027
+
1028
+ impl SerdeCode {
1029
+ pub fn new ( attributes : Vec < TokenStream > ) -> Self {
1030
+ Self { attributes }
1031
+ }
1032
+ pub fn flatten ( ) -> Self {
1033
+ Self {
1034
+ attributes : vec ! [ quote! { flatten } ] ,
927
1035
}
928
- } else {
929
- quote ! {
930
- impl #struct_name_code {
931
- pub fn new( #( #new_fn_params) , * ) -> Self {
932
- Self {
933
- #new_fn_body
934
- }
935
- }
936
- }
1036
+ }
1037
+ }
1038
+
1039
+ impl ToTokens for SerdeCode {
1040
+ fn to_tokens ( & self , tokens : & mut TokenStream ) {
1041
+ let attributes = & self . attributes ;
1042
+ if !attributes. is_empty ( ) {
1043
+ tokens. extend ( quote ! {
1044
+ #[ serde( #( #attributes) , * ) ]
1045
+ } ) ;
937
1046
}
938
- } ) ;
1047
+ }
1048
+ }
939
1049
940
- if !mod_code. is_empty ( ) {
941
- code. extend ( quote ! {
942
- pub mod #ns {
943
- use super :: * ;
944
- #mod_code
945
- }
946
- } ) ;
1050
+ #[ derive( Default ) ]
1051
+ pub struct DocCommentCode {
1052
+ description : Option < String > ,
1053
+ }
1054
+
1055
+ impl From < & str > for DocCommentCode {
1056
+ fn from ( description : & str ) -> Self {
1057
+ Self {
1058
+ description : Some ( description. to_string ( ) ) ,
1059
+ }
947
1060
}
1061
+ }
948
1062
949
- Ok ( code)
1063
+ impl From < & Option < String > > for DocCommentCode {
1064
+ fn from ( description : & Option < String > ) -> Self {
1065
+ Self {
1066
+ description : description. clone ( ) ,
1067
+ }
1068
+ }
1069
+ }
1070
+
1071
+ impl ToTokens for DocCommentCode {
1072
+ fn to_tokens ( & self , tokens : & mut TokenStream ) {
1073
+ if let Some ( description) = & self . description {
1074
+ tokens. extend ( quote ! {
1075
+ #[ doc = #description]
1076
+ } ) ;
1077
+ }
1078
+ }
950
1079
}
951
1080
952
1081
pub struct StructFieldCode {
@@ -963,16 +1092,16 @@ impl ToTokens for StructFieldCode {
963
1092
}
964
1093
965
1094
enum TypeCode {
966
- Struct ( TokenStream ) ,
1095
+ Struct ( StructCode ) ,
967
1096
Enum ( TokenStream ) ,
968
1097
XmlWrapped ( XmlWrappedCode ) ,
969
1098
}
970
1099
971
1100
impl ToTokens for TypeCode {
972
1101
fn to_tokens ( & self , tokens : & mut TokenStream ) {
973
1102
match self {
974
- TypeCode :: Struct ( code) => tokens . extend ( code. clone ( ) ) ,
975
- TypeCode :: Enum ( code) => tokens . extend ( code. clone ( ) ) ,
1103
+ TypeCode :: Struct ( code) => code. to_tokens ( tokens ) ,
1104
+ TypeCode :: Enum ( code) => code. to_tokens ( tokens ) ,
976
1105
TypeCode :: XmlWrapped ( code) => code. to_tokens ( tokens) ,
977
1106
}
978
1107
}
0 commit comments