@@ -67,7 +67,7 @@ pub(crate) fn generate_class_constants(class: &GodotClass) -> TokenStream {
67
67
68
68
let mut class_constants: Vec < ( & ConstantName , & ConstantValue ) > =
69
69
class. constants . iter ( ) . collect ( ) ;
70
- class_constants. sort_by ( |a , b| a . 0 . cmp ( b . 0 ) ) ;
70
+ class_constants. sort_by ( constant_sorter ) ;
71
71
72
72
for ( name, value) in & class_constants {
73
73
let name = format_ident ! ( "{}" , name) ;
@@ -88,33 +88,18 @@ pub(crate) fn generate_class_constants(class: &GodotClass) -> TokenStream {
88
88
}
89
89
}
90
90
91
- fn generate_enum_name ( class_name : & str , enum_name : & str ) -> String {
92
- // In order to not pollute the API with more Result types,
93
- // rename the Result enum used by Search to SearchResult
94
- // to_camel_case() is used to make the enums more Rust like.
95
- // DOFBlurQuality => DofBlurQuality
96
- match enum_name {
97
- "Result" => {
98
- let mut res = String :: from ( class_name) ;
99
- res. push_str ( enum_name) ;
100
- res. to_camel_case ( )
101
- }
102
- _ => enum_name. to_camel_case ( ) ,
103
- }
104
- }
105
-
106
91
pub ( crate ) fn generate_enums ( class : & GodotClass ) -> TokenStream {
107
- // TODO: check whether the start of the variant name is
108
- // equal to the end of the enum name and if so don't repeat it
109
- // it. For example ImageFormat::Rgb8 instead of ImageFormat::FormatRgb8.
92
+ // TODO: check whether the start of the variant name is equal to the end of the enum name and if so, don't repeat it.
93
+ // For example ImageFormat::Rgb8 instead of ImageFormat::FormatRgb8.
94
+
110
95
let mut enums: Vec < & Enum > = class. enums . iter ( ) . collect ( ) ;
111
96
enums. sort ( ) ;
112
97
let enums = enums. iter ( ) . map ( |e| {
113
98
let enum_name = generate_enum_name ( & class. name , & e. name ) ;
114
99
let typ_name = format_ident ! ( "{}" , enum_name) ;
115
100
116
101
let mut values: Vec < _ > = e. values . iter ( ) . collect ( ) ;
117
- values. sort_by ( |a , b| a . 1 . cmp ( b . 1 ) ) ;
102
+ values. sort_by ( constant_sorter ) ;
118
103
119
104
let consts = values. iter ( ) . map ( |( key, val) | {
120
105
let key = key. to_uppercase ( ) ;
@@ -150,3 +135,24 @@ pub(crate) fn generate_enums(class: &GodotClass) -> TokenStream {
150
135
#( #enums) *
151
136
}
152
137
}
138
+
139
+ fn generate_enum_name ( class_name : & str , enum_name : & str ) -> String {
140
+ // In order to not pollute the API with more Result types,
141
+ // rename the Result enum used by Search to SearchResult
142
+ // to_camel_case() is used to make the enums more Rust like.
143
+ // DOFBlurQuality => DofBlurQuality
144
+ match enum_name {
145
+ "Result" => {
146
+ let mut res = String :: from ( class_name) ;
147
+ res. push_str ( enum_name) ;
148
+ res. to_camel_case ( )
149
+ }
150
+ _ => enum_name. to_camel_case ( ) ,
151
+ }
152
+ }
153
+
154
+ // Ensures deterministic order of constants, not dependent on inner hash-map or sort workings
155
+ fn constant_sorter ( a : & ( & String , & i64 ) , b : & ( & String , & i64 ) ) -> std:: cmp:: Ordering {
156
+ Ord :: cmp ( a. 1 , b. 1 ) // first, sort by integer value
157
+ . then ( Ord :: cmp ( a. 0 , b. 0 ) ) // and if equal, by name
158
+ }
0 commit comments