Skip to content

Use linked-hash-map to implement OrderedDocument #36

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 9, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rust-crypto = "0.2.31"
rustc-serialize = "0.3"
serde = "0.7"
time = "0.1"
linked-hash-map = "0.0.9"

[dependencies.num]
version = "~0.1.27"
Expand Down
26 changes: 13 additions & 13 deletions src/decoder/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::de::{self, Deserialize, Deserializer, Visitor,

use bson::Bson;
use oid::ObjectId;
use ordered::{OrderedDocument, OrderedDocumentIntoIterator};
use ordered::{OrderedDocument, OrderedDocumentIntoIterator, OrderedDocumentVisitor};
use super::error::{DecoderError, DecoderResult};

pub struct BsonVisitor;
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Deserialize for Bson {

impl Visitor for BsonVisitor {
type Value = Bson;

#[inline]
fn visit_bool<E>(&mut self, value: bool) -> Result<Bson, E> {
Ok(Bson::Boolean(value))
Expand All @@ -66,7 +66,7 @@ impl Visitor for BsonVisitor {
Ok(Bson::I32(value as i32))
}


#[inline]
fn visit_i32<E>(&mut self, value: i32) -> Result<Bson, E> {
Ok(Bson::I32(value))
Expand All @@ -76,59 +76,59 @@ impl Visitor for BsonVisitor {
fn visit_i64<E>(&mut self, value: i64) -> Result<Bson, E> {
Ok(Bson::I64(value))
}

#[inline]
fn visit_u64<E>(&mut self, value: u64) -> Result<Bson, E> {
Ok(Bson::I64(value as i64))
}

#[inline]
fn visit_f64<E>(&mut self, value: f64) -> Result<Bson, E> {
Ok(Bson::FloatingPoint(value))
}

#[inline]
fn visit_str<E>(&mut self, value: &str) -> Result<Bson, E>
where E: de::Error
{
self.visit_string(String::from(value))
}

#[inline]
fn visit_string<E>(&mut self, value: String) -> Result<Bson, E> {
Ok(Bson::String(value))
}

#[inline]
fn visit_none<E>(&mut self) -> Result<Bson, E> {
Ok(Bson::Null)
}

#[inline]
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Bson, D::Error>
where D: Deserializer,
{
de::Deserialize::deserialize(deserializer)
}

#[inline]
fn visit_unit<E>(&mut self) -> Result<Bson, E> {
Ok(Bson::Null)
}

#[inline]
fn visit_seq<V>(&mut self, visitor: V) -> Result<Bson, V::Error>
where V: SeqVisitor,
{
let values = try!(de::impls::VecVisitor::new().visit_seq(visitor));
Ok(Bson::Array(values))
}

#[inline]
fn visit_map<V>(&mut self, visitor: V) -> Result<Bson, V::Error>
where V: MapVisitor,
{
let values = try!(de::impls::BTreeMapVisitor::new().visit_map(visitor));
let values = try!(OrderedDocumentVisitor::new().visit_map(visitor));
Ok(Bson::from_extended_document(values.into()))
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extern crate rand;
extern crate rustc_serialize;
extern crate serde;
extern crate time;
extern crate linked_hash_map;

pub use self::bson::{Bson, Document, Array};
pub use self::encoder::{encode_document, to_bson, Encoder, EncoderResult, EncoderError};
Expand All @@ -63,4 +64,4 @@ pub mod spec;
mod bson;
mod encoder;
mod decoder;
mod ordered;
pub mod ordered;
Loading