Skip to content

Commit 051467b

Browse files
committed
update docs for 2.0, add From<Uuid> impl for Binary/Bson
1 parent d112672 commit 051467b

File tree

6 files changed

+269
-9
lines changed

6 files changed

+269
-9
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ assert_matches = "1.2"
6161
serde_bytes = "0.11"
6262
pretty_assertions = "0.6.1"
6363
chrono = { version = "0.4", features = ["serde"] }
64+
65+
[package.metadata.docs.rs]
66+
all-features = true
67+
rustdoc-args = ["--cfg", "docsrs"]

README.md

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
# bson-rs
1+
# bson
22

33
[![crates.io](https://img.shields.io/crates/v/bson.svg)](https://crates.io/crates/bson)
4+
[![docs.rs](https://docs.rs/mongodb/badge.svg)](https://docs.rs/bson)
45
[![crates.io](https://img.shields.io/crates/l/bson.svg)](https://crates.io/crates/bson)
56

67
Encoding and decoding support for BSON in Rust
78

89
## Index
10+
- [Installation](#installation)
11+
- [Requirements](#requirements)
12+
- [Importing](#importing)
13+
- [Feature flags](#feature-flags)
14+
- [Useful links](#useful-links)
915
- [Overview of BSON Format](#overview-of-bson-format)
1016
- [Usage](#usage)
1117
- [BSON Values](#bson-values)
1218
- [BSON Documents](#bson-documents)
1319
- [Modeling BSON with strongly typed data structures](#modeling-bson-with-strongly-typed-data-structures)
20+
- [Working with datetimes](#working-with-datetimes)
21+
- [Working with UUIDs](#working-with-uuids)
1422
- [Contributing](#contributing)
1523
- [Running the Tests](#running-the-tests)
1624
- [Continuous Integration](#continuous-integration)
@@ -20,17 +28,29 @@ Encoding and decoding support for BSON in Rust
2028
- [Serde Documentation](https://serde.rs/)
2129

2230
## Installation
23-
This crate works with Cargo and can be found on
24-
[crates.io](https://crates.io/crates/bson) with a `Cargo.toml` like:
31+
### Requirements
32+
- Rust 1.48+
33+
34+
### Importing
35+
This crate is available on [crates.io](https://crates.io/crates/bson). To use it in your application, simply add it to your project's `Cargo.toml`.
2536

2637
```toml
2738
[dependencies]
2839
bson = "2.0.0-beta.3"
2940
```
3041

31-
This crate requires Rust 1.48+.
42+
Note that if you are using `bson` through the `mongodb` crate, you do not need to specify it in your
43+
`Cargo.toml`, since the `mongodb` crate already re-exports it.
44+
45+
#### Feature Flags
46+
47+
| Feature | Description | Extra dependencies | Default |
48+
|:-------------|:-----------------------------------------------------------------------------------------------|:-------------------|:--------|
49+
| `u2i` | Attempt to serialize unsigned integer types found in `Serialize` structs to signed BSON types. | n/a | yes |
50+
| `chrono-0_4` | Enable support for v0.4 of the [`chrono`](docs.rs/chrono/0.4) crate in the public API. | n/a | no |
51+
| `uuid-0_8` | Enable support for v0.8 of the [`uuid`](docs.rs/uuid/0.8) crate in the public API. | `uuid` 0.8 | no |
3252

33-
## Overview of BSON Format
53+
## Overview of the BSON Format
3454

3555
BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents.
3656
Like JSON, BSON supports the embedding of documents and arrays within other documents
@@ -186,6 +206,82 @@ separate the "business logic" that operates over the data from the (de)serializa
186206
translates the data to/from its serialized form. This can lead to more clear and concise code
187207
that is also less error prone.
188208

209+
### Working with datetimes
210+
211+
The BSON format includes a datetime type, which is modeled in this crate by the
212+
[`bson::DateTime`](https://docs.rs/bson/2.0.0-beta.3/bson/struct.DateTime.html) struct, and the
213+
`Serialize` and `Deserialize` implementations for this struct produce and parse BSON datetimes when
214+
serializing to or deserializing from BSON. The popular crate [`chrono`](https://docs.rs/chrono) also
215+
provides a `DateTime` type, but its `Serialize` and `Deserialize` implementations operate on strings
216+
instead, so when using it with BSON, the BSON datetime type is not used. To work around this, the
217+
`chrono-0_4` feature flag can be enabled. This flag exposes a number of convenient conversions
218+
between `bson::DateTime` and `chrono::DateTime`, including the
219+
[`chrono_datetime_as_bson_datetime`](https://docs.rs/bson/2.0.0-beta.3/bson/serde_helpers/chrono_datetime_as_bson_datetime/index.html)
220+
serde helper, which can be used to (de)serialize `chrono::DateTime`s to/from BSON datetimes, and the
221+
`From<chrono::DateTime>` implementation for `Bson`, which allows `chrono::DateTime` values to be
222+
used in the `doc!` and `bson!` macros.
223+
224+
e.g.
225+
``` rust
226+
use serde::{Serialize, Deserialize};
227+
228+
#[derive(Serialize, Deserialize)]
229+
struct Foo {
230+
// serializes as a BSON datetime.
231+
date_time: bson::DateTime,
232+
233+
// serializes as an RFC 3339 / ISO-8601 string.
234+
chrono_datetime: chrono::DateTime<chrono::Utc>,
235+
236+
// serializes as a BSON datetime.
237+
// this requires the "chrono-0_4" feature flag
238+
#[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
239+
chrono_as_bson: chrono::DateTime<chrono::Utc>,
240+
}
241+
242+
// this automatic conversion also requires the "chrono-0_4" feature flag
243+
let query = doc! {
244+
"created_at": chrono::Utc::now(),
245+
};
246+
```
247+
248+
### Working with UUIDs
249+
250+
The BSON format does not contain a dedicated UUID type, though it does have a "binary" type which
251+
has UUID subtypes. Accordingly, this crate provides a
252+
[`Binary`](https://docs.rs/bson/latest/bson/struct.Binary.html) struct which models this binary type
253+
and serializes to and deserializes from it. The popular [`uuid`](https://docs.rs/uuid) crate does provide a UUID type
254+
(`Uuid`), though its `Serialize` and `Deserialize` implementations operate on strings, so when using
255+
it with BSON, the BSON binary type will not be used. To facilitate the conversion between `Uuid`
256+
values and BSON binary values, the `uuid-0_8` feature flag can be enabled. This flag exposes a
257+
number of convenient conversions from `Uuid`, including the
258+
[`uuid_as_bson_binary`](https://docs.rs/bson/2.0.0-beta.3/bson/serde_helpers/uuid_as_binary/index.html)
259+
serde helper, which can be used to (de)serialize `Uuid`s to/from BSON binaries with the UUID
260+
subtype, and the `From<Uuid>` implementation for `Bson`, which allows `Uuid` values to be used in
261+
the `doc!` and `bson!` macros.
262+
263+
e.g.
264+
265+
``` rust
266+
use serde::{Serialize, Deserialize};
267+
268+
#[derive(Serialize, Deserialize)]
269+
struct Foo {
270+
// serializes as a String.
271+
uuid: Uuid,
272+
273+
// serializes as a BSON binary with subtype 4.
274+
// this requires the "uuid-0_8" feature flag
275+
#[serde(with = "bson::serde_helpers::uuid_as_binary")]
276+
uuid_as_bson: uuid::Uuid,
277+
}
278+
279+
// this automatic conversion also requires the "uuid-0_8" feature flag
280+
let query = doc! {
281+
"uuid": uuid::Uuid::new_v4(),
282+
};
283+
```
284+
189285
## Contributing
190286

191287
We encourage and would happily accept contributions in the form of GitHub pull requests. Before opening one, be sure to run the tests locally; check out the [testing section](#running-the-tests) for information on how to do that. Once you open a pull request, your branch will be run against the same testing matrix that we use for our [continuous integration](#continuous-integration) system, so it is usually sufficient to only run the integration tests locally against a standalone. Remember to always run the linter tests before opening a pull request.

src/bson.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,21 @@ impl From<oid::ObjectId> for Bson {
321321
}
322322

323323
#[cfg(feature = "chrono-0_4")]
324+
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
324325
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Bson {
325326
fn from(a: chrono::DateTime<T>) -> Bson {
326327
Bson::DateTime(crate::DateTime::from(a))
327328
}
328329
}
329330

331+
#[cfg(feature = "uuid-0_8")]
332+
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
333+
impl From<uuid::Uuid> for Bson {
334+
fn from(uuid: uuid::Uuid) -> Self {
335+
Bson::Binary(uuid.into())
336+
}
337+
}
338+
330339
impl From<crate::DateTime> for Bson {
331340
fn from(dt: crate::DateTime) -> Self {
332341
Bson::DateTime(dt)
@@ -1045,6 +1054,17 @@ impl Binary {
10451054
}
10461055
}
10471056

1057+
#[cfg(feature = "uuid-0_8")]
1058+
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
1059+
impl From<uuid::Uuid> for Binary {
1060+
fn from(uuid: uuid::Uuid) -> Self {
1061+
Binary {
1062+
subtype: BinarySubtype::Uuid,
1063+
bytes: uuid.as_bytes().to_vec(),
1064+
}
1065+
}
1066+
}
1067+
10481068
/// Represents a DBPointer. (Deprecated)
10491069
#[derive(Debug, Clone, PartialEq)]
10501070
pub struct DbPointer {

src/datetime.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl crate::DateTime {
8181
/// Convert the given `chrono::DateTime` into a `bson::DateTime`, truncating it to millisecond
8282
/// precision.
8383
#[cfg(feature = "chrono-0_4")]
84+
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
8485
pub fn from_chrono<T: chrono::TimeZone>(dt: chrono::DateTime<T>) -> Self {
8586
Self::from_millis(dt.timestamp_millis())
8687
}
@@ -119,6 +120,7 @@ impl crate::DateTime {
119120
/// assert_eq!(chrono_big, chrono::MAX_DATETIME)
120121
/// ```
121122
#[cfg(feature = "chrono-0_4")]
123+
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
122124
pub fn to_chrono(self) -> chrono::DateTime<Utc> {
123125
self.to_chrono_private()
124126
}
@@ -204,13 +206,15 @@ impl From<crate::DateTime> for SystemTime {
204206
}
205207

206208
#[cfg(feature = "chrono-0_4")]
209+
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
207210
impl From<crate::DateTime> for chrono::DateTime<Utc> {
208211
fn from(bson_dt: DateTime) -> Self {
209212
bson_dt.to_chrono()
210213
}
211214
}
212215

213216
#[cfg(feature = "chrono-0_4")]
217+
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
214218
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for crate::DateTime {
215219
fn from(x: chrono::DateTime<T>) -> Self {
216220
Self::from_chrono(x)

src/lib.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@
4141
//!
4242
//! For more information about BSON itself, see [bsonspec.org](http://bsonspec.org).
4343
//!
44+
//! ## Installation
45+
//! ### Requirements
46+
//! - Rust 1.48+
47+
//!
48+
//! ### Importing
49+
//! This crate is available on [crates.io](https://crates.io/crates/bson). To use it in your application,
50+
//! simply add it to your project's `Cargo.toml`.
51+
//!
52+
//! ```toml
53+
//! [dependencies]
54+
//! bson = "2.0.0-beta.3"
55+
//! ```
56+
//!
57+
//! Note that if you are using `bson` through the `mongodb` crate, you do not need to specify it in
58+
//! your `Cargo.toml`, since the `mongodb` crate already re-exports it.
59+
//!
60+
//! #### Feature Flags
61+
//!
62+
//! | Feature | Description
63+
//! | Extra dependencies | Default | |:-------------|:
64+
//! -----------------------------------------------------------------------------------------------|:
65+
//! -------------------|:--------| | `u2i` | Attempt to serialize unsigned integer types
66+
//! found in `Serialize` structs to signed BSON types. | n/a | yes |
67+
//! | `chrono-0_4` | Enable support for v0.4 of the [`chrono`](docs.rs/chrono/0.4) crate in the
68+
//! public API. | n/a | no | | `uuid-0_8` | Enable support for v0.8 of
69+
//! the [`uuid`](docs.rs/uuid/0.8) crate in the public API. | `uuid` 0.8 | no
70+
//! |
71+
//!
4472
//! ## BSON values
4573
//!
4674
//! Many different types can be represented as a BSON value, including 32-bit and 64-bit signed
@@ -181,10 +209,94 @@
181209
//! separate the "business logic" that operates over the data from the (de)serialization logic that
182210
//! translates the data to/from its serialized form. This can lead to more clear and concise code
183211
//! that is also less error prone.
212+
//!
213+
//! ## Working with datetimes
214+
//!
215+
//! The BSON format includes a datetime type, which is modeled in this crate by the
216+
//! [`bson::DateTime`] struct, and the
217+
//! `Serialize` and `Deserialize` implementations for this struct produce and parse BSON datetimes
218+
//! when serializing to or deserializing from BSON. The popular crate [`chrono`](docs.rs/chrono)
219+
//! also provides a `DateTime` type, but its `Serialize` and `Deserialize` implementations operate
220+
//! on strings instead, so when using it with BSON, the BSON datetime type is not used. To work
221+
//! around this, the `chrono-0_4` feature flag can be enabled. This flag exposes a number of
222+
//! convenient conversions between `bson::DateTime` and `chrono::DateTime`, including the
223+
//! [`serde_helpers::chrono_datetime_as_bson_datetime`]
224+
//! serde helper, which can be used to (de)serialize `chrono::DateTime`s to/from BSON datetimes, and
225+
//! the `From<chrono::DateTime>` implementation for [`Bson`], which allows `chrono::DateTime` values
226+
//! to be used in the `doc!` and `bson!` macros.
227+
//!
228+
//! e.g.
229+
//! ``` rust
230+
//! # #[cfg(feature = "chrono-0_4")]
231+
//! # {
232+
//! use serde::{Serialize, Deserialize};
233+
//!
234+
//! #[derive(Serialize, Deserialize)]
235+
//! struct Foo {
236+
//! // serializes as a BSON datetime.
237+
//! date_time: bson::DateTime,
238+
//!
239+
//! // serializes as an RFC 3339 / ISO-8601 string.
240+
//! chrono_datetime: chrono::DateTime<chrono::Utc>,
241+
//!
242+
//! // serializes as a BSON datetime.
243+
//! // this requires the "chrono-0_4" feature flag
244+
//! #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
245+
//! chrono_as_bson: chrono::DateTime<chrono::Utc>,
246+
//! }
247+
//!
248+
//! // this automatic conversion also requires the "chrono-0_4" feature flag
249+
//! let query = doc! {
250+
//! "created_at": chrono::Utc::now(),
251+
//! };
252+
//! # }
253+
//! ```
254+
//!
255+
//! ## Working with UUIDs
256+
//!
257+
//! The BSON format does not contain a dedicated UUID type, though it does have a "binary" type
258+
//! which has UUID subtypes. Accordingly, this crate provides a
259+
//! [`Binary`] struct which models this binary type and
260+
//! serializes to and deserializes from it. The popular `uuid` crate does provide a UUID type
261+
//! (`Uuid`), though its `Serialize` and `Deserialize` implementations operate on strings, so when
262+
//! using it with BSON, the BSON binary type will not be used. To facilitate the conversion between
263+
//! `Uuid` values and BSON binary values, the `uuid-0_8` feature flag can be enabled. This flag
264+
//! exposes a number of convenient conversions from `Uuid`, including the
265+
//! [`serde_helpers::uuid_as_bson_binary`]
266+
//! serde helper, which can be used to (de)serialize `Uuid`s to/from BSON binaries with the UUID
267+
//! subtype, and the `From<Uuid>` implementation for `Bson`, which allows `Uuid` values to be used
268+
//! in the `doc!` and `bson!` macros.
269+
//!
270+
//! e.g.
271+
//!
272+
//! ``` rust
273+
//! # #[cfg(feature = "chrono-0_4")]
274+
//! # {
275+
//! use serde::{Serialize, Deserialize};
276+
//!
277+
//! #[derive(Serialize, Deserialize)]
278+
//! struct Foo {
279+
//! // serializes as a String.
280+
//! uuid: Uuid,
281+
//!
282+
//! // serializes as a BSON binary with subtype 4.
283+
//! // this requires the "uuid-0_8" feature flag
284+
//! #[serde(with = "bson::serde_helpers::uuid_as_binary")]
285+
//! uuid_as_bson: uuid::Uuid,
286+
//! }
287+
//!
288+
//! // this automatic conversion also requires the "uuid-0_8" feature flag
289+
//! let query = doc! {
290+
//! "uuid": uuid::Uuid::new_v4(),
291+
//! };
292+
//! # }
293+
//! ```
184294
185295
#![allow(clippy::cognitive_complexity)]
186296
#![doc(html_root_url = "https://docs.rs/bson/2.0.0-beta.3")]
297+
#![cfg_attr(docsrs, feature(doc_cfg))]
187298

299+
#[doc(inline)]
188300
pub use self::{
189301
bson::{Array, Binary, Bson, DbPointer, Document, JavaScriptCodeWithScope, Regex, Timestamp},
190302
datetime::DateTime,

0 commit comments

Comments
 (0)