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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ sudo: false
script:
- cargo build -v
- cargo test -v --no-fail-fast
- cargo test -v --no-fail-fast --features u2i
- cd serde-tests && cargo test -v --no-fail-fast
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ readme = "README.md"
homepage = "https://github.com/zonyitoo/bson-rs"
documentation = "https://docs.rs/crate/bson"

[features]
# no features by default
default = []
# attempt to encode unsigned types in signed types
u2i = []

[lib]
name = "bson"

Expand All @@ -26,7 +32,9 @@ linked-hash-map = "0.5"
hostname = "0.1"
hex = "0.3"
md5 = "0.3"
try_from = "0.2"

[dev-dependencies]
assert_matches = "1.2"
serde_derive = "1.0"
serde_bytes = "0.10"
11 changes: 11 additions & 0 deletions src/encoder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum EncoderError {
InvalidMapKeyType(Bson),
Unknown(String),
UnsupportedUnsignedType,
UnsignedTypesValueExceedsRange(u64),
}

impl From<io::Error> for EncoderError {
Expand All @@ -25,6 +26,14 @@ impl fmt::Display for EncoderError {
&EncoderError::InvalidMapKeyType(ref bson) => write!(fmt, "Invalid map key type: {:?}", bson),
&EncoderError::Unknown(ref inner) => inner.fmt(fmt),
&EncoderError::UnsupportedUnsignedType => write!(fmt, "BSON does not support unsigned type"),
&EncoderError::UnsignedTypesValueExceedsRange(value) => {
write!(
fmt,
"BSON does not support unsigned types.
An attempt to encode the value: {} in a signed type failed due to the values size.",
value
)
},
}
}
}
Expand All @@ -36,6 +45,8 @@ impl error::Error for EncoderError {
&EncoderError::InvalidMapKeyType(_) => "Invalid map key type",
&EncoderError::Unknown(ref inner) => inner,
&EncoderError::UnsupportedUnsignedType => "BSON does not support unsigned type",
&EncoderError::UnsignedTypesValueExceedsRange(_) => "BSON does not support unsigned types.
An attempt to encode the value: {} in a signed type failed due to the values size."
}
}
fn cause(&self) -> Option<&error::Error> {
Expand Down
36 changes: 28 additions & 8 deletions src/encoder/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::ser::{Serialize, SerializeMap, SerializeSeq, SerializeStruct, Seriali

use bson::{Array, Bson, Document, UtcDateTime};
use oid::ObjectId;
use try_from::TryFrom;

use super::{to_bson, EncoderError, EncoderResult};

Expand Down Expand Up @@ -88,8 +89,12 @@ impl Serializer for Encoder {
}

#[inline]
fn serialize_u8(self, _value: u8) -> EncoderResult<Bson> {
Err(EncoderError::UnsupportedUnsignedType)
fn serialize_u8(self, value: u8) -> EncoderResult<Bson> {
if cfg!(feature = "u2i") {
Ok(Bson::I32(value as i32))
} else {
Err(EncoderError::UnsupportedUnsignedType)
}
}

#[inline]
Expand All @@ -98,8 +103,12 @@ impl Serializer for Encoder {
}

#[inline]
fn serialize_u16(self, _value: u16) -> EncoderResult<Bson> {
Err(EncoderError::UnsupportedUnsignedType)
fn serialize_u16(self, value: u16) -> EncoderResult<Bson> {
if cfg!(feature = "u2i") {
Ok(Bson::I32(value as i32))
} else {
Err(EncoderError::UnsupportedUnsignedType)
}
}

#[inline]
Expand All @@ -108,8 +117,12 @@ impl Serializer for Encoder {
}

#[inline]
fn serialize_u32(self, _value: u32) -> EncoderResult<Bson> {
Err(EncoderError::UnsupportedUnsignedType)
fn serialize_u32(self, value: u32) -> EncoderResult<Bson> {
if cfg!(feature = "u2i") {
Ok(Bson::I64(value as i64))
} else {
Err(EncoderError::UnsupportedUnsignedType)
}
}

#[inline]
Expand All @@ -118,8 +131,15 @@ impl Serializer for Encoder {
}

#[inline]
fn serialize_u64(self, _value: u64) -> EncoderResult<Bson> {
Err(EncoderError::UnsupportedUnsignedType)
fn serialize_u64(self, value: u64) -> EncoderResult<Bson> {
if cfg!(feature = "u2i") {
match i64::try_from(value) {
Ok(ivalue) => Ok(Bson::I64(ivalue)),
Err(_) => Err(EncoderError::UnsignedTypesValueExceedsRange(value)),
}
} else {
Err(EncoderError::UnsupportedUnsignedType)
}
}

#[inline]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern crate serde;
extern crate serde_json;
extern crate md5;
extern crate time;
extern crate try_from;

pub use self::bson::{Array, Bson, Document, TimeStamp, UtcDateTime};
pub use self::decoder::{decode_document, decode_document_utf8_lossy, from_bson, Decoder, DecoderError, DecoderResult};
Expand Down
2 changes: 2 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[macro_use(assert_matches)]
extern crate assert_matches;
#[macro_use(bson, doc)]
extern crate bson;
extern crate byteorder;
Expand Down
79 changes: 77 additions & 2 deletions tests/modules/ser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bson::{from_bson, to_bson, Bson};
use bson::{from_bson, to_bson, Bson, EncoderError, EncoderResult};
use bson::oid::ObjectId;
use std::collections::BTreeMap;
use std::{u8, u16, u32, u64};

#[test]
fn floating_point() {
Expand Down Expand Up @@ -53,11 +54,85 @@ fn int32() {
assert_eq!(deser, obj);
}

#[test]
#[cfg_attr(feature = "u2i", ignore)]
fn uint8() {
let obj_min: EncoderResult<Bson> = to_bson(&u8::MIN);
assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType));
}

#[test]
#[cfg(feature = "u2i")]
fn uint8_u2i() {
let obj: Bson = to_bson(&u8::MIN).unwrap();
let deser: u8 = from_bson(obj).unwrap();
assert_eq!(deser, u8::MIN);

let obj_max: Bson = to_bson(&u8::MAX).unwrap();
let deser_max: u8 = from_bson(obj_max).unwrap();
assert_eq!(deser_max, u8::MAX);
}

#[test]
#[cfg_attr(feature = "u2i", ignore)]
fn uint16() {
let obj_min: EncoderResult<Bson> = to_bson(&u16::MIN);
assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType));
}

#[test]
#[cfg(feature = "u2i")]
fn uint16_u2i() {
let obj: Bson = to_bson(&u16::MIN).unwrap();
let deser: u16 = from_bson(obj).unwrap();
assert_eq!(deser, u16::MIN);

let obj_max: Bson = to_bson(&u16::MAX).unwrap();
let deser_max: u16 = from_bson(obj_max).unwrap();
assert_eq!(deser_max, u16::MAX);
}

#[test]
#[cfg_attr(feature = "u2i", ignore)]
fn uint32() {
let obj_min: EncoderResult<Bson> = to_bson(&u32::MIN);
assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType));
}

#[test]
#[cfg(feature = "u2i")]
fn uint32_u2i() {
let obj_min: Bson = to_bson(&u32::MIN).unwrap();
let deser_min: u32 = from_bson(obj_min).unwrap();
assert_eq!(deser_min, u32::MIN);

let obj_max: Bson = to_bson(&u32::MAX).unwrap();
let deser_max: u32 = from_bson(obj_max).unwrap();
assert_eq!(deser_max, u32::MAX);
}

#[test]
#[cfg_attr(feature = "u2i", ignore)]
fn uint64() {
let obj_min: EncoderResult<Bson> = to_bson(&u64::MIN);
assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType));
}

#[test]
#[cfg(feature = "u2i")]
fn uint64_u2i() {
let obj_min: Bson = to_bson(&u64::MIN).unwrap();
let deser_min: u64 = from_bson(obj_min).unwrap();
assert_eq!(deser_min, u64::MIN);

let obj_max: EncoderResult<Bson> = to_bson(&u64::MAX);
assert_matches!(obj_max, Err(EncoderError::UnsignedTypesValueExceedsRange(u64::MAX)));
}

#[test]
fn int64() {
let obj = Bson::I64(101);
let i: i64 = from_bson(obj.clone()).unwrap();

assert_eq!(i, 101);

let deser: Bson = to_bson(&i).unwrap();
Expand Down