Skip to content

Commit 8838cad

Browse files
committed
Removed duplicate of serde::de::Unexpected from ron error API
1 parent 64e45f1 commit 8838cad

File tree

5 files changed

+53
-116
lines changed

5 files changed

+53
-116
lines changed

src/error.rs

+44-107
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Err
33

44
/// This type represents all possible errors that can occur when
55
/// serializing or deserializing RON data.
6-
#[derive(Clone, Debug, PartialEq)]
6+
#[derive(Clone, Debug, PartialEq, Eq)]
77
pub struct SpannedError {
88
pub code: Error,
99
pub position: Position,
@@ -12,7 +12,7 @@ pub struct SpannedError {
1212
pub type Result<T, E = Error> = std::result::Result<T, E>;
1313
pub type SpannedResult<T> = std::result::Result<T, SpannedError>;
1414

15-
#[derive(Clone, Debug, PartialEq)]
15+
#[derive(Clone, Debug, PartialEq, Eq)]
1616
#[non_exhaustive]
1717
pub enum Error {
1818
Io(String),
@@ -59,13 +59,9 @@ pub enum Error {
5959
Utf8Error(Utf8Error),
6060
TrailingCharacters,
6161

62-
ExpectedDifferentType {
63-
expected: String,
64-
found: UnexpectedSerdeTypeValue,
65-
},
6662
InvalidValueForType {
6763
expected: String,
68-
found: UnexpectedSerdeTypeValue,
64+
found: String,
6965
},
7066
ExpectedDifferentLength {
7167
expected: String,
@@ -148,16 +144,6 @@ impl fmt::Display for Error {
148144
}
149145
Error::UnexpectedByte(ref byte) => write!(f, "Unexpected byte {:?}", byte),
150146
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-
}
161147
Error::InvalidValueForType {
162148
ref expected,
163149
ref found,
@@ -244,7 +230,7 @@ impl fmt::Display for Error {
244230
}
245231
}
246232

247-
#[derive(Clone, Copy, Debug, PartialEq)]
233+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
248234
pub struct Position {
249235
pub line: usize,
250236
pub col: usize,
@@ -271,17 +257,52 @@ impl de::Error for Error {
271257

272258
#[cold]
273259
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)
278262
}
279263

280264
#[cold]
281265
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+
282303
Error::InvalidValueForType {
283304
expected: exp.to_string(),
284-
found: unexp.into(),
305+
found: UnexpectedSerdeTypeValue(unexp).to_string(),
285306
}
286307
}
287308

@@ -358,90 +379,6 @@ impl From<SpannedError> for Error {
358379
}
359380
}
360381

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-
445382
struct OneOf {
446383
alts: &'static [&'static str],
447384
none: &'static str,

src/parse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,11 @@ impl<'a> Bytes<'a> {
656656
}
657657

658658
pub fn peek(&self) -> Option<u8> {
659-
self.bytes.get(0).cloned()
659+
self.bytes.first().cloned()
660660
}
661661

662662
pub fn peek_or_eof(&self) -> Result<u8> {
663-
self.bytes.get(0).cloned().ok_or(Error::Eof)
663+
self.bytes.first().cloned().ok_or(Error::Eof)
664664
}
665665

666666
pub fn signed_integer<T>(&mut self) -> Result<T>

tests/203_error_positions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::num::NonZeroU32;
22

3-
use ron::error::{Error, Position, SpannedError, UnexpectedSerdeTypeValue};
3+
use ron::error::{Error, Position, SpannedError};
44
use serde::{
55
de::{Deserialize, Error as DeError, Unexpected},
66
Deserializer,
@@ -27,9 +27,9 @@ fn test_error_positions() {
2727
assert_eq!(
2828
ron::from_str::<TypeError>(" ()"),
2929
Err(SpannedError {
30-
code: Error::ExpectedDifferentType {
30+
code: Error::InvalidValueForType {
3131
expected: String::from("impossible"),
32-
found: UnexpectedSerdeTypeValue::Unit,
32+
found: String::from("a unit value"),
3333
},
3434
position: Position { line: 1, col: 3 },
3535
})
@@ -40,7 +40,7 @@ fn test_error_positions() {
4040
Err(SpannedError {
4141
code: Error::InvalidValueForType {
4242
expected: String::from("a nonzero u32"),
43-
found: UnexpectedSerdeTypeValue::Unsigned(0),
43+
found: String::from("the unsigned integer `0`"),
4444
},
4545
position: Position { line: 1, col: 28 },
4646
})

tests/359_deserialize_seed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ fn test_deserialize_seed() {
4747
assert_eq!(
4848
ron::Options::default().from_str_seed("'a'", Seed(42)),
4949
Err(ron::error::SpannedError {
50-
code: ron::Error::ExpectedDifferentType {
50+
code: ron::Error::InvalidValueForType {
5151
expected: String::from("an integer"),
52-
found: ron::error::UnexpectedSerdeTypeValue::Str(String::from("a")),
52+
found: String::from("the string \"a\""),
5353
},
5454
position: ron::error::Position { line: 1, col: 4 },
5555
})

tests/roundtrip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn roundtrip_pretty() {
8282

8383
#[test]
8484
fn roundtrip_sep_tuple_members() {
85-
#[derive(Debug, Deserialize, PartialEq, Serialize)]
85+
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
8686
pub enum FileOrMem {
8787
File(String),
8888
Memory,

0 commit comments

Comments
 (0)