11//! Strongly typed property hints.
22
3- use std:: fmt:: { self , Write } ;
3+ use std:: fmt:: { self , Display , Write } ;
44use std:: ops:: RangeInclusive ;
55
66use crate :: core_types:: GodotString ;
@@ -116,12 +116,22 @@ where
116116/// ```
117117#[ derive( Clone , Eq , PartialEq , Debug , Default ) ]
118118pub struct EnumHint {
119- values : Vec < String > ,
119+ values : Vec < EnumHintEntry > ,
120120}
121121
122122impl EnumHint {
123123 #[ inline]
124124 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 ( ) ;
125135 EnumHint { values }
126136 }
127137
@@ -136,13 +146,46 @@ impl EnumHint {
136146 }
137147
138148 for rest in iter {
139- write ! ( s, ",{rest}" ) . unwrap ( ) ;
149+ write ! ( s, "," ) . unwrap ( ) ;
150+ write ! ( s, "{rest}" ) . unwrap ( ) ;
140151 }
141152
142153 s. into ( )
143154 }
144155}
145156
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+
146189/// Possible hints for integers.
147190#[ derive( Clone , Debug ) ]
148191#[ non_exhaustive]
@@ -469,3 +512,13 @@ impl ArrayHint {
469512 }
470513 }
471514}
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