Skip to content
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
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ bson = "0.14"
```

## Usage
Link the library in _main.rs_:

```rust
#[macro_use(bson, doc)]
extern crate bson;
```

Prepare your struct for Serde serialization:

Expand Down Expand Up @@ -63,6 +57,8 @@ if let bson::Bson::Document(document) = serialized_person {
Deserialize the struct:

```rust
use bson::doc;

// Read the document from a MongoDB collection
let person_document = mongoCollection.find_one(Some(doc! { "_id": bson::oid::ObjectId::with_string("12345").expect("Id not valid") }), None)?
.expect("Document not found");
Expand Down
2 changes: 1 addition & 1 deletion examples/decode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern crate bson;
use bson;

use std::fs::File;

Expand Down
6 changes: 3 additions & 3 deletions examples/encode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate bson;
extern crate chrono;

use bson::{decode_document, encode_document, oid, Array, Bson, Document};
use std::io::Cursor;

use bson::{decode_document, encode_document, oid, Array, Bson, Document};
use chrono;

fn main() {
let mut doc = Document::new();
doc.insert("foo".to_string(), Bson::String("bar".to_string()));
Expand Down
4 changes: 2 additions & 2 deletions serde-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
name = "serde-tests"
version = "0.1.0"
authors = ["Kevin Yeh <[email protected]>"]
edition = "2018"

[dependencies]
serde = "1.0"
bson = { path = ".." }
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] }

[lib]
name = "serde_tests"
Expand Down
4 changes: 0 additions & 4 deletions serde-tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
extern crate bson;
extern crate serde;
#[macro_use] extern crate serde_derive;

use std::collections::{BTreeMap, HashSet};
use serde::{Deserialize, Serialize, Deserializer};
use serde::de::Unexpected;
Expand Down
7 changes: 2 additions & 5 deletions src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::ops::{Deref, DerefMut};
use chrono::offset::TimeZone;
use chrono::{DateTime, Timelike, Utc};
use hex;
use serde_json::Value;
use serde_json::{Value, json};

#[cfg(feature = "decimal128")]
use crate::decimal128::Decimal128;
Expand Down Expand Up @@ -751,7 +751,6 @@ impl Bson {
///
/// ```rust,ignore
/// use serde::{Serialize, Deserialize};
/// extern crate bson;
/// use bson::TimeStamp;
///
/// #[derive(Serialize, Deserialize)]
Expand All @@ -770,9 +769,7 @@ pub struct TimeStamp {
/// Just a helper for convenience
///
/// ```rust,ignore
/// #[macro_use]
/// extern crate serde_derive;
/// extern crate bson;
/// use serde::{Serialize, Deserialize};
/// use bson::UtcDateTime;
///
/// #[derive(Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use chrono::Timelike;
use crate::bson::Bson;
#[cfg(feature = "decimal128")]
use crate::decimal128::Decimal128;
use crate::serde::Serialize;
use ::serde::Serialize;

fn write_string<W: Write + ?Sized>(writer: &mut W, s: &str) -> EncoderResult<()> {
writer.write_i32::<LittleEndian>(s.len() as i32 + 1)?;
Expand Down
13 changes: 0 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
//! ## Basic usage
//!
//! ```rust
//! extern crate bson;
//! use bson::{decode_document, encode_document, Bson, Document};
//! use std::io::Cursor;
//!
Expand All @@ -42,18 +41,6 @@
//! }
//! ```

extern crate byteorder;
extern crate chrono;
extern crate hex;
extern crate linked_hash_map;
extern crate rand;
extern crate serde;
#[macro_use]
extern crate serde_json;
#[cfg(feature = "decimal128")]
extern crate decimal;
extern crate md5;
extern crate time;

pub use self::bson::{Array, Bson, Document, TimeStamp, UtcDateTime};
#[cfg(feature = "decimal128")]
Expand Down
60 changes: 29 additions & 31 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
/// Construct a bson::BSON value from a literal.
///
/// ```rust
/// # #[macro_use]
/// # extern crate bson;
/// # use bson::bson;
/// #
/// # fn main() {
/// let value = bson!({
Expand Down Expand Up @@ -40,32 +39,32 @@ macro_rules! bson {

// Next element is `null`.
(@array [$($elems:expr,)*] null $($rest:tt)*) => {
bson!(@array [$($elems,)* bson!(null)] $($rest)*)
$crate::bson!(@array [$($elems,)* $crate::bson!(null)] $($rest)*)
};

// Next element is an array.
(@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
bson!(@array [$($elems,)* bson!([$($array)*])] $($rest)*)
$crate::bson!(@array [$($elems,)* $crate::bson!([$($array)*])] $($rest)*)
};

// Next element is a map.
(@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
bson!(@array [$($elems,)* bson!({$($map)*})] $($rest)*)
$crate::bson!(@array [$($elems,)* $crate::bson!({$($map)*})] $($rest)*)
};

// Next element is an expression followed by comma.
(@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
bson!(@array [$($elems,)* bson!($next),] $($rest)*)
$crate::bson!(@array [$($elems,)* $crate::bson!($next),] $($rest)*)
};

// Last element is an expression with no trailing comma.
(@array [$($elems:expr,)*] $last:expr) => {
bson!(@array [$($elems,)* bson!($last)])
$crate::bson!(@array [$($elems,)* $crate::bson!($last)])
};

// Comma after the most recent element.
(@array [$($elems:expr),*] , $($rest:tt)*) => {
bson!(@array [$($elems,)*] $($rest)*)
$crate::bson!(@array [$($elems,)*] $($rest)*)
};

//////////////////////////////////////////////////////////////////////////
Expand All @@ -84,7 +83,7 @@ macro_rules! bson {
// Insert the current entry followed by trailing comma.
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
$object.insert_bson(($($key)+).into(), $value);
bson!(@object $object () ($($rest)*) ($($rest)*));
$crate::bson!(@object $object () ($($rest)*) ($($rest)*));
};

// Insert the last entry without trailing comma.
Expand All @@ -94,65 +93,65 @@ macro_rules! bson {

// Next value is `null`.
(@object $object:ident ($($key:tt)+) (=> null $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!(null)) $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!(null)) $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!(null)) $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!(null)) $($rest)*);
};

// Next value is an array.
(@object $object:ident ($($key:tt)+) (=> [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!([$($array)*])) $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!([$($array)*])) $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!([$($array)*])) $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!([$($array)*])) $($rest)*);
};

// Next value is a map.
(@object $object:ident ($($key:tt)+) (=> {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!({$($map)*})) $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!({$($map)*})) $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!({$($map)*})) $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!({$($map)*})) $($rest)*);
};

// Next value is an expression followed by comma.
(@object $object:ident ($($key:tt)+) (=> $value:expr , $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!($value)) , $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)) , $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!($value)) , $($rest)*);
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)) , $($rest)*);
};

// Last value is an expression with no trailing comma.
(@object $object:ident ($($key:tt)+) (=> $value:expr) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!($value)));
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)));
};

(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
bson!(@object $object [$($key)+] (bson!($value)));
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)));
};

// Missing value for last entry. Trigger a reasonable error message.
(@object $object:ident ($($key:tt)+) (=>) $copy:tt) => {
// "unexpected end of macro invocation"
bson!();
$crate::bson!();
};

(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
// "unexpected end of macro invocation"
bson!();
$crate::bson!();
};

// Missing key-value separator and value for last entry.
// Trigger a reasonable error message.
(@object $object:ident ($($key:tt)+) () $copy:tt) => {
// "unexpected end of macro invocation"
bson!();
$crate::bson!();
};

// Misplaced key-value separator. Trigger a reasonable error message.
Expand All @@ -175,16 +174,16 @@ macro_rules! bson {
// Key is fully parenthesized. This avoids clippy double_parens false
// positives because the parenthesization may be necessary here.
(@object $object:ident () (($key:expr) => $($rest:tt)*) $copy:tt) => {
bson!(@object $object ($key) (=> $($rest)*) (=> $($rest)*));
$crate::bson!(@object $object ($key) (=> $($rest)*) (=> $($rest)*));
};

(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
bson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
$crate::bson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
};

// Munch a token into the current key.
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
bson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
$crate::bson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
};

//////////////////////////////////////////////////////////////////////////
Expand All @@ -202,15 +201,15 @@ macro_rules! bson {
};

([ $($tt:tt)+ ]) => {
$crate::Bson::Array(bson!(@array [] $($tt)+))
$crate::Bson::Array($crate::bson!(@array [] $($tt)+))
};

({}) => {
$crate::Bson::Document(doc!{})
$crate::Bson::Document($crate::doc!{})
};

({$($tt:tt)+}) => {
$crate::Bson::Document(doc!{$($tt)+});
$crate::Bson::Document($crate::doc!{$($tt)+});
};

// Any Serialize type: numbers, strings, struct literals, variables etc.
Expand All @@ -223,8 +222,7 @@ macro_rules! bson {
/// Construct a bson::Document value.
///
/// ```rust
/// # #[macro_use]
/// # extern crate bson;
/// # use bson::doc;
/// #
/// # fn main() {
/// let value = doc! {
Expand All @@ -244,7 +242,7 @@ macro_rules! doc {
() => {{ $crate::Document::new() }};
( $($tt:tt)+ ) => {{
let mut object = $crate::Document::new();
bson!(@object object () ($($tt)+) ($($tt)+));
$crate::bson!(@object object () ($($tt)+) ($($tt)+));
object
}};
}
10 changes: 0 additions & 10 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
#[macro_use(assert_matches)]
extern crate assert_matches;
#[macro_use(bson, doc)]
extern crate bson;
extern crate byteorder;
extern crate chrono;
#[cfg(feature = "decimal128")]
extern crate decimal;
extern crate hex;

mod modules;
6 changes: 2 additions & 4 deletions tests/modules/bson.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
extern crate serde_json;

use self::serde_json::{Value, json};
use bson::{Bson, Document, oid::ObjectId, spec::BinarySubtype};
use serde_json::{Value, json};
use bson::{Bson, Document, doc, oid::ObjectId, spec::BinarySubtype};

#[test]
fn to_json() {
Expand Down
1 change: 1 addition & 0 deletions tests/modules/encoder_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "decimal128")]
use bson::decimal128::Decimal128;
use bson::doc;
use bson::oid::ObjectId;
use bson::spec::BinarySubtype;
use bson::{decode_document, decode_document_utf8_lossy, encode_document, Bson};
Expand Down
1 change: 1 addition & 0 deletions tests/modules/macros.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bson::oid::ObjectId;
use bson::spec::BinarySubtype;
use bson::Bson;
use bson::doc;
use chrono::offset::Utc;
use hex;

Expand Down
1 change: 1 addition & 0 deletions tests/modules/ordered.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "decimal128")]
use bson::decimal128::Decimal128;
use bson::doc;
use bson::oid::ObjectId;
use bson::spec::BinarySubtype;
use bson::ValueAccessError;
Expand Down
1 change: 1 addition & 0 deletions tests/modules/ser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use assert_matches::assert_matches;
#[cfg(feature = "decimal128")]
use bson::decimal128::Decimal128;
use bson::oid::ObjectId;
Expand Down
11 changes: 2 additions & 9 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#[macro_use]
extern crate bson;
extern crate chrono;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_bytes;

use bson::{Bson, Decoder, Encoder};
use bson::{Bson, Decoder, Encoder, bson, doc};
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

use std::collections::BTreeMap;

Expand Down