Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #102 from dtolnay/invalid
Browse files Browse the repository at this point in the history
Fix panic in invalid_type
  • Loading branch information
dtolnay committed Sep 3, 2018
2 parents 81eb976 + cfbdb31 commit 8fa3e2c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/de.rs
Expand Up @@ -557,12 +557,14 @@ where
}

fn invalid_type(event: &Event, exp: &Expected) -> Error {
enum Void {}

struct InvalidType<'a> {
exp: &'a Expected,
}

impl<'de, 'a> Visitor<'de> for InvalidType<'a> {
type Value = Error;
type Value = Void;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.exp.fmt(formatter)
Expand All @@ -573,7 +575,10 @@ fn invalid_type(event: &Event, exp: &Expected) -> Error {
Event::Alias(_) => unreachable!(),
Event::Scalar(ref v, style, ref tag) => {
let get_type = InvalidType { exp: exp };
visit_scalar(v, style, tag, get_type).unwrap()
match visit_scalar(v, style, tag, get_type) {
Ok(void) => match void {},
Err(invalid_type) => invalid_type,
}
}
Event::SequenceStart => de::Error::invalid_type(Unexpected::Seq, exp),
Event::MappingStart => de::Error::invalid_type(Unexpected::Map, exp),
Expand Down
12 changes: 12 additions & 0 deletions tests/test_error.rs
Expand Up @@ -245,3 +245,15 @@ fn test_no_location() {

assert_eq!(utf8_location.is_none(), true);
}

#[test]
fn test_invalid_scalar_type() {
#[derive(Deserialize, Debug)]
struct S {
x: [(); 1],
}

let yaml = "x:\n";
let expected = "x: invalid type: unit value, expected an array of length 1 at line 2 column 1";
test_error::<S>(yaml, expected);
}

0 comments on commit 8fa3e2c

Please sign in to comment.