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
+8 -8
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]`")),
}
}
}
+17 -17
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"))
}
}
+3 -3
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"))
}
}