Skip to content

Commit e007bea

Browse files
committed
Added missing documentation, removed ToBson.
1 parent 90acaba commit e007bea

File tree

5 files changed

+27
-30
lines changed

5 files changed

+27
-30
lines changed

src/bson.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_serialize::hex::ToHex;
2929

3030
use spec::{ElementType, BinarySubtype};
3131

32+
/// Possible BSON value types.
3233
#[derive(Debug, Clone)]
3334
pub enum Bson {
3435
FloatingPoint(f64),
@@ -49,10 +50,13 @@ pub enum Bson {
4950
UtcDatetime(DateTime<UTC>),
5051
}
5152

53+
/// Alias for `Vec<Bson>`.
5254
pub type Array = Vec<Bson>;
55+
/// Alias for `BTreeMap<String, Bson>`.
5356
pub type Document = BTreeMap<String, Bson>;
5457

5558
impl Bson {
59+
/// Get the `ElementType` of this value.
5660
pub fn element_type(&self) -> ElementType {
5761
match self {
5862
&Bson::FloatingPoint(..) => ElementType::FloatingPoint,
@@ -74,6 +78,7 @@ impl Bson {
7478
}
7579
}
7680

81+
/// Convert this value to the best approximate `Json`.
7782
pub fn to_json(&self) -> json::Json {
7883
match self {
7984
&Bson::FloatingPoint(v) => json::Json::F64(v),
@@ -120,6 +125,7 @@ impl Bson {
120125
}
121126
}
122127

128+
/// Create a `Bson` from a `Json`.
123129
pub fn from_json(j: &json::Json) -> Bson {
124130
match j {
125131
&json::Json::I64(x) => Bson::I64(x),
@@ -133,25 +139,3 @@ impl Bson {
133139
}
134140
}
135141
}
136-
137-
pub trait ToBson {
138-
fn to_bson(&self) -> Bson;
139-
}
140-
141-
impl ToBson for str {
142-
fn to_bson(&self) -> Bson {
143-
Bson::String(self.to_owned())
144-
}
145-
}
146-
147-
impl<T: ToBson> ToBson for [T] {
148-
fn to_bson(&self) -> Bson {
149-
Bson::Array(self.iter().map(|x| x.to_bson()).collect())
150-
}
151-
}
152-
153-
impl<T: ToBson> ToBson for BTreeMap<String, T> {
154-
fn to_bson(&self) -> Bson {
155-
Bson::Document(self.iter().map(|(k, v)| (k.clone(), v.to_bson())).collect())
156-
}
157-
}

src/decoder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use chrono::{DateTime, NaiveDateTime, UTC};
3030
use spec::{self, BinarySubtype};
3131
use bson::{Bson, Array, Document};
3232

33+
/// Possible errors that can arise during decoding.
3334
#[derive(Debug)]
3435
pub enum DecoderError {
3536
IoError(io::Error),
@@ -89,6 +90,7 @@ impl error::Error for DecoderError {
8990
}
9091
}
9192

93+
/// Alias for `Result<T, DecoderError>`.
9294
pub type DecoderResult<T> = Result<T, DecoderError>;
9395

9496
fn read_string<R: Read + ?Sized>(reader: &mut R) -> DecoderResult<String> {
@@ -123,6 +125,7 @@ fn read_i64<R: Read + ?Sized>(reader: &mut R) -> DecoderResult<i64> {
123125
reader.read_i64::<LittleEndian>().map_err(From::from)
124126
}
125127

128+
/// Attempt to decode a `Document` from a byte stream.
126129
pub fn decode_document<R: Read + ?Sized>(reader: &mut R) -> DecoderResult<Document> {
127130
let mut doc = Document::new();
128131

src/encoder.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use byteorder::{self, LittleEndian, WriteBytesExt};
2929

3030
use bson::Bson;
3131

32+
/// Possible errors that can arise during encoding.
3233
#[derive(Debug)]
3334
pub enum EncoderError {
3435
IoError(io::Error),
@@ -67,6 +68,7 @@ impl error::Error for EncoderError {
6768
}
6869
}
6970

71+
/// Alias for `Result<T, EncoderError>`.
7072
pub type EncoderResult<T> = Result<T, EncoderError>;
7173

7274
fn write_string<W: Write + ?Sized>(writer: &mut W, s: &str) -> EncoderResult<()> {
@@ -109,13 +111,17 @@ fn encode_array<W: Write + ?Sized>(writer: &mut W, arr: &[Bson]) -> EncoderResul
109111
Ok(())
110112
}
111113

114+
/// Attempt to encode a `Document` into a byte stream.
115+
///
116+
/// Can encode any type which is iterable as `(key: &str, value: &Bson)` pairs,
117+
/// which generally means most maps.
112118
pub fn encode_document
113-
<'a, W: Write + ?Sized, D: IntoIterator<Item=(&'a String, &'a Bson)>>
119+
<'a, S: AsRef<str> + 'a, W: Write + ?Sized, D: IntoIterator<Item=(&'a S, &'a Bson)>>
114120
(writer: &mut W, doc: D) -> EncoderResult<()>
115121
{
116122
let mut buf = Vec::new();
117123
for (key, val) in doc.into_iter() {
118-
try!(encode_bson(&mut buf, key, val));
124+
try!(encode_bson(&mut buf, key.as_ref(), val));
119125
}
120126

121127
try!(write_i32(writer, (buf.len() + mem::size_of::<i32>() + mem::size_of::<u8>()) as i32));

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! BSON is a binary format in which zero or more key/value pairs are stored as a single entity.
2323
//! We call this entity a document.
2424
//!
25-
//! This library supports Version 1.0 of BSON standard.
25+
//! This library supports version 1.0 of the [BSON standard](http://bsonspec.org/spec.html).
2626
//!
2727
//! ## Basic usage
2828
//!
@@ -46,7 +46,7 @@ extern crate rustc_serialize;
4646
extern crate chrono;
4747
extern crate byteorder;
4848

49-
pub use self::bson::{Bson, ToBson, Document, Array};
49+
pub use self::bson::{Bson, Document, Array};
5050
pub use self::encoder::{encode_document, EncoderResult, EncoderError};
5151
pub use self::decoder::{decode_document, DecoderResult, DecoderError};
5252

src/spec.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2020
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
//! BSON Specification Version 1.0
23-
/// http://bsonspec.org/spec.html
22+
//! Constants derived from the [BSON Specification Version 1.0](http://bsonspec.org/spec.html).
2423
2524
use std::convert::From;
2625

@@ -52,6 +51,9 @@ pub const BINARY_SUBTYPE_UUID_OLD : u8 = 0x03;
5251
pub const BINARY_SUBTYPE_UUID : u8 = 0x04;
5352
pub const BINARY_SUBTYPE_MD5 : u8 = 0x05;
5453

54+
/// All available BSON element types.
55+
///
56+
/// Not all element types are representable by the `Bson` type.
5557
#[repr(u8)]
5658
#[derive(Debug, Eq, PartialEq)]
5759
pub enum ElementType {
@@ -60,14 +62,14 @@ pub enum ElementType {
6062
EmbeddedDocument = ELEMENT_TYPE_EMBEDDED_DOCUMENT,
6163
Array = ELEMENT_TYPE_ARRAY,
6264
Binary = ELEMENT_TYPE_BINARY,
63-
#[warn(deprecated)]
65+
/// Deprecated.
6466
Undefined = ELEMENT_TYPE_UNDEFINED,
6567
ObjectId = ELEMENT_TYPE_OBJECT_ID,
6668
Boolean = ELEMENT_TYPE_BOOLEAN,
6769
UtcDatetime = ELEMENT_TYPE_UTC_DATETIME,
6870
NullValue = ELEMENT_TYPE_NULL_VALUE,
6971
RegularExpression = ELEMENT_TYPE_REGULAR_EXPRESSION,
70-
#[warn(deprecated)]
72+
/// Deprecated.
7173
DbPointer = ELEMENT_TYPE_DBPOINTER,
7274
JavaScriptCode = ELEMENT_TYPE_JAVASCRIPT_CODE,
7375
Deprecated = ELEMENT_TYPE_DEPRECATED,
@@ -81,6 +83,7 @@ pub enum ElementType {
8183
}
8284

8385
impl ElementType {
86+
/// Attempt to convert from a `u8`.
8487
#[inline]
8588
pub fn from(tag: u8) -> Option<ElementType> {
8689
use self::ElementType::*;
@@ -110,6 +113,7 @@ impl ElementType {
110113
}
111114
}
112115

116+
/// The available binary subtypes, plus a user-defined slot.
113117
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
114118
pub enum BinarySubtype {
115119
Generic,

0 commit comments

Comments
 (0)