Document serde:🇩🇪:value

This commit is contained in:
Erick Tryzelaar 2015-09-01 18:41:48 -07:00
parent 4be4f60f21
commit 55f5f397d7

View File

@ -1,4 +1,4 @@
#![allow(missing_docs)] //! This module supports deserializing from primitives with the `ValueDeserializer` trait.
use std::collections::{ use std::collections::{
BTreeMap, BTreeMap,
@ -18,11 +18,19 @@ use bytes;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// This represents all the possible errors that can occur using the `ValueDeserializer`.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Error { pub enum Error {
/// The value had some syntatic error.
SyntaxError, SyntaxError,
/// EOF while deserializing a value.
EndOfStreamError, EndOfStreamError,
/// Unknown field in struct.
UnknownFieldError(String), UnknownFieldError(String),
/// Struct is missing a field.
MissingFieldError(&'static str), MissingFieldError(&'static str),
} }
@ -35,9 +43,12 @@ impl de::Error for Error {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// This trait converts primitive types into a deserializer.
pub trait ValueDeserializer { pub trait ValueDeserializer {
/// The actual deserializer type.
type Deserializer: de::Deserializer<Error=Error>; type Deserializer: de::Deserializer<Error=Error>;
/// Convert this value into a deserializer.
fn into_deserializer(self) -> Self::Deserializer; fn into_deserializer(self) -> Self::Deserializer;
} }
@ -74,6 +85,7 @@ impl de::Deserializer for UnitDeserializer {
macro_rules! primitive_deserializer { macro_rules! primitive_deserializer {
($ty:ty, $name:ident, $method:ident) => { ($ty:ty, $name:ident, $method:ident) => {
/// A helper deserializer that deserializes a number.
pub struct $name(Option<$ty>); pub struct $name(Option<$ty>);
impl ValueDeserializer for $ty { impl ValueDeserializer for $ty {
@ -214,12 +226,14 @@ impl<'a> de::VariantVisitor for StringDeserializer {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// A helper deserializer that deserializes a sequence.
pub struct SeqDeserializer<I> { pub struct SeqDeserializer<I> {
iter: I, iter: I,
len: usize, len: usize,
} }
impl<I> SeqDeserializer<I> { impl<I> SeqDeserializer<I> {
/// Construct a new `SeqDeserializer<I>`.
pub fn new(iter: I, len: usize) -> Self { pub fn new(iter: I, len: usize) -> Self {
SeqDeserializer { SeqDeserializer {
iter: iter, iter: iter,
@ -310,6 +324,7 @@ impl<T> ValueDeserializer for HashSet<T>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// A helper deserializer that deserializes a map.
pub struct MapDeserializer<I, K, V> pub struct MapDeserializer<I, K, V>
where I: Iterator<Item=(K, V)>, where I: Iterator<Item=(K, V)>,
K: ValueDeserializer, K: ValueDeserializer,
@ -325,6 +340,7 @@ impl<I, K, V> MapDeserializer<I, K, V>
K: ValueDeserializer, K: ValueDeserializer,
V: ValueDeserializer, V: ValueDeserializer,
{ {
/// Construct a new `MapDeserializer<I, K, V>`.
pub fn new(iter: I, len: usize) -> Self { pub fn new(iter: I, len: usize) -> Self {
MapDeserializer { MapDeserializer {
iter: iter, iter: iter,
@ -431,6 +447,7 @@ impl<'a> ValueDeserializer for bytes::Bytes<'a>
} }
} }
/// A helper deserializer that deserializes a `&[u8]`.
pub struct BytesDeserializer<'a> (Option<&'a [u8]>); pub struct BytesDeserializer<'a> (Option<&'a [u8]>);
impl<'a> de::Deserializer for BytesDeserializer<'a> { impl<'a> de::Deserializer for BytesDeserializer<'a> {
@ -458,6 +475,7 @@ impl ValueDeserializer for bytes::ByteBuf
} }
} }
/// A helper deserializer that deserializes a `Vec<u8>`.
pub struct ByteBufDeserializer(Option<Vec<u8>>); pub struct ByteBufDeserializer(Option<Vec<u8>>);
impl de::Deserializer for ByteBufDeserializer { impl de::Deserializer for ByteBufDeserializer {