@@ -3,7 +3,7 @@ use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Err
3
3
4
4
/// This type represents all possible errors that can occur when
5
5
/// serializing or deserializing RON data.
6
- #[ derive( Clone , Debug , PartialEq ) ]
6
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
7
7
pub struct SpannedError {
8
8
pub code : Error ,
9
9
pub position : Position ,
@@ -12,7 +12,7 @@ pub struct SpannedError {
12
12
pub type Result < T , E = Error > = std:: result:: Result < T , E > ;
13
13
pub type SpannedResult < T > = std:: result:: Result < T , SpannedError > ;
14
14
15
- #[ derive( Clone , Debug , PartialEq ) ]
15
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
16
16
#[ non_exhaustive]
17
17
pub enum Error {
18
18
Io ( String ) ,
@@ -59,13 +59,9 @@ pub enum Error {
59
59
Utf8Error ( Utf8Error ) ,
60
60
TrailingCharacters ,
61
61
62
- ExpectedDifferentType {
63
- expected : String ,
64
- found : UnexpectedSerdeTypeValue ,
65
- } ,
66
62
InvalidValueForType {
67
63
expected : String ,
68
- found : UnexpectedSerdeTypeValue ,
64
+ found : String ,
69
65
} ,
70
66
ExpectedDifferentLength {
71
67
expected : String ,
@@ -148,16 +144,6 @@ impl fmt::Display for Error {
148
144
}
149
145
Error :: UnexpectedByte ( ref byte) => write ! ( f, "Unexpected byte {:?}" , byte) ,
150
146
Error :: TrailingCharacters => f. write_str ( "Non-whitespace trailing characters" ) ,
151
- Error :: ExpectedDifferentType {
152
- ref expected,
153
- ref found,
154
- } => {
155
- write ! (
156
- f,
157
- "Expected a value of type {} but found {} instead" ,
158
- expected, found
159
- )
160
- }
161
147
Error :: InvalidValueForType {
162
148
ref expected,
163
149
ref found,
@@ -244,7 +230,7 @@ impl fmt::Display for Error {
244
230
}
245
231
}
246
232
247
- #[ derive( Clone , Copy , Debug , PartialEq ) ]
233
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
248
234
pub struct Position {
249
235
pub line : usize ,
250
236
pub col : usize ,
@@ -271,17 +257,52 @@ impl de::Error for Error {
271
257
272
258
#[ cold]
273
259
fn invalid_type ( unexp : de:: Unexpected , exp : & dyn de:: Expected ) -> Self {
274
- Error :: ExpectedDifferentType {
275
- expected : exp. to_string ( ) ,
276
- found : unexp. into ( ) ,
277
- }
260
+ // Invalid type and invalid value are merged given their similarity in ron
261
+ Self :: invalid_value ( unexp, exp)
278
262
}
279
263
280
264
#[ cold]
281
265
fn invalid_value ( unexp : de:: Unexpected , exp : & dyn de:: Expected ) -> Self {
266
+ struct UnexpectedSerdeTypeValue < ' a > ( de:: Unexpected < ' a > ) ;
267
+
268
+ impl < ' a > fmt:: Display for UnexpectedSerdeTypeValue < ' a > {
269
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
270
+ use de:: Unexpected :: * ;
271
+
272
+ match self . 0 {
273
+ Bool ( b) => write ! ( f, "the boolean `{}`" , b) ,
274
+ Unsigned ( i) => write ! ( f, "the unsigned integer `{}`" , i) ,
275
+ Signed ( i) => write ! ( f, "the signed integer `{}`" , i) ,
276
+ Float ( n) => write ! ( f, "the floating point number `{}`" , n) ,
277
+ Char ( c) => write ! ( f, "the UTF-8 character `{}`" , c) ,
278
+ Str ( s) => write ! ( f, "the string {:?}" , s) ,
279
+ Bytes ( b) => {
280
+ f. write_str ( "the bytes b\" " ) ?;
281
+
282
+ for b in b {
283
+ write ! ( f, "\\ x{:02x}" , b) ?;
284
+ }
285
+
286
+ f. write_str ( "\" " )
287
+ }
288
+ Unit => write ! ( f, "a unit value" ) ,
289
+ Option => write ! ( f, "an optional value" ) ,
290
+ NewtypeStruct => write ! ( f, "a newtype struct" ) ,
291
+ Seq => write ! ( f, "a sequence" ) ,
292
+ Map => write ! ( f, "a map" ) ,
293
+ Enum => write ! ( f, "an enum" ) ,
294
+ UnitVariant => write ! ( f, "a unit variant" ) ,
295
+ NewtypeVariant => write ! ( f, "a newtype variant" ) ,
296
+ TupleVariant => write ! ( f, "a tuple variant" ) ,
297
+ StructVariant => write ! ( f, "a struct variant" ) ,
298
+ Other ( other) => f. write_str ( other) ,
299
+ }
300
+ }
301
+ }
302
+
282
303
Error :: InvalidValueForType {
283
304
expected : exp. to_string ( ) ,
284
- found : unexp. into ( ) ,
305
+ found : UnexpectedSerdeTypeValue ( unexp) . to_string ( ) ,
285
306
}
286
307
}
287
308
@@ -358,90 +379,6 @@ impl From<SpannedError> for Error {
358
379
}
359
380
}
360
381
361
- #[ derive( Clone , Debug , PartialEq ) ]
362
- pub enum UnexpectedSerdeTypeValue {
363
- Bool ( bool ) ,
364
- Unsigned ( u64 ) ,
365
- Signed ( i64 ) ,
366
- Float ( f64 ) ,
367
- Char ( char ) ,
368
- Str ( String ) ,
369
- Bytes ( Vec < u8 > ) ,
370
- Unit ,
371
- Option ,
372
- NewtypeStruct ,
373
- Seq ,
374
- Map ,
375
- Enum ,
376
- UnitVariant ,
377
- NewtypeVariant ,
378
- TupleVariant ,
379
- StructVariant ,
380
- Other ( String ) ,
381
- }
382
-
383
- impl < ' a > From < de:: Unexpected < ' a > > for UnexpectedSerdeTypeValue {
384
- fn from ( unexpected : de:: Unexpected < ' a > ) -> Self {
385
- use de:: Unexpected :: * ;
386
-
387
- match unexpected {
388
- Bool ( b) => Self :: Bool ( b) ,
389
- Unsigned ( u) => Self :: Unsigned ( u) ,
390
- Signed ( s) => Self :: Signed ( s) ,
391
- Float ( f) => Self :: Float ( f) ,
392
- Char ( c) => Self :: Char ( c) ,
393
- Str ( s) => Self :: Str ( s. to_owned ( ) ) ,
394
- Bytes ( b) => Self :: Bytes ( b. to_owned ( ) ) ,
395
- Unit => Self :: Unit ,
396
- Option => Self :: Option ,
397
- NewtypeStruct => Self :: NewtypeStruct ,
398
- Seq => Self :: Seq ,
399
- Map => Self :: Map ,
400
- Enum => Self :: Enum ,
401
- UnitVariant => Self :: UnitVariant ,
402
- NewtypeVariant => Self :: NewtypeVariant ,
403
- TupleVariant => Self :: TupleVariant ,
404
- StructVariant => Self :: StructVariant ,
405
- Other ( o) => Self :: Other ( o. to_owned ( ) ) ,
406
- }
407
- }
408
- }
409
-
410
- impl fmt:: Display for UnexpectedSerdeTypeValue {
411
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
412
- use self :: UnexpectedSerdeTypeValue :: * ;
413
-
414
- match * self {
415
- Bool ( b) => write ! ( f, "the boolean `{}`" , b) ,
416
- Unsigned ( i) => write ! ( f, "the integer `{}`" , i) ,
417
- Signed ( i) => write ! ( f, "the integer `{}`" , i) ,
418
- Float ( n) => write ! ( f, "the floating point number `{}`" , n) ,
419
- Char ( c) => write ! ( f, "the UTF-8 character `{}`" , c) ,
420
- Str ( ref s) => write ! ( f, "the string {:?}" , s) ,
421
- Bytes ( ref b) => {
422
- f. write_str ( "the bytes b\" " ) ?;
423
-
424
- for b in b {
425
- write ! ( f, "\\ x{:02x}" , b) ?;
426
- }
427
-
428
- f. write_str ( "\" " )
429
- }
430
- Unit => write ! ( f, "a unit value" ) ,
431
- Option => write ! ( f, "an optional value" ) ,
432
- NewtypeStruct => write ! ( f, "a newtype struct" ) ,
433
- Seq => write ! ( f, "a sequence" ) ,
434
- Map => write ! ( f, "a map" ) ,
435
- Enum => write ! ( f, "an enum" ) ,
436
- UnitVariant => write ! ( f, "a unit variant" ) ,
437
- NewtypeVariant => write ! ( f, "a newtype variant" ) ,
438
- TupleVariant => write ! ( f, "a tuple variant" ) ,
439
- StructVariant => write ! ( f, "a struct variant" ) ,
440
- Other ( ref other) => f. write_str ( other) ,
441
- }
442
- }
443
- }
444
-
445
382
struct OneOf {
446
383
alts : & ' static [ & ' static str ] ,
447
384
none : & ' static str ,
0 commit comments