Skip to content

Commit e270954

Browse files
committed
add travis, add example, add ToBson
1 parent c1ff254 commit e270954

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: rust
2+
3+
script:
4+
- cargo build -v
5+
- cargo test -v

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# bson-rs
22

3+
[![Build Status](https://travis-ci.org/zonyitoo/bson-rs.svg)](https://travis-ci.org/zonyitoo/bson-rs)
4+
35
Encoding and decoding support for BSON in Rust
46

57
```toml

examples/encode.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
extern crate bson;
2+
extern crate chrono;
3+
4+
use std::io::Cursor;
5+
use bson::{Bson, Document, Array, Encoder, Decoder};
6+
7+
fn main() {
8+
let mut doc = Document::new();
9+
doc.insert("foo".to_string(), Bson::String("bar".to_string()));
10+
11+
let mut arr = Array::new();
12+
arr.push(Bson::String("blah".to_string()));
13+
arr.push(Bson::UtcDatetime(chrono::UTC::now()));
14+
arr.push(Bson::ObjectId([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]));
15+
16+
doc.insert("array".to_string(), Bson::Array(arr));
17+
18+
let mut buf = Vec::new();
19+
{
20+
let mut enc = Encoder::new(&mut buf);
21+
enc.encode_document(&doc).unwrap();
22+
}
23+
24+
println!("Encoded: {:?}", buf);
25+
26+
let mut r = Cursor::new(&buf[..]);
27+
{
28+
let mut dec = Decoder::new(&mut r);
29+
let doc = dec.decode_document().unwrap();
30+
31+
println!("Decoded: {:?}", doc);
32+
}
33+
}

src/bson.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,25 @@ impl Bson {
113113
}
114114
}
115115
}
116+
117+
pub trait ToBson {
118+
fn to_bson(&self) -> Bson;
119+
}
120+
121+
impl ToBson for str {
122+
fn to_bson(&self) -> Bson {
123+
Bson::String(self.to_string())
124+
}
125+
}
126+
127+
impl<T: ToBson> ToBson for [T] {
128+
fn to_bson(&self) -> Bson {
129+
Bson::Array(self.iter().map(|x| x.to_bson()).collect())
130+
}
131+
}
132+
133+
impl<T: ToBson> ToBson for BTreeMap<String, T> {
134+
fn to_bson(&self) -> Bson {
135+
Bson::Document(self.iter().map(|(k, v)| (k.clone(), v.to_bson())).collect())
136+
}
137+
}

src/encoder.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ impl From<byteorder::Error> for EncoderError {
5050

5151
pub type EncoderResult<T> = Result<T, EncoderError>;
5252

53-
pub trait ToBson {
54-
fn code(&self) -> u8;
55-
fn serialize(&self, &mut Write) -> Result<(), EncoderError>;
56-
}
57-
5853
pub struct Encoder<'a> {
5954
writer: &'a mut Write
6055
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ extern crate rustc_serialize;
5555
extern crate chrono;
5656
extern crate byteorder;
5757

58-
pub use self::bson::{Bson, Document, Array};
59-
pub use self::encoder::{Encoder, EncoderResult};
60-
pub use self::decoder::{Decoder, DecoderResult};
58+
pub use self::bson::{Bson, ToBson, Document, Array};
59+
pub use self::encoder::{Encoder, EncoderResult, EncoderError};
60+
pub use self::decoder::{Decoder, DecoderResult, DecoderError};
6161

6262
pub mod bson;
6363
pub mod spec;

0 commit comments

Comments
 (0)