#[cfg(feature = "std")]
use std::error;
#[cfg(not(feature = "std"))]
use error;
#[cfg(all(not(feature = "std"), feature = "collections"))]
use collections::{String, Vec};
use core::fmt::{self, Display};
use core::marker::PhantomData;
#[doc(hidden)]
pub mod impls;
pub mod value;
mod from_primitive;
#[doc(hidden)]
pub mod private;
#[cfg(any(feature = "std", feature = "collections"))]
mod content;
pub trait Error: Sized + error::Error {
fn custom<T: Display>(msg: T) -> Self;
fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self {
struct InvalidType<'a> {
unexp: Unexpected<'a>,
exp: &'a Expected,
}
impl<'a> Display for InvalidType<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "invalid type: {}, expected {}", self.unexp, self.exp)
}
}
Error::custom(InvalidType {
unexp: unexp,
exp: exp,
})
}
fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self {
struct InvalidValue<'a> {
unexp: Unexpected<'a>,
exp: &'a Expected,
}
impl<'a> Display for InvalidValue<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "invalid value: {}, expected {}", self.unexp, self.exp)
}
}
Error::custom(InvalidValue {
unexp: unexp,
exp: exp,
})
}
fn invalid_length(len: usize, exp: &Expected) -> Self {
struct InvalidLength<'a> {
len: usize,
exp: &'a Expected,
}
impl<'a> Display for InvalidLength<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "invalid length {}, expected {}", self.len, self.exp)
}
}
Error::custom(InvalidLength {
len: len,
exp: exp,
})
}
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
struct UnknownVariant<'a> {
variant: &'a str,
expected: &'static [&'static str],
}
impl<'a> Display for UnknownVariant<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
if self.expected.is_empty() {
write!(formatter,
"unknown variant `{}`, there are no variants",
self.variant)
} else {
write!(formatter,
"unknown variant `{}`, expected {}",
self.variant,
OneOf { names: self.expected })
}
}
}
Error::custom(UnknownVariant {
variant: variant,
expected: expected,
})
}
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
struct UnknownField<'a> {
field: &'a str,
expected: &'static [&'static str],
}
impl<'a> Display for UnknownField<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
if self.expected.is_empty() {
write!(formatter,
"unknown field `{}`, there are no fields",
self.field)
} else {
write!(formatter,
"unknown field `{}`, expected {}",
self.field,
OneOf { names: self.expected })
}
}
}
Error::custom(UnknownField {
field: field,
expected: expected,
})
}
fn missing_field(field: &'static str) -> Self {
struct MissingField {
field: &'static str,
}
impl Display for MissingField {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "missing field `{}`", self.field)
}
}
Error::custom(MissingField { field: field })
}
fn duplicate_field(field: &'static str) -> Self {
struct DuplicateField {
field: &'static str,
}
impl Display for DuplicateField {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "duplicate field `{}`", self.field)
}
}
Error::custom(DuplicateField { field: field })
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum Unexpected<'a> {
Bool(bool),
Unsigned(u64),
Signed(i64),
Float(f64),
Char(char),
Str(&'a str),
Bytes(&'a [u8]),
Unit,
Option,
NewtypeStruct,
Seq,
Map,
Enum,
UnitVariant,
NewtypeVariant,
TupleVariant,
StructVariant,
Other(&'a str),
}
impl<'a> fmt::Display for Unexpected<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use self::Unexpected::*;
match *self {
Bool(b) => write!(formatter, "boolean `{}`", b),
Unsigned(i) => write!(formatter, "integer `{}`", i),
Signed(i) => write!(formatter, "integer `{}`", i),
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"),
Seq => write!(formatter, "sequence"),
Map => write!(formatter, "map"),
Enum => write!(formatter, "enum"),
UnitVariant => write!(formatter, "unit variant"),
NewtypeVariant => write!(formatter, "newtype variant"),
TupleVariant => write!(formatter, "tuple variant"),
StructVariant => write!(formatter, "struct variant"),
Other(other) => formatter.write_str(other),
}
}
}
pub trait Expected {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result;
}
impl<T> Expected for T
where T: Visitor
{
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.expecting(formatter)
}
}
impl<'a> Expected for &'a str {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(self)
}
}
impl<'a> Display for Expected + 'a {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Expected::fmt(self, formatter)
}
}
pub trait Deserialize: Sized {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer;
}
pub trait DeserializeSeed: Sized {
type Value;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer;
}
impl<T> DeserializeSeed for PhantomData<T>
where T: Deserialize
{
type Value = T;
#[inline]
fn deserialize<D>(self, deserializer: D) -> Result<T, D::Error>
where D: Deserializer
{
T::deserialize(deserializer)
}
}
pub trait Deserializer: Sized {
type Error: Error;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_unit_struct<V>(self,
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_newtype_struct<V>(self,
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_seq_fixed_size<V>(self,
len: usize,
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_tuple_struct<V>(self,
name: &'static str,
len: usize,
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
fn deserialize_struct<V>(self,
name: &'static str,
fields: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_struct_field<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_enum<V>(self,
name: &'static str,
variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
}
pub trait Visitor: Sized {
type Value;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result;
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where E: Error
{
Err(Error::invalid_type(Unexpected::Bool(v), &self))
}
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
where E: Error
{
self.visit_i64(v as i64)
}
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
where E: Error
{
self.visit_i64(v as i64)
}
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where E: Error
{
self.visit_i64(v as i64)
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where E: Error
{
Err(Error::invalid_type(Unexpected::Signed(v), &self))
}
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
where E: Error
{
self.visit_u64(v as u64)
}
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
where E: Error
{
self.visit_u64(v as u64)
}
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
where E: Error
{
self.visit_u64(v as u64)
}
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where E: Error
{
Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
}
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
where E: Error
{
self.visit_f64(v as f64)
}
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where E: Error
{
Err(Error::invalid_type(Unexpected::Float(v), &self))
}
#[inline]
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
where E: Error
{
self.visit_str(::utils::encode_utf8(v).as_str())
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: Error
{
Err(Error::invalid_type(Unexpected::Str(v), &self))
}
#[inline]
#[cfg(any(feature = "std", feature = "collections"))]
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where E: Error
{
self.visit_str(&v)
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
where E: Error
{
Err(Error::invalid_type(Unexpected::Unit, &self))
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where E: Error
{
Err(Error::invalid_type(Unexpected::Option, &self))
}
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
{
let _ = deserializer;
Err(Error::invalid_type(Unexpected::Option, &self))
}
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
{
let _ = deserializer;
Err(Error::invalid_type(Unexpected::NewtypeStruct, &self))
}
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: SeqVisitor
{
let _ = visitor;
Err(Error::invalid_type(Unexpected::Seq, &self))
}
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: MapVisitor
{
let _ = visitor;
Err(Error::invalid_type(Unexpected::Map, &self))
}
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: EnumVisitor
{
let _ = visitor;
Err(Error::invalid_type(Unexpected::Enum, &self))
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where E: Error
{
let _ = v;
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
}
#[cfg(any(feature = "std", feature = "collections"))]
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where E: Error
{
self.visit_bytes(&v)
}
}
pub trait SeqVisitor {
type Error: Error;
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: DeserializeSeed;
#[inline]
fn visit<T>(&mut self) -> Result<Option<T>, Self::Error>
where T: Deserialize
{
self.visit_seed(PhantomData)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}
impl<'a, V> SeqVisitor for &'a mut V
where V: SeqVisitor
{
type Error = V::Error;
#[inline]
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, V::Error>
where T: DeserializeSeed
{
(**self).visit_seed(seed)
}
#[inline]
fn visit<T>(&mut self) -> Result<Option<T>, V::Error>
where T: Deserialize
{
(**self).visit()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
}
}
pub trait MapVisitor {
type Error: Error;
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
where K: DeserializeSeed;
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
where V: DeserializeSeed;
#[inline]
fn visit_seed<K, V>(&mut self,
kseed: K,
vseed: V)
-> Result<Option<(K::Value, V::Value)>, Self::Error>
where K: DeserializeSeed,
V: DeserializeSeed
{
match try!(self.visit_key_seed(kseed)) {
Some(key) => {
let value = try!(self.visit_value_seed(vseed));
Ok(Some((key, value)))
}
None => Ok(None),
}
}
#[inline]
fn visit_key<K>(&mut self) -> Result<Option<K>, Self::Error>
where K: Deserialize
{
self.visit_key_seed(PhantomData)
}
#[inline]
fn visit_value<V>(&mut self) -> Result<V, Self::Error>
where V: Deserialize
{
self.visit_value_seed(PhantomData)
}
#[inline]
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
where K: Deserialize,
V: Deserialize
{
self.visit_seed(PhantomData, PhantomData)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}
impl<'a, V_> MapVisitor for &'a mut V_
where V_: MapVisitor
{
type Error = V_::Error;
#[inline]
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
where K: DeserializeSeed
{
(**self).visit_key_seed(seed)
}
#[inline]
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
where V: DeserializeSeed
{
(**self).visit_value_seed(seed)
}
#[inline]
fn visit_seed<K, V>(&mut self,
kseed: K,
vseed: V)
-> Result<Option<(K::Value, V::Value)>, Self::Error>
where K: DeserializeSeed,
V: DeserializeSeed
{
(**self).visit_seed(kseed, vseed)
}
#[inline]
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, V_::Error>
where K: Deserialize,
V: Deserialize
{
(**self).visit()
}
#[inline]
fn visit_key<K>(&mut self) -> Result<Option<K>, V_::Error>
where K: Deserialize
{
(**self).visit_key()
}
#[inline]
fn visit_value<V>(&mut self) -> Result<V, V_::Error>
where V: Deserialize
{
(**self).visit_value()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
}
}
pub trait EnumVisitor: Sized {
type Error: Error;
type Variant: VariantVisitor<Error = Self::Error>;
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
where V: DeserializeSeed;
#[inline]
fn visit_variant<V>(self) -> Result<(V, Self::Variant), Self::Error>
where V: Deserialize
{
self.visit_variant_seed(PhantomData)
}
}
pub trait VariantVisitor: Sized {
type Error: Error;
fn visit_unit(self) -> Result<(), Self::Error>;
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
where T: DeserializeSeed;
#[inline]
fn visit_newtype<T>(self) -> Result<T, Self::Error>
where T: Deserialize
{
self.visit_newtype_seed(PhantomData)
}
fn visit_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn visit_struct<V>(self,
fields: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
}
struct OneOf {
names: &'static [&'static str],
}
impl Display for OneOf {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.names.len() {
0 => panic!(), 1 => write!(formatter, "`{}`", self.names[0]),
2 => write!(formatter, "`{}` or `{}`", self.names[0], self.names[1]),
_ => {
try!(write!(formatter, "one of "));
for (i, alt) in self.names.iter().enumerate() {
if i > 0 {
try!(write!(formatter, ", "));
}
try!(write!(formatter, "`{}`", alt));
}
Ok(())
}
}
}
}