Add a string argument to Error::syntax()

This commit is contained in:
Erick Tryzelaar 2015-08-07 08:08:56 -07:00
parent 2aeb51ad51
commit 7fb2bd50bf
13 changed files with 51 additions and 45 deletions

View File

@ -275,7 +275,7 @@ to generate an error for a few common error conditions. Here's how it could be u
fn visit_string<E>(&mut self, _: String) -> Result<i32, E>
where E: Error,
{
Err(serde::de::Error::syntax())
Err(serde::de::Error::syntax("expect a string"))
}
...
@ -366,7 +366,7 @@ impl serde::Deserialize for PointField {
match value {
"x" => Ok(Field::X),
"y" => Ok(Field::Y),
_ => Err(serde::de::Error::syntax()),
_ => Err(serde::de::Error::syntax("expected x or y")),
}
}
}

View File

@ -85,7 +85,7 @@ impl Visitor for BoolVisitor {
match s.trim() {
"true" => Ok(true),
"false" => Ok(false),
_ => Err(Error::syntax()),
_ => Err(Error::syntax("expected `true` or `false`")),
}
}
}
@ -108,7 +108,7 @@ macro_rules! impl_deserialize_num_method {
{
match FromPrimitive::$from_method(v) {
Some(v) => Ok(v),
None => Err(Error::syntax()),
None => Err(Error::syntax("expected a number")),
}
}
}
@ -149,7 +149,7 @@ impl<
fn visit_str<E>(&mut self, v: &str) -> Result<T, E>
where E: Error,
{
str::FromStr::from_str(v.trim()).or(Err(Error::syntax()))
str::FromStr::from_str(v.trim()).or(Err(Error::syntax("expected a str")))
}
}
@ -200,7 +200,7 @@ impl Visitor for CharVisitor {
let mut iter = v.chars();
if let Some(v) = iter.next() {
if iter.next().is_some() {
Err(Error::syntax())
Err(Error::syntax("expected a character"))
} else {
Ok(v)
}
@ -243,7 +243,7 @@ impl Visitor for StringVisitor {
{
match str::from_utf8(v) {
Ok(s) => Ok(s.to_string()),
Err(_) => Err(Error::syntax()),
Err(_) => Err(Error::syntax("expected utf8 `&[u8]`")),
}
}
@ -252,7 +252,7 @@ impl Visitor for StringVisitor {
{
match String::from_utf8(v) {
Ok(s) => Ok(s),
Err(_) => Err(Error::syntax()),
Err(_) => Err(Error::syntax("expected utf8 `&[u8]`")),
}
}
}
@ -848,7 +848,7 @@ impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable +
fn deserialize<D>(deserializer: &mut D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
let value = try!(Deserialize::deserialize(deserializer));
if value == Zero::zero() {
return Err(Error::syntax())
return Err(Error::syntax("expected a non-zero value"))
}
unsafe {
Ok(NonZero::new(value))
@ -900,7 +900,7 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
_ => {
match str::from_utf8(value) {
Ok(value) => Err(Error::unknown_field(value)),
Err(_) => Err(Error::syntax()),
Err(_) => Err(Error::syntax("expected a `&[u8]`")),
}
}
}

View File

@ -6,7 +6,7 @@ pub mod value;
///////////////////////////////////////////////////////////////////////////////
pub trait Error {
fn syntax() -> Self;
fn syntax(msg: &str) -> Self;
fn end_of_stream() -> Self;
@ -273,7 +273,7 @@ pub trait Deserializer {
_visitor: V) -> Result<V::Value, Self::Error>
where V: EnumVisitor,
{
Err(Error::syntax())
Err(Error::syntax("expected an enum"))
}
/// This method hints that the `Deserialize` type is expecting a `Vec<u8>`. This allows
@ -304,7 +304,7 @@ pub trait Visitor {
fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected a bool"))
}
fn visit_isize<E>(&mut self, v: isize) -> Result<Self::Value, E>
@ -334,7 +334,7 @@ pub trait Visitor {
fn visit_i64<E>(&mut self, _v: i64) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected a i64"))
}
fn visit_usize<E>(&mut self, v: usize) -> Result<Self::Value, E>
@ -364,7 +364,7 @@ pub trait Visitor {
fn visit_u64<E>(&mut self, _v: u64) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected a u64"))
}
fn visit_f32<E>(&mut self, v: f32) -> Result<Self::Value, E>
@ -376,7 +376,7 @@ pub trait Visitor {
fn visit_f64<E>(&mut self, _v: f64) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected a f64"))
}
#[inline]
@ -391,7 +391,7 @@ pub trait Visitor {
fn visit_str<E>(&mut self, _v: &str) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected a str"))
}
#[inline]
@ -404,7 +404,7 @@ pub trait Visitor {
fn visit_unit<E>(&mut self) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected a unit"))
}
#[inline]
@ -417,37 +417,37 @@ pub trait Visitor {
fn visit_none<E>(&mut self) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected an Option::None"))
}
fn visit_some<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
where D: Deserializer,
{
Err(Error::syntax())
Err(Error::syntax("expected an Option::Some"))
}
fn visit_newtype_struct<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
where D: Deserializer,
{
Err(Error::syntax())
Err(Error::syntax("expected a newtype struct"))
}
fn visit_seq<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
where V: SeqVisitor,
{
Err(Error::syntax())
Err(Error::syntax("expected a sequence"))
}
fn visit_map<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
where V: MapVisitor,
{
Err(Error::syntax())
Err(Error::syntax("expected a map"))
}
fn visit_bytes<E>(&mut self, _v: &[u8]) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::syntax())
Err(Error::syntax("expected a &[u8]"))
}
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<Self::Value, E>
@ -593,7 +593,7 @@ pub trait VariantVisitor {
/// `visit_unit` is called when deserializing a variant with no values.
fn visit_unit(&mut self) -> Result<(), Self::Error> {
Err(Error::syntax())
Err(Error::syntax("expected a univ variant"))
}
/// `visit_newtype` is called when deserializing a variant with a single value. By default this
@ -612,7 +612,7 @@ pub trait VariantVisitor {
_visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
Err(Error::syntax())
Err(Error::syntax("expected a tuple variant"))
}
/// `visit_struct` is called when deserializing a struct-like variant.
@ -621,7 +621,7 @@ pub trait VariantVisitor {
_visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
Err(Error::syntax())
Err(Error::syntax("expected a struct variant"))
}
}

View File

@ -24,9 +24,9 @@ pub enum Error {
}
impl de::Error for Error {
fn syntax() -> Self { Error::SyntaxError }
fn syntax(_: &str) -> Self { Error::SyntaxError }
fn end_of_stream() -> Self { Error::EndOfStreamError }
fn unknown_field(field: &str) -> Self { Error::UnknownFieldError(field.to_string()) }
fn unknown_field(field: &str) -> Self { Error::UnknownFieldError(String::from(field)) }
fn missing_field(field: &'static str) -> Self { Error::MissingFieldError(field) }
}
@ -374,7 +374,7 @@ impl<I, K, V> de::MapVisitor for MapDeserializer<I, K, V>
let mut de = value.into_deserializer();
de::Deserialize::deserialize(&mut de)
}
None => Err(de::Error::syntax())
None => Err(de::Error::syntax("expected a map value"))
}
}

View File

@ -804,7 +804,7 @@ fn deserialize_field_visitor(
let index_body = quote_expr!(cx,
match value {
$index_field_arms
_ => { Err(::serde::de::Error::syntax()) }
_ => { Err(::serde::de::Error::syntax("expected a field")) }
}
);
@ -903,7 +903,13 @@ fn deserialize_field_visitor(
// TODO: would be better to generate a byte string literal match
match ::std::str::from_utf8(value) {
Ok(s) => self.visit_str(s),
_ => Err(::serde::de::Error::syntax()),
_ => {
Err(
::serde::de::Error::syntax(
"could not convert a byte string to a String"
)
)
}
}
}
}

View File

@ -152,7 +152,7 @@ impl From<de::value::Error> for Error {
fn from(error: de::value::Error) -> Error {
match error {
de::value::Error::SyntaxError => {
de::Error::syntax()
Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)
}
de::value::Error::EndOfStreamError => {
de::Error::end_of_stream()
@ -168,7 +168,7 @@ impl From<de::value::Error> for Error {
}
impl de::Error for Error {
fn syntax() -> Error {
fn syntax(_: &str) -> Error {
Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)
}

View File

@ -700,7 +700,7 @@ impl de::Deserializer for Deserializer {
{
let value = match self.value.take() {
Some(Value::Object(value)) => value,
Some(_) => { return Err(de::Error::syntax()); }
Some(_) => { return Err(de::Error::syntax("expected an enum")); }
None => { return Err(de::Error::end_of_stream()); }
};
@ -708,12 +708,12 @@ impl de::Deserializer for Deserializer {
let (variant, value) = match iter.next() {
Some(v) => v,
None => return Err(de::Error::syntax()),
None => return Err(de::Error::syntax("expected a variant name")),
};
// enums are encoded in json as maps with a single key:value pair
match iter.next() {
Some(_) => Err(de::Error::syntax()),
Some(_) => Err(de::Error::syntax("expected map")),
None => visitor.visit(VariantDeserializer {
de: self,
val: Some(value),
@ -768,7 +768,7 @@ impl<'a> de::VariantVisitor for VariantDeserializer<'a> {
visitor,
)
} else {
Err(de::Error::syntax())
Err(de::Error::syntax("expected a tuple"))
}
}
@ -788,7 +788,7 @@ impl<'a> de::VariantVisitor for VariantDeserializer<'a> {
visitor,
)
} else {
Err(de::Error::syntax())
Err(de::Error::syntax("expected a struct"))
}
}
}

View File

@ -20,7 +20,7 @@ pub enum Error {
}
impl serde::de::Error for Error {
fn syntax() -> Error { Error::SyntaxError }
fn syntax(_: &str) -> Error { Error::SyntaxError }
fn end_of_stream() -> Error { Error::EndOfStreamError }

View File

@ -17,7 +17,7 @@ pub enum Error {
}
impl serde::de::Error for Error {
fn syntax() -> Error { Error::SyntaxError }
fn syntax(_: &str) -> Error { Error::SyntaxError }
fn end_of_stream() -> Error { Error::EndOfStream }

View File

@ -33,7 +33,7 @@ pub enum Error {
}
impl serde::de::Error for Error {
fn syntax() -> Error { Error::SyntaxError }
fn syntax(_: &str) -> Error { Error::SyntaxError }
fn end_of_stream() -> Error { Error::EndOfStream }

View File

@ -15,7 +15,7 @@ pub enum Error {
}
impl serde::de::Error for Error {
fn syntax() -> Error { Error::SyntaxError }
fn syntax(_: &str) -> Error { Error::SyntaxError }
fn end_of_stream() -> Error { Error::EndOfStreamError }

View File

@ -9,7 +9,7 @@ use serde_json;
struct Error;
impl serde::de::Error for Error {
fn syntax() -> Error { Error }
fn syntax(_: &str) -> Error { Error }
fn end_of_stream() -> Error { Error }

View File

@ -68,7 +68,7 @@ enum Error {
}
impl de::Error for Error {
fn syntax() -> Error { Error::SyntaxError }
fn syntax(_: &str) -> Error { Error::SyntaxError }
fn end_of_stream() -> Error { Error::EndOfStreamError }