@@ -148,66 +148,69 @@ impl<'a> Iterator for OverStringIter<'a> {
148148
149149type HashIter < ' a > = OptIter < super :: linked_hash_map:: Iter < ' a , Yaml , Yaml > > ;
150150
151- pub trait GetVal {
152- fn get_yaml ( & self , k : & str ) -> Option < & Yaml > ;
153- fn get_bool ( & self , k : & str ) -> Result < Option < bool > > {
151+ pub trait GetVal < K >
152+ where
153+ K : ?Sized + core:: fmt:: Debug ,
154+ {
155+ fn get_yaml ( & self , k : & K ) -> Option < & Yaml > ;
156+ fn get_bool ( & self , k : & K ) -> Result < Option < bool > > {
154157 match self . get_yaml ( k) {
155158 None => Ok ( None ) ,
156159 Some ( v) => v
157160 . bool ( )
158- . with_context ( || format ! ( "Under key `{k}`" ) )
161+ . with_context ( || format ! ( "Under key `{k:? }`" ) )
159162 . map ( Some ) ,
160163 }
161164 }
162- fn get_i64 ( & self , k : & str ) -> Result < Option < i64 > > {
165+ fn get_i64 ( & self , k : & K ) -> Result < Option < i64 > > {
163166 match self . get_yaml ( k) {
164167 None => Ok ( None ) ,
165168 Some ( v) => v
166169 . i64 ( )
167- . with_context ( || format ! ( "Under key `{k}`" ) )
170+ . with_context ( || format ! ( "Under key `{k:? }`" ) )
168171 . map ( Some ) ,
169172 }
170173 }
171- fn get_u64 ( & self , k : & str ) -> Result < Option < u64 > > {
174+ fn get_u64 ( & self , k : & K ) -> Result < Option < u64 > > {
172175 self . get_i64 ( k) . map ( |v| v. map ( |v| v as u64 ) )
173176 }
174- fn get_u32 ( & self , k : & str ) -> Result < Option < u32 > > {
177+ fn get_u32 ( & self , k : & K ) -> Result < Option < u32 > > {
175178 self . get_i64 ( k) . map ( |v| v. map ( |v| v as u32 ) )
176179 }
177- fn get_str ( & self , k : & str ) -> Result < Option < & str > > {
180+ fn get_str ( & self , k : & K ) -> Result < Option < & str > > {
178181 match self . get_yaml ( k) {
179182 None => Ok ( None ) ,
180183 Some ( v) => v
181184 . str ( )
182- . with_context ( || format ! ( "Under key `{k}`" ) )
185+ . with_context ( || format ! ( "Under key `{k:? }`" ) )
183186 . map ( Some ) ,
184187 }
185188 }
186- fn get_string ( & self , k : & str ) -> Result < Option < String > > {
189+ fn get_string ( & self , k : & K ) -> Result < Option < String > > {
187190 self . get_str ( k) . map ( |v| v. map ( From :: from) )
188191 }
189- fn get_hash ( & self , k : & str ) -> Result < Option < & Hash > > {
192+ fn get_hash ( & self , k : & K ) -> Result < Option < & Hash > > {
190193 match self . get_yaml ( k) {
191194 None => Ok ( None ) ,
192195 Some ( v) => v
193196 . hash ( )
194- . with_context ( || format ! ( "Under key `{k}`" ) )
197+ . with_context ( || format ! ( "Under key `{k:? }`" ) )
195198 . map ( Some ) ,
196199 }
197200 }
198- fn hash_iter < ' a > ( & ' a self , k : & str ) -> HashIter < ' a > {
201+ fn hash_iter < ' a > ( & ' a self , k : & K ) -> HashIter < ' a > {
199202 HashIter :: new ( self . get_yaml ( k) . and_then ( Yaml :: as_hash) . map ( |h| h. iter ( ) ) )
200203 }
201- fn get_vec ( & self , k : & str ) -> Result < Option < & Vec < Yaml > > > {
204+ fn get_vec ( & self , k : & K ) -> Result < Option < & Vec < Yaml > > > {
202205 match self . get_yaml ( k) {
203206 None => Ok ( None ) ,
204207 Some ( v) => v
205208 . vec ( )
206- . with_context ( || format ! ( "Under key `{k}`" ) )
209+ . with_context ( || format ! ( "Under key `{k:? }`" ) )
207210 . map ( Some ) ,
208211 }
209212 }
210- fn str_vec_iter < ' a > ( & ' a self , k : & str ) -> Result < OptIter < OverStringIter < ' a > > > {
213+ fn str_vec_iter < ' a > ( & ' a self , k : & K ) -> Result < OptIter < OverStringIter < ' a > > > {
211214 let yaml = self . get_yaml ( k) ;
212215 Ok ( OptIter :: new ( match yaml {
213216 None => None ,
@@ -216,21 +219,28 @@ pub trait GetVal {
216219 if y. iter ( ) . all ( |x| x. as_str ( ) . is_some ( ) ) {
217220 Some ( OverStringIter ( yaml. unwrap ( ) , None ) )
218221 } else {
219- return Err ( anyhow ! ( "`{k}` requires string value or array of strings" ) ) ;
222+ return Err ( anyhow ! ( "`{k:? }` requires string value or array of strings" ) ) ;
220223 }
221224 }
222- _ => return Err ( anyhow ! ( "`{k}` requires string value or array of strings" ) ) ,
225+ _ => return Err ( anyhow ! ( "`{k:? }` requires string value or array of strings" ) ) ,
223226 } ) )
224227 }
225228}
226229
227- impl GetVal for Hash {
230+ impl GetVal < str > for Hash {
228231 #[ inline( always) ]
229232 fn get_yaml ( & self , k : & str ) -> Option < & Yaml > {
230233 self . get ( & k. to_yaml ( ) )
231234 }
232235}
233236
237+ impl GetVal < Yaml > for Hash {
238+ #[ inline( always) ]
239+ fn get_yaml ( & self , k : & Yaml ) -> Option < & Yaml > {
240+ self . get ( k)
241+ }
242+ }
243+
234244#[ cfg( test) ]
235245mod tests {
236246 use crate :: patch:: yaml_ext:: GetVal ;
0 commit comments