Skip to content

Commit 01fea3f

Browse files
authored
RUST-736 Update documentation for 2.0 release (#295)
1 parent 7910f07 commit 01fea3f

File tree

14 files changed

+309
-25
lines changed

14 files changed

+309
-25
lines changed

.evergreen/check-rustdoc.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
5+
. ~/.cargo/env
6+
cargo +nightly rustdoc -p bson --all-features -- --cfg docsrs -D warnings

.evergreen/config.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ functions:
112112
${PREPARE_SHELL}
113113
.evergreen/check-clippy.sh
114114
115+
"check rustdoc":
116+
- command: shell.exec
117+
type: test
118+
params:
119+
shell: bash
120+
working_dir: "src"
121+
script: |
122+
${PREPARE_SHELL}
123+
.evergreen/check-rustdoc.sh
124+
115125
"init test-results":
116126
- command: shell.exec
117127
params:
@@ -148,6 +158,10 @@ tasks:
148158
commands:
149159
- func: "check clippy"
150160

161+
- name: "check-rustdoc"
162+
commands:
163+
- func: "check rustdoc"
164+
151165
axes:
152166
- id: "extra-rust-versions"
153167
values:
@@ -185,3 +199,4 @@ buildvariants:
185199
tasks:
186200
- name: "check-clippy"
187201
- name: "check-rustfmt"
202+
- name: "check-rustdoc"

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ assert_matches = "1.2"
5858
serde_bytes = "0.11"
5959
pretty_assertions = "0.6.1"
6060
chrono = { version = "0.4", features = ["serde"] }
61+
uuid = { version = "0.8.1", features = ["serde", "v4"] }
62+
63+
[package.metadata.docs.rs]
64+
all-features = true
65+
rustdoc-args = ["--cfg", "docsrs"]

README.md

Lines changed: 100 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,28 @@ 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+
| `chrono-0_4` | Enable support for v0.4 of the [`chrono`](docs.rs/chrono/0.4) crate in the public API. | n/a | no |
50+
| `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 |
3251

33-
## Overview of BSON Format
52+
## Overview of the BSON Format
3453

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

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

191286
The MSRV for this crate is currently 1.48.0. This will be rarely be increased, and if it ever is,

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ use_try_shorthand = true
77
wrap_comments = true
88
imports_layout = "HorizontalVertical"
99
imports_granularity = "Crate"
10+
ignore = ["src/lib.rs"]

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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use chrono::{LocalResult, TimeZone, Utc};
3030
/// will serialize to and deserialize from that format's equivalent of the
3131
/// [extended JSON representation](https://docs.mongodb.com/manual/reference/mongodb-extended-json/) of a datetime.
3232
/// To serialize a [`chrono::DateTime`] as a BSON datetime, you can use
33-
/// [`serde_helpers::chrono_datetime_as_bson_datetime`].
33+
/// [`crate::serde_helpers::chrono_datetime_as_bson_datetime`].
3434
///
3535
/// ```rust
3636
/// # #[cfg(feature = "chrono-0_4")]
@@ -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,11 +120,12 @@ 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
}
125127

126-
/// Convert the given [`std::SystemTime`] to a [`DateTime`].
128+
/// Convert the given [`std::time::SystemTime`] to a [`DateTime`].
127129
///
128130
/// If the provided time is too far in the future or too far in the past to be represented
129131
/// by a BSON datetime, either [`DateTime::MAX`] or [`DateTime::MIN`] will be
@@ -149,7 +151,7 @@ impl crate::DateTime {
149151
}
150152
}
151153

152-
/// Convert this [`DateTime`] to a [`std::SystemTime`].
154+
/// Convert this [`DateTime`] to a [`std::time::SystemTime`].
153155
pub fn to_system_time(self) -> SystemTime {
154156
if self.0 >= 0 {
155157
SystemTime::UNIX_EPOCH + Duration::from_millis(self.0 as u64)
@@ -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/de/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub enum Error {
3030
EndOfStream,
3131

3232
/// A general error encountered during deserialization.
33-
/// See: https://docs.serde.rs/serde/de/trait.Error.html
33+
/// See: <https://docs.serde.rs/serde/de/trait.Error.html>
3434
#[non_exhaustive]
3535
DeserializationError {
3636
/// A message describing the error.

src/de/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ where
423423
/// sequences with the Unicode replacement character.
424424
///
425425
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
426-
/// in rare cases can contain invalidly truncated strings (https://jira.mongodb.org/browse/SERVER-24007).
426+
/// in rare cases can contain invalidly truncated strings (<https://jira.mongodb.org/browse/SERVER-24007>).
427427
/// For most use cases, [`crate::from_reader`] can be used instead.
428428
pub fn from_reader_utf8_lossy<R, T>(reader: R) -> Result<T>
429429
where
@@ -447,7 +447,7 @@ where
447447
/// sequences with the Unicode replacement character.
448448
///
449449
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
450-
/// in rare cases can contain invalidly truncated strings (https://jira.mongodb.org/browse/SERVER-24007).
450+
/// in rare cases can contain invalidly truncated strings (<https://jira.mongodb.org/browse/SERVER-24007>).
451451
/// For most use cases, [`crate::from_slice`] can be used instead.
452452
pub fn from_slice_utf8_lossy<'de, T>(bytes: &'de [u8]) -> Result<T>
453453
where

src/document.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl Document {
607607
/// stream.
608608
///
609609
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
610-
/// in rare cases can contain invalidly truncated strings (https://jira.mongodb.org/browse/SERVER-24007).
610+
/// in rare cases can contain invalidly truncated strings (<https://jira.mongodb.org/browse/SERVER-24007>).
611611
/// For most use cases, `Document::from_reader` can be used instead.
612612
pub fn from_reader_utf8_lossy<R: Read>(mut reader: R) -> crate::de::Result<Document> {
613613
Self::decode(&mut reader, true)

src/extjson/de.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum Error {
3535
InvalidObjectId(oid::Error),
3636

3737
/// A general error encountered during deserialization.
38-
/// See: https://docs.serde.rs/serde/de/trait.Error.html
38+
/// See: <https://docs.serde.rs/serde/de/trait.Error.html>
3939
DeserializationError { message: String },
4040
}
4141

0 commit comments

Comments
 (0)