Skip to content

Commit adbbc97

Browse files
committed
Merge pull request #34 from kyeah/master
Update serde to 0.7
2 parents d5c12b0 + 228b5a2 commit adbbc97

File tree

7 files changed

+82
-75
lines changed

7 files changed

+82
-75
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ libc = "0.1"
1717
rand = "0.3"
1818
rust-crypto = "0.2.31"
1919
rustc-serialize = "0.3"
20-
serde = "~0.6.1"
20+
serde = "0.7"
2121
time = "0.1"
2222

2323
[dependencies.num]

serde-tests/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ build = "build.rs"
66

77

88
[dependencies]
9-
serde = "0.6"
9+
serde = "0.7"
1010
bson = { path = "..", features = ["serde"] }
1111

1212
[build-dependencies]
13-
syntex = "0.22"
14-
serde_codegen = "0.6"
13+
syntex = "^0.29.0"
14+
serde_codegen = "0.7"
1515

1616
[lib]
1717
name = "serde_tests"

serde-tests/test.rs.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn application_decode_error() {
121121
fn deserialize<D: Deserializer>(d: &mut D) -> Result<Range10, D::Error> {
122122
let x: usize = try!(Deserialize::deserialize(d));
123123
if x > 10 {
124-
Err(serde::de::Error::syntax("more than 10"))
124+
Err(serde::de::Error::invalid_value("more than 10"))
125125
} else {
126126
Ok(Range10(x))
127127
}

src/decoder/error.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ pub enum DecoderError {
1414
// An unexpected field was found.
1515
UnknownField(String),
1616
// There was an error with the syntactical structure of the BSON.
17-
SyntaxError,
17+
SyntaxError(String),
1818
// The end of the BSON input was reached too soon.
1919
EndOfStream,
20+
Unknown(String),
2021
}
2122

2223
impl From<io::Error> for DecoderError {
@@ -52,8 +53,9 @@ impl fmt::Display for DecoderError {
5253
write!(fmt, "expected a field of type `{}`", field_type)
5354
}
5455
DecoderError::UnknownField(ref field) => write!(fmt, "unknown field `{}`", field),
55-
DecoderError::SyntaxError => write!(fmt, "syntax error"),
56+
DecoderError::SyntaxError(ref inner) => inner.fmt(fmt),
5657
DecoderError::EndOfStream => write!(fmt, "end of stream"),
58+
DecoderError::Unknown(ref inner) => inner.fmt(fmt),
5759
}
5860
}
5961
}
@@ -67,8 +69,9 @@ impl error::Error for DecoderError {
6769
DecoderError::InvalidArrayKey(_, _) => "invalid array key",
6870
DecoderError::ExpectedField(_) => "expected a field",
6971
DecoderError::UnknownField(_) => "found an unknown field",
70-
DecoderError::SyntaxError => "syntax error",
72+
DecoderError::SyntaxError(ref inner) => inner,
7173
DecoderError::EndOfStream => "end of stream",
74+
DecoderError::Unknown(ref inner) => inner,
7275
}
7376
}
7477
fn cause(&self) -> Option<&error::Error> {
@@ -81,8 +84,12 @@ impl error::Error for DecoderError {
8184
}
8285

8386
impl de::Error for DecoderError {
84-
fn syntax(_: &str) -> DecoderError {
85-
DecoderError::SyntaxError
87+
fn custom<T: Into<String>>(msg: T) -> DecoderError {
88+
DecoderError::Unknown(msg.into())
89+
}
90+
91+
fn invalid_value(msg: &str) -> DecoderError {
92+
DecoderError::SyntaxError(msg.to_owned())
8693
}
8794

8895
fn end_of_stream() -> DecoderError {

src/decoder/serde.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ impl Deserialize for ObjectId {
1515
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
1616
where D: Deserializer,
1717
{
18-
deserializer.visit_map(BsonVisitor)
18+
deserializer.deserialize_map(BsonVisitor)
1919
.and_then(|bson| if let Bson::ObjectId(oid) = bson {
2020
Ok(oid)
2121
} else {
22-
Err(de::Error::syntax(&format!("expected objectId extended document, found {}", bson)))
22+
Err(de::Error::invalid_value(&format!("expected objectId extended document, found {}", bson)))
2323
})
2424
}
2525
}
@@ -29,11 +29,11 @@ impl Deserialize for OrderedDocument {
2929
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
3030
where D: Deserializer,
3131
{
32-
deserializer.visit_map(BsonVisitor)
32+
deserializer.deserialize_map(BsonVisitor)
3333
.and_then(|bson| if let Bson::Document(doc) = bson {
3434
Ok(doc)
3535
} else {
36-
Err(de::Error::syntax(&format!("expected document, found extended JSON data type: {}", bson)))
36+
Err(de::Error::invalid_value(&format!("expected document, found extended JSON data type: {}", bson)))
3737
})
3838
}
3939
}
@@ -43,7 +43,7 @@ impl Deserialize for Bson {
4343
fn deserialize<D>(deserializer: &mut D) -> Result<Bson, D::Error>
4444
where D: Deserializer,
4545
{
46-
deserializer.visit(BsonVisitor)
46+
deserializer.deserialize(BsonVisitor)
4747
}
4848
}
4949

@@ -149,7 +149,7 @@ impl Deserializer for Decoder {
149149
type Error = DecoderError;
150150

151151
#[inline]
152-
fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
152+
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
153153
where V: Visitor,
154154
{
155155
let value = match self.value.take() {
@@ -195,7 +195,7 @@ impl Deserializer for Decoder {
195195
}
196196

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

208208
#[inline]
209-
fn visit_enum<V>(&mut self,
209+
fn deserialize_enum<V>(&mut self,
210210
_name: &str,
211211
_variants: &'static [&'static str],
212212
mut visitor: V) -> DecoderResult<V::Value>
213213
where V: EnumVisitor,
214214
{
215215
let value = match self.value.take() {
216216
Some(Bson::Document(value)) => value,
217-
Some(_) => { return Err(de::Error::syntax("expected an enum")); }
217+
Some(_) => { return Err(de::Error::invalid_value("expected an enum")); }
218218
None => { return Err(de::Error::end_of_stream()); }
219219
};
220220

221221
let mut iter = value.into_iter();
222222

223223
let (variant, value) = match iter.next() {
224224
Some(v) => v,
225-
None => return Err(de::Error::syntax("expected a variant name")),
225+
None => return Err(de::Error::invalid_value("expected a variant name")),
226226
};
227227

228228
// enums are encoded in json as maps with a single key:value pair
229229
match iter.next() {
230-
Some(_) => Err(de::Error::syntax("expected map")),
230+
Some(_) => Err(de::Error::invalid_value("expected map")),
231231
None => visitor.visit(VariantDecoder {
232232
de: self,
233233
val: Some(value),
@@ -237,18 +237,13 @@ impl Deserializer for Decoder {
237237
}
238238

239239
#[inline]
240-
fn visit_newtype_struct<V>(&mut self,
240+
fn deserialize_newtype_struct<V>(&mut self,
241241
_name: &'static str,
242242
mut visitor: V) -> DecoderResult<V::Value>
243243
where V: Visitor,
244244
{
245245
visitor.visit_newtype_struct(self)
246246
}
247-
248-
#[inline]
249-
fn format() -> &'static str {
250-
"json"
251-
}
252247
}
253248

254249
struct VariantDecoder<'a> {
@@ -286,7 +281,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
286281
{
287282
if let Bson::Array(fields) = try!(self.val.take()
288283
.ok_or(DecoderError::EndOfStream)) {
289-
Deserializer::visit(
284+
Deserializer::deserialize(
290285
&mut SeqDecoder {
291286
de: self.de,
292287
len: fields.len(),
@@ -295,7 +290,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
295290
visitor,
296291
)
297292
} else {
298-
Err(de::Error::syntax("expected a tuple"))
293+
Err(de::Error::invalid_value("expected a tuple"))
299294
}
300295
}
301296

@@ -306,7 +301,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
306301
{
307302
if let Bson::Document(fields) = try!(self.val.take()
308303
.ok_or(DecoderError::EndOfStream)) {
309-
Deserializer::visit(
304+
Deserializer::deserialize(
310305
&mut MapDecoder {
311306
de: self.de,
312307
len: fields.len(),
@@ -316,7 +311,7 @@ impl<'a> VariantVisitor for VariantDecoder<'a> {
316311
visitor,
317312
)
318313
} else {
319-
Err(de::Error::syntax("expected a struct"))
314+
Err(de::Error::invalid_value("expected a struct"))
320315
}
321316
}
322317
}
@@ -331,7 +326,7 @@ impl<'a> Deserializer for SeqDecoder<'a> {
331326
type Error = DecoderError;
332327

333328
#[inline]
334-
fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
329+
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
335330
where V: Visitor,
336331
{
337332
if self.len == 0 {
@@ -362,7 +357,7 @@ impl<'a> SeqVisitor for SeqDecoder<'a> {
362357
if self.len == 0 {
363358
Ok(())
364359
} else {
365-
Err(de::Error::length_mismatch(self.len))
360+
Err(de::Error::invalid_length(self.len))
366361
}
367362
}
368363

@@ -422,13 +417,13 @@ impl<'a> MapVisitor for MapDecoder<'a> {
422417
impl Deserializer for UnitDecoder {
423418
type Error = DecoderError;
424419

425-
fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
420+
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
426421
where V: Visitor,
427422
{
428423
visitor.visit_unit()
429424
}
430425

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

449444
#[inline]
450-
fn visit<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
445+
fn deserialize<V>(&mut self, mut visitor: V) -> DecoderResult<V::Value>
451446
where V: Visitor,
452447
{
453448
visitor.visit_map(self)

src/encoder/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{io, error, fmt};
22
use byteorder;
3+
use serde::ser;
34
use super::serde::State;
45

56
/// Possible errors that can arise during encoding.
@@ -9,6 +10,7 @@ pub enum EncoderError {
910
InvalidMapKeyType(State),
1011
InvalidState(State),
1112
EmptyState,
13+
Unknown(String),
1214
}
1315

1416
impl From<io::Error> for EncoderError {
@@ -30,6 +32,7 @@ impl fmt::Display for EncoderError {
3032
&EncoderError::InvalidMapKeyType(ref inner) => write!(fmt, "Invalid map key type: {:?}", inner),
3133
&EncoderError::InvalidState(ref inner) => write!(fmt, "Invalid state emitted: {:?}", inner),
3234
&EncoderError::EmptyState => write!(fmt, "No state emitted"),
35+
&EncoderError::Unknown(ref inner) => inner.fmt(fmt),
3336
}
3437
}
3538
}
@@ -41,6 +44,7 @@ impl error::Error for EncoderError {
4144
&EncoderError::InvalidMapKeyType(_) => "Invalid map key type",
4245
&EncoderError::InvalidState(_) => "Invalid state emitted",
4346
&EncoderError::EmptyState => "No state emitted",
47+
&EncoderError::Unknown(ref inner) => inner,
4448
}
4549
}
4650
fn cause(&self) -> Option<&error::Error> {
@@ -51,5 +55,11 @@ impl error::Error for EncoderError {
5155
}
5256
}
5357

58+
impl ser::Error for EncoderError {
59+
fn custom<T: Into<String>>(msg: T) -> EncoderError {
60+
EncoderError::Unknown(msg.into())
61+
}
62+
}
63+
5464
/// Alias for `Result<T, EncoderError>`.
5565
pub type EncoderResult<T> = Result<T, EncoderError>;

0 commit comments

Comments
 (0)