Skip to content

Update serde to 0.7 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ libc = "0.1"
rand = "0.3"
rust-crypto = "0.2.31"
rustc-serialize = "0.3"
serde = "~0.6.1"
serde = "0.7"
time = "0.1"

[dependencies.num]
Expand Down
6 changes: 3 additions & 3 deletions serde-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ build = "build.rs"


[dependencies]
serde = "0.6"
serde = "0.7"
bson = { path = "..", features = ["serde"] }

[build-dependencies]
syntex = "0.22"
serde_codegen = "0.6"
syntex = "^0.29.0"
serde_codegen = "0.7"

[lib]
name = "serde_tests"
Expand Down
2 changes: 1 addition & 1 deletion serde-tests/test.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn application_decode_error() {
fn deserialize<D: Deserializer>(d: &mut D) -> Result<Range10, D::Error> {
let x: usize = try!(Deserialize::deserialize(d));
if x > 10 {
Err(serde::de::Error::syntax("more than 10"))
Err(serde::de::Error::invalid_value("more than 10"))
} else {
Ok(Range10(x))
}
Expand Down
17 changes: 12 additions & 5 deletions src/decoder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ pub enum DecoderError {
// An unexpected field was found.
UnknownField(String),
// There was an error with the syntactical structure of the BSON.
SyntaxError,
SyntaxError(String),
// The end of the BSON input was reached too soon.
EndOfStream,
Unknown(String),
}

impl From<io::Error> for DecoderError {
Expand Down Expand Up @@ -52,8 +53,9 @@ impl fmt::Display for DecoderError {
write!(fmt, "expected a field of type `{}`", field_type)
}
DecoderError::UnknownField(ref field) => write!(fmt, "unknown field `{}`", field),
DecoderError::SyntaxError => write!(fmt, "syntax error"),
DecoderError::SyntaxError(ref inner) => inner.fmt(fmt),
DecoderError::EndOfStream => write!(fmt, "end of stream"),
DecoderError::Unknown(ref inner) => inner.fmt(fmt),
}
}
}
Expand All @@ -67,8 +69,9 @@ impl error::Error for DecoderError {
DecoderError::InvalidArrayKey(_, _) => "invalid array key",
DecoderError::ExpectedField(_) => "expected a field",
DecoderError::UnknownField(_) => "found an unknown field",
DecoderError::SyntaxError => "syntax error",
DecoderError::SyntaxError(ref inner) => inner,
DecoderError::EndOfStream => "end of stream",
DecoderError::Unknown(ref inner) => inner,
}
}
fn cause(&self) -> Option<&error::Error> {
Expand All @@ -81,8 +84,12 @@ impl error::Error for DecoderError {
}

impl de::Error for DecoderError {
fn syntax(_: &str) -> DecoderError {
DecoderError::SyntaxError
fn custom<T: Into<String>>(msg: T) -> DecoderError {
DecoderError::Unknown(msg.into())
}

fn invalid_value(msg: &str) -> DecoderError {
DecoderError::SyntaxError(msg.to_owned())
}

fn end_of_stream() -> DecoderError {
Expand Down
47 changes: 21 additions & 26 deletions src/decoder/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ impl Deserialize for ObjectId {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer,
{
deserializer.visit_map(BsonVisitor)
deserializer.deserialize_map(BsonVisitor)
.and_then(|bson| if let Bson::ObjectId(oid) = bson {
Ok(oid)
} else {
Err(de::Error::syntax(&format!("expected objectId extended document, found {}", bson)))
Err(de::Error::invalid_value(&format!("expected objectId extended document, found {}", bson)))
})
}
}
Expand All @@ -29,11 +29,11 @@ impl Deserialize for OrderedDocument {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer,
{
deserializer.visit_map(BsonVisitor)
deserializer.deserialize_map(BsonVisitor)
.and_then(|bson| if let Bson::Document(doc) = bson {
Ok(doc)
} else {
Err(de::Error::syntax(&format!("expected document, found extended JSON data type: {}", bson)))
Err(de::Error::invalid_value(&format!("expected document, found extended JSON data type: {}", bson)))
})
}
}
Expand All @@ -43,7 +43,7 @@ impl Deserialize for Bson {
fn deserialize<D>(deserializer: &mut D) -> Result<Bson, D::Error>
where D: Deserializer,
{
deserializer.visit(BsonVisitor)
deserializer.deserialize(BsonVisitor)
}
}

Expand Down Expand Up @@ -149,7 +149,7 @@ impl Deserializer for Decoder {
type Error = DecoderError;

#[inline]
fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
where V: Visitor,
{
let value = match self.value.take() {
Expand Down Expand Up @@ -195,7 +195,7 @@ impl Deserializer for Decoder {
}

#[inline]
fn visit_option<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
fn deserialize_option<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
where V: Visitor,
{
match self.value {
Expand All @@ -206,28 +206,28 @@ impl Deserializer for Decoder {
}

#[inline]
fn visit_enum<V>(&mut self,
fn deserialize_enum<V>(&mut self,
_name: &str,
_variants: &'static [&'static str],
mut visitor: V) -> DecoderResult<V::Value>
where V: EnumVisitor,
{
let value = match self.value.take() {
Some(Bson::Document(value)) => value,
Some(_) => { return Err(de::Error::syntax("expected an enum")); }
Some(_) => { return Err(de::Error::invalid_value("expected an enum")); }
None => { return Err(de::Error::end_of_stream()); }
};

let mut iter = value.into_iter();

let (variant, value) = match iter.next() {
Some(v) => v,
None => return Err(de::Error::syntax("expected a variant name")),
None => return Err(de::Error::invalid_value("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("expected map")),
Some(_) => Err(de::Error::invalid_value("expected map")),
None => visitor.visit(VariantDecoder {
de: self,
val: Some(value),
Expand All @@ -237,18 +237,13 @@ impl Deserializer for Decoder {
}

#[inline]
fn visit_newtype_struct<V>(&mut self,
fn deserialize_newtype_struct<V>(&mut self,
_name: &'static str,
mut visitor: V) -> DecoderResult<V::Value>
where V: Visitor,
{
visitor.visit_newtype_struct(self)
}

#[inline]
fn format() -> &'static str {
"json"
}
}

struct VariantDecoder<'a> {
Expand Down Expand Up @@ -286,7 +281,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
{
if let Bson::Array(fields) = try!(self.val.take()
.ok_or(DecoderError::EndOfStream)) {
Deserializer::visit(
Deserializer::deserialize(
&mut SeqDecoder {
de: self.de,
len: fields.len(),
Expand All @@ -295,7 +290,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
visitor,
)
} else {
Err(de::Error::syntax("expected a tuple"))
Err(de::Error::invalid_value("expected a tuple"))
}
}

Expand All @@ -306,7 +301,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
{
if let Bson::Document(fields) = try!(self.val.take()
.ok_or(DecoderError::EndOfStream)) {
Deserializer::visit(
Deserializer::deserialize(
&mut MapDecoder {
de: self.de,
len: fields.len(),
Expand All @@ -316,7 +311,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
visitor,
)
} else {
Err(de::Error::syntax("expected a struct"))
Err(de::Error::invalid_value("expected a struct"))
}
}
}
Expand All @@ -331,7 +326,7 @@ impl<'a> Deserializer for SeqDecoder<'a> {
type Error = DecoderError;

#[inline]
fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
where V: Visitor,
{
if self.len == 0 {
Expand Down Expand Up @@ -362,7 +357,7 @@ impl<'a> SeqVisitor for SeqDecoder<'a> {
if self.len == 0 {
Ok(())
} else {
Err(de::Error::length_mismatch(self.len))
Err(de::Error::invalid_length(self.len))
}
}

Expand Down Expand Up @@ -422,13 +417,13 @@ impl<'a> MapVisitor for MapDecoder<'a> {
impl Deserializer for UnitDecoder {
type Error = DecoderError;

fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
where V: Visitor,
{
visitor.visit_unit()
}

fn visit_option<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
fn deserialize_option<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
where V: Visitor,
{
visitor.visit_none()
Expand All @@ -447,7 +442,7 @@ impl<'a> Deserializer for MapDecoder<'a> {
type Error = DecoderError;

#[inline]
fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
where V: Visitor,
{
visitor.visit_map(self)
Expand Down
10 changes: 10 additions & 0 deletions src/encoder/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{io, error, fmt};
use byteorder;
use serde::ser;
use super::serde::State;

/// Possible errors that can arise during encoding.
Expand All @@ -9,6 +10,7 @@ pub enum EncoderError {
InvalidMapKeyType(State),
InvalidState(State),
EmptyState,
Unknown(String),
}

impl From<io::Error> for EncoderError {
Expand All @@ -30,6 +32,7 @@ impl fmt::Display for EncoderError {
&EncoderError::InvalidMapKeyType(ref inner) => write!(fmt, "Invalid map key type: {:?}", inner),
&EncoderError::InvalidState(ref inner) => write!(fmt, "Invalid state emitted: {:?}", inner),
&EncoderError::EmptyState => write!(fmt, "No state emitted"),
&EncoderError::Unknown(ref inner) => inner.fmt(fmt),
}
}
}
Expand All @@ -41,6 +44,7 @@ impl error::Error for EncoderError {
&EncoderError::InvalidMapKeyType(_) => "Invalid map key type",
&EncoderError::InvalidState(_) => "Invalid state emitted",
&EncoderError::EmptyState => "No state emitted",
&EncoderError::Unknown(ref inner) => inner,
}
}
fn cause(&self) -> Option<&error::Error> {
Expand All @@ -51,5 +55,11 @@ impl error::Error for EncoderError {
}
}

impl ser::Error for EncoderError {
fn custom<T: Into<String>>(msg: T) -> EncoderError {
EncoderError::Unknown(msg.into())
}
}

/// Alias for `Result<T, EncoderError>`.
pub type EncoderResult<T> = Result<T, EncoderError>;
Loading