@@ -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 < ( String , Option < i64 > ) > ,
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 ( |v| ( v, None ) ) . collect ( ) ;
126
+ EnumHint { values }
127
+ }
128
+
129
+ #[ inline]
130
+ pub fn with_numbers ( values : Vec < ( String , i64 ) > ) -> Self {
131
+ let values = values
132
+ . into_iter ( )
133
+ . map ( |( key, val) | ( key, Some ( val) ) )
134
+ . collect ( ) ;
125
135
EnumHint { values }
126
136
}
127
137
@@ -130,13 +140,22 @@ impl EnumHint {
130
140
let mut s = String :: new ( ) ;
131
141
132
142
let mut iter = self . values . iter ( ) ;
143
+ let write_item = |s : & mut String , item : & ( String , Option < i64 > ) | match item {
144
+ ( key, Some ( val) ) => {
145
+ write ! ( s, "{key}:{val}" )
146
+ }
147
+ ( key, None ) => {
148
+ write ! ( s, "{key}" )
149
+ }
150
+ } ;
133
151
134
152
if let Some ( first) = iter. next ( ) {
135
- write ! ( s, "{ first}" ) . unwrap ( ) ;
153
+ write_item ( & mut s, first) . unwrap ( ) ;
136
154
}
137
155
138
156
for rest in iter {
139
- write ! ( s, ",{rest}" ) . unwrap ( ) ;
157
+ write ! ( s, "," ) . unwrap ( ) ;
158
+ write_item ( & mut s, rest) . unwrap ( ) ;
140
159
}
141
160
142
161
s. into ( )
@@ -469,3 +488,13 @@ impl ArrayHint {
469
488
}
470
489
}
471
490
}
491
+
492
+ godot_test ! ( test_enum_hint_without_mapping {
493
+ let hint = EnumHint :: new( vec![ "Foo" . into( ) , "Bar" . into( ) ] ) ;
494
+ assert_eq!( hint. to_godot_hint_string( ) . to_string( ) , "Foo,Bar" . to_string( ) , ) ;
495
+ } ) ;
496
+
497
+ godot_test ! ( test_enum_hint_with_mapping {
498
+ let hint = EnumHint :: with_numbers( vec![ ( "Foo" . into( ) , 42 ) , ( "Bar" . into( ) , 67 ) ] ) ;
499
+ assert_eq!( hint. to_godot_hint_string( ) . to_string( ) , "Foo:42,Bar:67" . to_string( ) , ) ;
500
+ } ) ;
0 commit comments