1
1
//! Strongly typed property hints.
2
2
3
- use std:: fmt:: { self , Write } ;
3
+ use std:: fmt:: { self , Display , Write } ;
4
4
use std:: ops:: RangeInclusive ;
5
5
6
6
use crate :: core_types:: GodotString ;
@@ -116,12 +116,22 @@ where
116
116
/// ```
117
117
#[ derive( Clone , Eq , PartialEq , Debug , Default ) ]
118
118
pub struct EnumHint {
119
- values : Vec < String > ,
119
+ values : Vec < EnumHintEntry > ,
120
120
}
121
121
122
122
impl EnumHint {
123
123
#[ inline]
124
124
pub fn new ( values : Vec < String > ) -> Self {
125
+ let values = values. into_iter ( ) . map ( EnumHintEntry :: new) . collect ( ) ;
126
+ EnumHint { values }
127
+ }
128
+
129
+ #[ inline]
130
+ pub fn with_values ( values : Vec < ( String , i64 ) > ) -> Self {
131
+ let values = values
132
+ . into_iter ( )
133
+ . map ( |( key, value) | EnumHintEntry :: with_value ( key, value) )
134
+ . collect ( ) ;
125
135
EnumHint { values }
126
136
}
127
137
@@ -136,13 +146,46 @@ impl EnumHint {
136
146
}
137
147
138
148
for rest in iter {
139
- write ! ( s, ",{rest}" ) . unwrap ( ) ;
149
+ write ! ( s, "," ) . unwrap ( ) ;
150
+ write ! ( s, "{rest}" ) . unwrap ( ) ;
140
151
}
141
152
142
153
s. into ( )
143
154
}
144
155
}
145
156
157
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
158
+ pub struct EnumHintEntry {
159
+ key : String ,
160
+ value : Option < i64 > ,
161
+ }
162
+
163
+ impl EnumHintEntry {
164
+ #[ inline]
165
+ pub fn new ( key : String ) -> Self {
166
+ Self { key, value : None }
167
+ }
168
+
169
+ #[ inline]
170
+ pub fn with_value ( key : String , value : i64 ) -> Self {
171
+ Self {
172
+ key,
173
+ value : Some ( value) ,
174
+ }
175
+ }
176
+ }
177
+
178
+ impl Display for EnumHintEntry {
179
+ #[ inline]
180
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
181
+ write ! ( f, "{}" , self . key) ?;
182
+ if let Some ( value) = self . value {
183
+ write ! ( f, ":{}" , value) ?;
184
+ }
185
+ Ok ( ( ) )
186
+ }
187
+ }
188
+
146
189
/// Possible hints for integers.
147
190
#[ derive( Clone , Debug ) ]
148
191
#[ non_exhaustive]
@@ -469,3 +512,13 @@ impl ArrayHint {
469
512
}
470
513
}
471
514
}
515
+
516
+ godot_test ! ( test_enum_hint_without_mapping {
517
+ let hint = EnumHint :: new( vec![ "Foo" . into( ) , "Bar" . into( ) ] ) ;
518
+ assert_eq!( hint. to_godot_hint_string( ) . to_string( ) , "Foo,Bar" . to_string( ) , ) ;
519
+ } ) ;
520
+
521
+ godot_test ! ( test_enum_hint_with_mapping {
522
+ let hint = EnumHint :: with_values( vec![ ( "Foo" . into( ) , 42 ) , ( "Bar" . into( ) , 67 ) ] ) ;
523
+ assert_eq!( hint. to_godot_hint_string( ) . to_string( ) , "Foo:42,Bar:67" . to_string( ) , ) ;
524
+ } ) ;
0 commit comments