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

Fix panic in invalid_type #102

Merged
merged 1 commit into from Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}