From 73a364d4fd616980cc0ed9f50d662f4dd5396bd1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 22 Jan 2017 04:57:12 -0800 Subject: [PATCH] Capture byte value for error message --- serde/src/de/impls.rs | 6 +++--- serde/src/de/mod.rs | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index aaf3e7a6..9c12a6e2 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -295,7 +295,7 @@ impl Visitor for StringVisitor { { match str::from_utf8(v) { Ok(s) => Ok(s.to_owned()), - Err(_) => Err(Error::invalid_value(Unexpected::Bytes, &self)), + Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), } } @@ -304,7 +304,7 @@ impl Visitor for StringVisitor { { match String::from_utf8(v) { Ok(s) => Ok(s), - Err(_) => Err(Error::invalid_value(Unexpected::Bytes, &self)), + Err(e) => Err(Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self)), } } } @@ -1180,7 +1180,7 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { _ => { match str::from_utf8(value) { Ok(value) => Err(Error::unknown_variant(value, VARIANTS)), - Err(_) => Err(Error::invalid_value(Unexpected::Bytes, &self)), + Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)), } } } diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 866bc840..6fca1bc4 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -237,6 +237,9 @@ pub enum Unexpected<'a> { /// The input contained a `&str` or `String` that was not expected. Str(&'a str), + /// The input contained a `&[u8]` or `Vec` that was not expected. + Bytes(&'a [u8]), + /// The input contained a unit `()` that was not expected. Unit, @@ -266,9 +269,6 @@ pub enum Unexpected<'a> { /// The input contained a struct variant that was not expected. StructVariant, - - /// The input contained a `&[u8]` or `Vec` that was not expected. - Bytes, } impl<'a> fmt::Display for Unexpected<'a> { @@ -281,6 +281,7 @@ impl<'a> fmt::Display for Unexpected<'a> { Float(f) => write!(formatter, "floating point `{}`", f), Char(c) => write!(formatter, "character `{}`", c), Str(s) => write!(formatter, "string {:?}", s), + Bytes(_) => write!(formatter, "byte array"), Unit => write!(formatter, "unit value"), Option => write!(formatter, "Option value"), NewtypeStruct => write!(formatter, "newtype struct"), @@ -291,7 +292,6 @@ impl<'a> fmt::Display for Unexpected<'a> { NewtypeVariant => write!(formatter, "newtype variant"), TupleVariant => write!(formatter, "tuple variant"), StructVariant => write!(formatter, "struct variant"), - Bytes => write!(formatter, "byte array"), } } } @@ -942,7 +942,7 @@ pub trait Visitor: Sized { where E: Error, { let _ = v; - Err(Error::invalid_type(Unexpected::Bytes, &self)) + Err(Error::invalid_type(Unexpected::Bytes(v), &self)) } /// `visit_byte_buf` deserializes a `Vec` into a `Value`.