Repository: serde-rs/serde
File: serde/src/de/value.rs (Rust)

Code before:
    HashSet,
    btree_map,
    btree_set,
    hash_map,
    hash_set,
};
use std::hash::Hash;
use std::vec;

use de;
use bytes;

///////////////////////////////////////////////////////////////////////////////

/// This represents all the possible errors that can occur using the `ValueDeserializer`.
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
    /// The value had some syntatic error.
    SyntaxError,

    /// EOF while deserializing a value.
    EndOfStreamError,

    /// Unknown field in struct.
    UnknownFieldError(String),

    /// Struct is missing a field.
    MissingFieldError(&'static str),
}

impl de::Error for Error {
    fn syntax(_: &str) -> Self { Error::SyntaxError }
    fn end_of_stream() -> Self { Error::EndOfStreamError }
    fn unknown_field(field: &str) -> Self { Error::UnknownFieldError(String::from(field)) }
    fn missing_field(field: &'static str) -> Self { Error::MissingFieldError(field) }
}

///////////////////////////////////////////////////////////////////////////////

/// This trait converts primitive types into a deserializer.
pub trait ValueDeserializer {
    /// The actual deserializer type.
    type Deserializer: de::Deserializer<Error=Error>;

    /// Convert this value into a deserializer.
    fn into_deserializer(self) -> Self::Deserializer;
}

///////////////////////////////////////////////////////////////////////////////

impl ValueDeserializer for () {

Review comment (suggestion):
The `ValueDeserializer`s are generic in respect to the error now, so the `Length` variant is indeed not used by the existing implementations. This is a remnant of take one to avoid unifying `Length` into `Syntax`, because all nested deserializers are required to go through `value::Error` there which is not the case in take two. If we remove it and some function calls `de::Error::length_mismatch` the error would be unified into the `Syntax` variant though, but I guess this is intentional because it's explicitly stated in the `de::Error` trait. This could occur when a `ValueDeserializer` is not nested in another deserializer and instead used standalone (which I assume is rare). But if `ValueDeserializer`s are nested the `value::Error` enum isn't necessary anyway.


Code after:
    HashSet,
    btree_map,
    btree_set,
    hash_map,
    hash_set,
};
use std::hash::Hash;
use std::vec;
use std::marker::PhantomData;

use de;
use bytes;

///////////////////////////////////////////////////////////////////////////////

/// This represents all the possible errors that can occur using the `ValueDeserializer`.
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
    /// The value had some syntatic error.
    Syntax(String),

    /// The value had an incorrect type.
    Type(de::Type),

    /// The value had an invalid length.
    Length(usize),

    /// EOF while deserializing a value.
    EndOfStream,

    /// Unknown field in struct.
    UnknownField(String),

    /// Struct is missing a field.
    MissingField(&'static str),
}

impl de::Error for Error {
    fn syntax(msg: &str) -> Self { Error::Syntax(String::from(msg)) }
    fn type_mismatch(type_: de::Type) -> Self { Error::Type(type_) }
    fn length_mismatch(len: usize) -> Self { Error::Length(len) }
    fn end_of_stream() -> Self { Error::EndOfStream }
    fn unknown_field(field: &str) -> Self { Error::UnknownField(String::from(field)) }
    fn missing_field(field: &'static str) -> Self { Error::MissingField(field) }
}

///////////////////////////////////////////////////////////////////////////////

/// This trait converts primitive types into a deserializer.
pub trait ValueDeserializer<E: de::Error = Error> {
    /// The actual deserializer type.