File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ fn from_yaml_value(
50
50
match key {
51
51
yaml:: Yaml :: String ( k) => m. insert ( k. to_owned ( ) , from_yaml_value ( uri, value) ?) ,
52
52
yaml:: Yaml :: Integer ( k) => m. insert ( k. to_string ( ) , from_yaml_value ( uri, value) ?) ,
53
- _ => unreachable ! ( ) ,
53
+ other => Err ( Box :: new ( UnsupportedHashKeyError ( format ! ( "{other:?}" ) ) ) ) ? ,
54
54
} ;
55
55
}
56
56
Ok ( Value :: new ( uri, ValueKind :: Table ( m) ) )
@@ -103,3 +103,22 @@ impl Error for FloatParsingError {
103
103
"Floating point number parsing failed"
104
104
}
105
105
}
106
+
107
+ #[ derive( Debug , Clone ) ]
108
+ struct UnsupportedHashKeyError ( String ) ;
109
+
110
+ impl fmt:: Display for UnsupportedHashKeyError {
111
+ fn fmt ( & self , format : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
112
+ write ! (
113
+ format,
114
+ "Cannot parse {} because it is an unsupported hash key type" ,
115
+ self . 0
116
+ )
117
+ }
118
+ }
119
+
120
+ impl Error for UnsupportedHashKeyError {
121
+ fn description ( & self ) -> & str {
122
+ "Unsupported yaml hash key found"
123
+ }
124
+ }
Original file line number Diff line number Diff line change @@ -340,8 +340,9 @@ fn yaml() {
340
340
}
341
341
342
342
#[ test]
343
- #[ should_panic]
344
- fn test_yaml_parsing_unsupported_hash ( ) {
343
+ /// We only support certain types as keys to a yaml hash, this test ensures
344
+ /// we communicate that to the user effectively.
345
+ fn test_yaml_parsing_unsupported_hash_has_useful_error_message ( ) {
345
346
let result = Config :: builder ( )
346
347
. add_source ( File :: from_str (
347
348
r#"
@@ -352,4 +353,8 @@ inner_vec:
352
353
) )
353
354
. build ( ) ;
354
355
assert ! ( result. is_err( ) ) ;
356
+ assert_data_eq ! (
357
+ result. unwrap_err( ) . to_string( ) ,
358
+ str ![ "Cannot parse Array([Integer(1), Integer(2)]) because it is an unsupported hash key type" ]
359
+ ) ;
355
360
}
You can’t perform that action at this time.
0 commit comments