Skip to content

Commit e13a588

Browse files
authored
RUST-1985 Update bson toplevel rustdoc (#589)
1 parent d7b516d commit e13a588

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

src/lib.rs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@
6565
//! | `jiff-0_2` | Enable support for v0.2 of the [`jiff`](https://docs.rs/jiff/0.2) crate in the public API. | no |
6666
//! | `uuid-1` | Enable support for v1.x of the [`uuid`](https://docs.rs/uuid/1.x) crate in the public API. | no |
6767
//! | `time-0_3` | Enable support for v0.3 of the [`time`](https://docs.rs/time/0.3) crate in the public API. | no |
68+
//! | `serde` | Enable integration with the [`serde`](https://docs.rs/serde/) serialization/deserialization framework. | no |
6869
//! | `serde_with-3` | Enable [`serde_with`](https://docs.rs/serde_with/3.x) 3.x integrations for [`DateTime`] and [`Uuid`]. | no |
6970
//! | `serde_path_to_error` | Enable support for error paths via integration with [`serde_path_to_error`](https://docs.rs/serde_path_to_err/latest). This is an unstable feature and any breaking changes to `serde_path_to_error` may affect usage of it via this feature. | no |
70-
//! | `compat-3-0-0` | Required for future compatibility if default features are disabled. | no |
71+
//! | `compat-3-0-0` | Required for future compatibility if default features are disabled. | yes |
7172
//! | `large_dates` | Increase the supported year range for some `bson::DateTime` utilities from +/-9,999 (inclusive) to +/-999,999 (inclusive). Note that enabling this feature can impact performance and introduce parsing ambiguities. | no |
7273
//! | `serde_json-1` | Enable support for v1.x of the [`serde_json`](https://docs.rs/serde_json/1.x) crate in the public API. | no |
7374
//!
@@ -170,12 +171,13 @@
170171
//! let error = doc.get_i64("i32"); // Err(...)
171172
//! ```
172173
//!
173-
//! ## Modeling BSON with strongly typed data structures
174+
//! ## Integration with `serde`
174175
//!
175176
//! While it is possible to work with documents and BSON values directly, it will often introduce a
176177
//! lot of boilerplate for verifying the necessary keys are present and their values are the correct
177-
//! types. [`serde`](https://serde.rs/) provides a powerful way of mapping BSON data into Rust data structures largely
178-
//! automatically, removing the need for all that boilerplate.
178+
//! types. Enabling the `serde` feature provides integration with the [`serde`](https://serde.rs/)
179+
//! crate that maps BSON data into Rust data structs largely automatically, removing the need for
180+
//! all that boilerplate.
179181
//!
180182
//! e.g.:
181183
//! ```rust
@@ -224,6 +226,64 @@
224226
//! failing field. This feature does incur a small CPU and memory overhead during (de)serialization
225227
//! and should be enabled with care in performance-sensitive environments.
226228
//!
229+
//! ### Embedding BSON Value Types
230+
//!
231+
//! The `serde` feature also enables implementations of [`Serialize`](serde::Serialize) and
232+
//! [`Deserialize`](serde::Deserialize) for the Rust types provided by this crate that represent
233+
//! BSON values, allowing them to be embedded in domain-specific structs as appropriate:
234+
//! ```rust
235+
//! use serde::{Deserialize, Serialize};
236+
//! use bson::{bson, Bson, oid::ObjectId};
237+
//!
238+
//! #[derive(Serialize, Deserialize)]
239+
//! struct Person {
240+
//! id: ObjectId,
241+
//! name: String,
242+
//! age: i32,
243+
//! phones: Vec<String>,
244+
//! }
245+
//!
246+
//! let bson_data: Bson = bson!({
247+
//! "id": ObjectId::new(),
248+
//! "name": "John Doe",
249+
//! "age": 43,
250+
//! "phones": [
251+
//! "+44 1234567",
252+
//! "+44 2345678"
253+
//! ]
254+
//! });
255+
//!
256+
//! let person: Person = bson::deserialize_from_bson(bson_data).unwrap();
257+
//! ```
258+
//!
259+
//! ### Encoding vs. Serialization
260+
//!
261+
//! With the `serde` feature enabled, a BSON document can be converted to its wire-format byte
262+
//! representation in multiple ways:
263+
//! ```rust
264+
//! # fn wrapper() -> bson::error::Result<()> {
265+
//! use bson::{doc, serialize_to_vec};
266+
//! let my_document = doc! { "hello": "bson" };
267+
//! let encoded = my_document.to_vec()?;
268+
//! let serialized = serialize_to_vec(&my_document)?;
269+
//! # Ok(())
270+
//! # }
271+
//! # wrapper().unwrap();
272+
//! ```
273+
//!
274+
//! We recommend that, where possible, documents be converted to byte form using the encoding
275+
//! methods ([`Document::to_vec`]/[`Document::to_writer`]); this is more efficient as it avoids
276+
//! the intermediate `serde` data model representation. This also applies to decoding; prefer
277+
//! [`Document::from_reader`] over [`deserialize_from_reader`] / [`deserialize_from_slice`].
278+
//!
279+
//! ### Serializer Compatibility
280+
//!
281+
//! The implementations of [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize)
282+
//! for BSON value types are tested with the `serde` \[de\]serializers provided by this crate and by
283+
//! the `serde_json` crate. Compatibility with formats provided by other crates is not guaranteed
284+
//! and the data produced by serializing BSON values to other formats may change when this crate is
285+
//! updated.
286+
//!
227287
//! ## Working with Extended JSON
228288
//!
229289
//! MongoDB Extended JSON (extJSON) is a format of JSON that allows for the encoding

0 commit comments

Comments
 (0)