Skip to content

Commit e13c774

Browse files
authored
minor: hide work-in-progress raw BSON API (#324)
1 parent 961b5c9 commit e13c774

File tree

6 files changed

+8
-717
lines changed

6 files changed

+8
-717
lines changed

serde-tests/test.rs

Lines changed: 2 additions & 287 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ use bson::{
2929
DeserializerOptions,
3030
Document,
3131
JavaScriptCodeWithScope,
32-
RawArray,
33-
RawBinary,
34-
RawBson,
35-
RawDbPointer,
36-
RawDocument,
37-
RawDocumentBuf,
38-
RawJavaScriptCodeWithScope,
39-
RawRegex,
4032
Regex,
4133
SerializerOptions,
4234
Timestamp,
@@ -145,18 +137,6 @@ where
145137
);
146138
}
147139

148-
/// Verifies the following:
149-
/// - Deserializing a `T` from the provided bytes does not error
150-
/// - Serializing the `T` back to bytes produces the input.
151-
fn run_raw_round_trip_test<'de, T>(bytes: &'de [u8], description: &str)
152-
where
153-
T: Deserialize<'de> + Serialize + std::fmt::Debug,
154-
{
155-
let t: T = bson::from_slice(bytes).expect(description);
156-
let vec = bson::to_vec(&t).expect(description);
157-
assert_eq!(vec.as_slice(), bytes);
158-
}
159-
160140
#[test]
161141
fn smoke() {
162142
#[derive(Serialize, Deserialize, PartialEq, Debug)]
@@ -727,154 +707,6 @@ fn empty_array() {
727707
run_deserialize_test(&v, &doc, "empty_array");
728708
}
729709

730-
#[test]
731-
fn raw_doc_buf() {
732-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
733-
struct Foo {
734-
d: RawDocumentBuf,
735-
}
736-
737-
let bytes = bson::to_vec(&doc! {
738-
"d": {
739-
"a": 12,
740-
"b": 5.5,
741-
"c": [1, true, "ok"],
742-
"d": { "a": "b" },
743-
"e": ObjectId::new(),
744-
}
745-
})
746-
.expect("raw_doc_buf");
747-
748-
run_raw_round_trip_test::<Foo>(bytes.as_slice(), "raw_doc_buf");
749-
}
750-
751-
#[test]
752-
fn raw_doc() {
753-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
754-
struct Foo<'a> {
755-
#[serde(borrow)]
756-
d: &'a RawDocument,
757-
}
758-
759-
let bytes = bson::to_vec(&doc! {
760-
"d": {
761-
"a": 12,
762-
"b": 5.5,
763-
"c": [1, true, "ok"],
764-
"d": { "a": "b" },
765-
"e": ObjectId::new(),
766-
}
767-
})
768-
.expect("raw doc");
769-
770-
run_raw_round_trip_test::<Foo>(bytes.as_slice(), "raw_doc");
771-
}
772-
773-
#[test]
774-
fn raw_array() {
775-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
776-
struct Foo<'a> {
777-
#[serde(borrow)]
778-
d: &'a RawArray,
779-
}
780-
781-
let bytes = bson::to_vec(&doc! {
782-
"d": [1, true, { "ok": 1 }, [ "sub", "array" ], Uuid::new()]
783-
})
784-
.expect("raw_array");
785-
786-
run_raw_round_trip_test::<Foo>(bytes.as_slice(), "raw_array");
787-
}
788-
789-
#[test]
790-
fn raw_binary() {
791-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
792-
struct Foo<'a> {
793-
#[serde(borrow)]
794-
generic: RawBinary<'a>,
795-
796-
#[serde(borrow)]
797-
old: RawBinary<'a>,
798-
799-
#[serde(borrow)]
800-
uuid: RawBinary<'a>,
801-
802-
#[serde(borrow)]
803-
other: RawBinary<'a>,
804-
}
805-
806-
let bytes = bson::to_vec(&doc! {
807-
"generic": Binary {
808-
bytes: vec![1, 2, 3, 4, 5],
809-
subtype: BinarySubtype::Generic,
810-
},
811-
"old": Binary {
812-
bytes: vec![1, 2, 3],
813-
subtype: BinarySubtype::BinaryOld,
814-
},
815-
"uuid": Uuid::new(),
816-
"other": Binary {
817-
bytes: vec![1u8; 100],
818-
subtype: BinarySubtype::UserDefined(100),
819-
}
820-
})
821-
.expect("raw_binary");
822-
823-
run_raw_round_trip_test::<Foo>(bytes.as_slice(), "raw_binary");
824-
}
825-
826-
#[test]
827-
fn raw_regex() {
828-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
829-
struct Foo<'a> {
830-
#[serde(borrow)]
831-
r: RawRegex<'a>,
832-
}
833-
834-
let bytes = bson::to_vec(&doc! {
835-
"r": Regex {
836-
pattern: "a[b-c]d".to_string(),
837-
options: "ab".to_string(),
838-
},
839-
})
840-
.expect("raw_regex");
841-
842-
run_raw_round_trip_test::<Foo>(bytes.as_slice(), "raw_regex");
843-
}
844-
845-
#[test]
846-
fn raw_code_w_scope() {
847-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
848-
struct Foo<'a> {
849-
#[serde(borrow)]
850-
r: RawJavaScriptCodeWithScope<'a>,
851-
}
852-
853-
let bytes = bson::to_vec(&doc! {
854-
"r": JavaScriptCodeWithScope {
855-
code: "console.log(x)".to_string(),
856-
scope: doc! { "x": 1 },
857-
},
858-
})
859-
.expect("raw_code_w_scope");
860-
861-
run_raw_round_trip_test::<Foo>(bytes.as_slice(), "raw_code_w_scope");
862-
}
863-
864-
#[test]
865-
fn raw_db_pointer() {
866-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
867-
struct Foo<'a> {
868-
#[serde(borrow)]
869-
a: RawDbPointer<'a>,
870-
}
871-
872-
// From the "DBpointer" bson corpus test
873-
let bytes = hex::decode("1A0000000C610002000000620056E1FC72E0C917E9C471416100").unwrap();
874-
875-
run_raw_round_trip_test::<Foo>(bytes.as_slice(), "raw_db_pointer");
876-
}
877-
878710
#[derive(Debug, Deserialize, Serialize, PartialEq)]
879711
struct SubDoc {
880712
a: i32,
@@ -1077,64 +909,6 @@ fn all_types_rmp() {
1077909
assert_eq!(back, v);
1078910
}
1079911

1080-
#[test]
1081-
fn all_raw_types_rmp() {
1082-
#[derive(Debug, Serialize, Deserialize, PartialEq)]
1083-
struct AllRawTypes<'a> {
1084-
#[serde(borrow)]
1085-
bson: RawBson<'a>,
1086-
#[serde(borrow)]
1087-
document: &'a RawDocument,
1088-
#[serde(borrow)]
1089-
array: &'a RawArray,
1090-
buf: RawDocumentBuf,
1091-
#[serde(borrow)]
1092-
binary: RawBinary<'a>,
1093-
#[serde(borrow)]
1094-
code_w_scope: RawJavaScriptCodeWithScope<'a>,
1095-
#[serde(borrow)]
1096-
regex: RawRegex<'a>,
1097-
}
1098-
1099-
let doc_bytes = bson::to_vec(&doc! {
1100-
"bson": "some string",
1101-
"array": [1, 2, 3],
1102-
"binary": Binary { bytes: vec![1, 2, 3], subtype: BinarySubtype::Generic },
1103-
"binary_old": Binary { bytes: vec![1, 2, 3], subtype: BinarySubtype::BinaryOld },
1104-
"code_w_scope": JavaScriptCodeWithScope {
1105-
code: "ok".to_string(),
1106-
scope: doc! { "x": 1 },
1107-
},
1108-
"regex": Regex {
1109-
pattern: "pattern".to_string(),
1110-
options: "opt".to_string()
1111-
}
1112-
})
1113-
.unwrap();
1114-
let doc_buf = RawDocumentBuf::new(doc_bytes).unwrap();
1115-
let document = &doc_buf;
1116-
let array = document.get_array("array").unwrap();
1117-
1118-
let v = AllRawTypes {
1119-
bson: document.get("bson").unwrap().unwrap(),
1120-
array,
1121-
document,
1122-
buf: doc_buf.clone(),
1123-
binary: document.get_binary("binary").unwrap(),
1124-
code_w_scope: document
1125-
.get("code_w_scope")
1126-
.unwrap()
1127-
.unwrap()
1128-
.as_javascript_with_scope()
1129-
.unwrap(),
1130-
regex: document.get_regex("regex").unwrap(),
1131-
};
1132-
let serialized = rmp_serde::to_vec_named(&v).unwrap();
1133-
let back: AllRawTypes = rmp_serde::from_slice(&serialized).unwrap();
1134-
1135-
assert_eq!(back, v);
1136-
}
1137-
1138912
#[test]
1139913
fn borrowed() {
1140914
#[derive(Debug, Deserialize, PartialEq)]
@@ -1280,71 +1054,18 @@ fn serde_with_uuid() {
12801054
run_test(&f, &expected, "serde_with - uuid");
12811055
}
12821056

1283-
#[test]
1284-
fn hint_cleared() {
1285-
#[derive(Debug, Serialize, Deserialize)]
1286-
struct Foo<'a> {
1287-
#[serde(borrow)]
1288-
doc: &'a RawDocument,
1289-
#[serde(borrow)]
1290-
binary: RawBinary<'a>,
1291-
}
1292-
1293-
let binary_value = Binary {
1294-
bytes: vec![1, 2, 3, 4],
1295-
subtype: BinarySubtype::Generic,
1296-
};
1297-
1298-
let doc_value = doc! {
1299-
"binary": binary_value.clone()
1300-
};
1301-
1302-
let bytes = bson::to_vec(&doc_value).unwrap();
1303-
1304-
let doc = RawDocument::new(&bytes).unwrap();
1305-
let binary = doc.get_binary("binary").unwrap();
1306-
1307-
let f = Foo { doc, binary };
1308-
1309-
let serialized_bytes = bson::to_vec(&f).unwrap();
1310-
let round_doc: Document = bson::from_slice(&serialized_bytes).unwrap();
1311-
1312-
assert_eq!(round_doc, doc! { "doc": doc_value, "binary": binary_value });
1313-
}
1314-
13151057
#[test]
13161058
fn non_human_readable() {
1317-
let bytes = vec![1, 2, 3, 4];
1318-
let binary = RawBinary {
1319-
bytes: &bytes,
1320-
subtype: BinarySubtype::BinaryOld,
1321-
};
1322-
1323-
let doc_bytes = bson::to_vec(&doc! { "a": "b", "array": [1, 2, 3] }).unwrap();
1324-
let doc = RawDocument::new(doc_bytes.as_slice()).unwrap();
1325-
let arr = doc.get_array("array").unwrap();
13261059
let oid = ObjectId::new();
13271060
let uuid = Uuid::new();
13281061

13291062
#[derive(Debug, Deserialize, Serialize)]
1330-
struct Foo<'a> {
1331-
#[serde(borrow)]
1332-
binary: RawBinary<'a>,
1333-
#[serde(borrow)]
1334-
doc: &'a RawDocument,
1335-
#[serde(borrow)]
1336-
arr: &'a RawArray,
1063+
struct Foo {
13371064
oid: ObjectId,
13381065
uuid: Uuid,
13391066
}
13401067

1341-
let val = Foo {
1342-
binary,
1343-
doc,
1344-
arr,
1345-
oid,
1346-
uuid,
1347-
};
1068+
let val = Foo { oid, uuid };
13481069

13491070
let human_readable = bson::to_bson(&val).unwrap();
13501071
let non_human_readable = bson::to_bson_with_options(
@@ -1354,12 +1075,6 @@ fn non_human_readable() {
13541075
.unwrap();
13551076

13561077
let expected = bson!({
1357-
"binary": Binary { bytes: bytes.clone(), subtype: BinarySubtype::BinaryOld },
1358-
"doc": {
1359-
"a": "b",
1360-
"array": [1, 2, 3],
1361-
},
1362-
"arr": [1, 2, 3],
13631078
"oid": oid,
13641079
"uuid": uuid
13651080
});

src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,21 +272,20 @@ pub use self::{
272272
bson::{Array, Binary, Bson, DbPointer, Document, JavaScriptCodeWithScope, Regex, Timestamp},
273273
datetime::DateTime,
274274
de::{
275-
from_bson, from_bson_with_options, from_document, from_document_with_options, from_reader, from_reader_utf8_lossy,
276-
from_slice, from_slice_utf8_lossy, Deserializer, DeserializerOptions,
275+
from_bson, from_bson_with_options, from_document, from_document_with_options, from_reader,
276+
from_reader_utf8_lossy, from_slice, from_slice_utf8_lossy, Deserializer,
277+
DeserializerOptions,
277278
},
278279
decimal128::Decimal128,
279-
raw::{
280-
RawArray, RawBinary, RawBson, RawDbPointer, RawDocument, RawDocumentBuf, RawJavaScriptCodeWithScope,
281-
RawRegex,
282-
},
283280
ser::{
284281
to_bson, to_bson_with_options, to_document, to_document_with_options, to_vec, Serializer,
285282
SerializerOptions,
286283
},
287284
uuid::{Uuid, UuidRepresentation},
288285
};
289286

287+
pub(crate) use self::raw::RawDocument;
288+
290289
#[macro_use]
291290
mod macros;
292291
mod bson;
@@ -296,7 +295,7 @@ pub mod decimal128;
296295
pub mod document;
297296
pub mod extjson;
298297
pub mod oid;
299-
pub mod raw;
298+
pub(crate) mod raw;
300299
pub mod ser;
301300
pub mod serde_helpers;
302301
pub mod spec;

0 commit comments

Comments
 (0)