Skip to content

Commit 2e4f285

Browse files
RUST-998 / RUST-2244 Documentation improvements (#581)
1 parent 266aa30 commit 2e4f285

File tree

11 files changed

+63
-61
lines changed

11 files changed

+63
-61
lines changed

src/binary.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#! Module containing functionality related to BSON binary values.
1+
//! Contains functionality related to BSON binary values.
2+
23
mod vector;
34

45
use std::fmt::{self, Display};

src/bson.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,23 +395,20 @@ impl From<oid::ObjectId> for Bson {
395395
}
396396

397397
#[cfg(feature = "time-0_3")]
398-
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
399398
impl From<time::OffsetDateTime> for Bson {
400399
fn from(a: time::OffsetDateTime) -> Bson {
401400
Bson::DateTime(crate::DateTime::from(a))
402401
}
403402
}
404403

405404
#[cfg(feature = "chrono-0_4")]
406-
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
407405
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Bson {
408406
fn from(a: chrono::DateTime<T>) -> Bson {
409407
Bson::DateTime(crate::DateTime::from(a))
410408
}
411409
}
412410

413411
#[cfg(feature = "uuid-1")]
414-
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
415412
impl From<uuid::Uuid> for Bson {
416413
fn from(uuid: uuid::Uuid) -> Self {
417414
Bson::Binary(uuid.into())

src/datetime.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ impl crate::DateTime {
211211
/// Convert the given [`chrono::DateTime`] into a [`bson::DateTime`](DateTime), truncating it to
212212
/// millisecond precision.
213213
#[cfg(feature = "chrono-0_4")]
214-
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
215214
pub fn from_chrono<T: chrono::TimeZone>(dt: chrono::DateTime<T>) -> Self {
216215
Self::from_millis(dt.timestamp_millis())
217216
}
@@ -241,7 +240,6 @@ impl crate::DateTime {
241240
/// assert_eq!(chrono_big, chrono::DateTime::<chrono::Utc>::MAX_UTC)
242241
/// ```
243242
#[cfg(feature = "chrono-0_4")]
244-
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
245243
pub fn to_chrono(self) -> chrono::DateTime<Utc> {
246244
match Utc.timestamp_millis_opt(self.0) {
247245
LocalResult::Single(dt) => dt,
@@ -324,7 +322,6 @@ impl crate::DateTime {
324322
/// assert_eq!(time_big, time::PrimitiveDateTime::MIN.assume_utc())
325323
/// ```
326324
#[cfg(feature = "time-0_3")]
327-
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
328325
pub fn to_time_0_3(self) -> time::OffsetDateTime {
329326
self.to_time_private()
330327
}
@@ -453,26 +450,20 @@ impl From<crate::DateTime> for SystemTime {
453450
}
454451

455452
#[cfg(feature = "chrono-0_4")]
456-
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
457453
impl From<crate::DateTime> for chrono::DateTime<Utc> {
458454
fn from(bson_dt: DateTime) -> Self {
459455
bson_dt.to_chrono()
460456
}
461457
}
462458

463459
#[cfg(feature = "chrono-0_4")]
464-
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
465460
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for crate::DateTime {
466461
fn from(x: chrono::DateTime<T>) -> Self {
467462
Self::from_chrono(x)
468463
}
469464
}
470465

471466
#[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
472-
#[cfg_attr(
473-
docsrs,
474-
doc(cfg(all(feature = "chrono-0_4", feature = "serde_with-3")))
475-
)]
476467
impl<'de> serde_with::DeserializeAs<'de, chrono::DateTime<Utc>> for crate::DateTime {
477468
fn deserialize_as<D>(deserializer: D) -> std::result::Result<chrono::DateTime<Utc>, D::Error>
478469
where
@@ -484,7 +475,6 @@ impl<'de> serde_with::DeserializeAs<'de, chrono::DateTime<Utc>> for crate::DateT
484475
}
485476

486477
#[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
487-
#[cfg_attr(docsrs, doc(cfg(all(feature = "chrono-0_4", feature = "chrono-0_4"))))]
488478
impl serde_with::SerializeAs<chrono::DateTime<Utc>> for crate::DateTime {
489479
fn serialize_as<S>(
490480
source: &chrono::DateTime<Utc>,
@@ -499,23 +489,20 @@ impl serde_with::SerializeAs<chrono::DateTime<Utc>> for crate::DateTime {
499489
}
500490

501491
#[cfg(feature = "time-0_3")]
502-
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
503492
impl From<crate::DateTime> for time::OffsetDateTime {
504493
fn from(bson_dt: DateTime) -> Self {
505494
bson_dt.to_time_0_3()
506495
}
507496
}
508497

509498
#[cfg(feature = "time-0_3")]
510-
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
511499
impl From<time::OffsetDateTime> for crate::DateTime {
512500
fn from(x: time::OffsetDateTime) -> Self {
513501
Self::from_time_0_3(x)
514502
}
515503
}
516504

517505
#[cfg(all(feature = "time-0_3", feature = "serde_with-3"))]
518-
#[cfg_attr(docsrs, doc(cfg(all(feature = "time-0_3", feature = "serde_with-3"))))]
519506
impl<'de> serde_with::DeserializeAs<'de, time::OffsetDateTime> for crate::DateTime {
520507
fn deserialize_as<D>(deserializer: D) -> std::result::Result<time::OffsetDateTime, D::Error>
521508
where
@@ -527,7 +514,6 @@ impl<'de> serde_with::DeserializeAs<'de, time::OffsetDateTime> for crate::DateTi
527514
}
528515

529516
#[cfg(all(feature = "time-0_3", feature = "serde_with-3"))]
530-
#[cfg_attr(docsrs, doc(cfg(all(feature = "time-0_3", feature = "chrono-0_4"))))]
531517
impl serde_with::SerializeAs<time::OffsetDateTime> for crate::DateTime {
532518
fn serialize_as<S>(
533519
source: &time::OffsetDateTime,

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub use oid::ObjectIdErrorKind;
1212
pub use uuid::UuidErrorKind;
1313
pub use value_access::ValueAccessErrorKind;
1414

15+
/// The result type for all methods that can return an error in the `bson` crate.
1516
pub type Result<T> = std::result::Result<T, Error>;
1617

1718
/// An error that can occur in the `bson` crate.

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,9 @@
373373
//! is, it will only happen in a minor or major version release.
374374
375375
#![allow(clippy::cognitive_complexity, clippy::derive_partial_eq_without_eq)]
376-
#![doc(html_root_url = "https://docs.rs/bson/2.6.0")]
377-
#![cfg_attr(docsrs, feature(doc_cfg))]
376+
#![doc(html_root_url = "https://docs.rs/bson/3.0.0")]
377+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
378+
#![warn(missing_docs)]
378379

379380
#[doc(inline)]
380381
pub use self::{

src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ macro_rules! serde_conv_doc {
449449
// https://github.com/jonasbb/serde_with/pull/320
450450
// https://github.com/jonasbb/serde_with/pull/729
451451
#[allow(clippy::all)]
452+
#[allow(missing_docs)]
452453
const _:() = {
453454
impl $m {
454455
$vis fn serialize<S>(x: &$t, serializer: S) -> Result<S::Ok, S::Error>

src/raw/document_buf.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ impl RawDocumentBuf {
8585
Ok(Self { data })
8686
}
8787

88+
/// Constructs a new [`RawDocumentBuf`], validating _only_ the
89+
/// following invariants:
90+
/// * `data` is at least five bytes long (the minimum for a valid BSON document)
91+
/// * the initial four bytes of `data` accurately represent the length of the bytes as
92+
/// required by the BSON spec.
93+
/// * the last byte of `data` is a 0
94+
///
95+
/// Note that the internal structure of the bytes representing the
96+
/// BSON elements is _not_ validated at all by this method. If the
97+
/// bytes do not conform to the BSON spec, then method calls on
98+
/// the RawDocument will return Errors where appropriate.
99+
///
100+
/// ```
101+
/// # use bson::raw::RawDocumentBuf;
102+
/// let doc = RawDocumentBuf::decode_from_reader(b"\x05\0\0\0\0".as_slice())?;
103+
/// # Ok::<(), bson::error::Error>(())
104+
/// ```
88105
pub fn decode_from_reader<R: std::io::Read>(reader: R) -> Result<Self> {
89106
let buf = crate::raw::reader_to_vec(reader)?;
90107
Self::decode_from_bytes(buf)
@@ -333,6 +350,7 @@ impl Borrow<RawDocument> for RawDocumentBuf {
333350
/// type Target = BindValue<Self>;
334351
/// }
335352
/// ```
353+
#[allow(missing_docs)]
336354
pub trait BindRawBsonRef: Sized {
337355
type Target: BindHelper<Target = Self>;
338356

src/raw/iter.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ impl<'a> RawIter<'a> {
109109
}
110110
}
111111

112+
/// A view into a value contained in a [`RawDocument`] or [`RawDocumentBuf`](crate::RawDocumentBuf).
113+
/// The underlying bytes of the element are not parsed or validated; call [`RawElement::value`] or
114+
/// one of the `TryFrom` implementations to convert the element into a BSON value.
112115
#[derive(Clone)]
113116
pub struct RawElement<'a> {
114117
key: &'a CStr,
@@ -118,27 +121,27 @@ pub struct RawElement<'a> {
118121
size: usize,
119122
}
120123

121-
impl<'a> TryInto<RawBsonRef<'a>> for RawElement<'a> {
124+
impl<'a> TryFrom<RawElement<'a>> for RawBsonRef<'a> {
122125
type Error = Error;
123126

124-
fn try_into(self) -> Result<RawBsonRef<'a>> {
125-
self.value()
127+
fn try_from(element: RawElement<'a>) -> Result<Self> {
128+
element.value()
126129
}
127130
}
128131

129-
impl TryInto<RawBson> for RawElement<'_> {
132+
impl TryFrom<RawElement<'_>> for RawBson {
130133
type Error = Error;
131134

132-
fn try_into(self) -> Result<RawBson> {
133-
Ok(self.value()?.to_raw_bson())
135+
fn try_from(element: RawElement<'_>) -> Result<Self> {
136+
Ok(element.value()?.to_raw_bson())
134137
}
135138
}
136139

137-
impl TryInto<Bson> for RawElement<'_> {
140+
impl TryFrom<RawElement<'_>> for Bson {
138141
type Error = Error;
139142

140-
fn try_into(self) -> Result<Bson> {
141-
self.value()?.to_raw_bson().try_into()
143+
fn try_from(element: RawElement<'_>) -> Result<Self> {
144+
element.value()?.to_raw_bson().try_into()
142145
}
143146
}
144147

@@ -157,18 +160,23 @@ impl<'a> RawElement<'a> {
157160
})
158161
}
159162

163+
/// The size of the element.
160164
pub fn size(&self) -> usize {
161165
self.size
162166
}
163167

168+
/// The document key the element corresponds to.
164169
pub fn key(&self) -> &'a CStr {
165170
self.key
166171
}
167172

173+
/// The type of the element.
168174
pub fn element_type(&self) -> ElementType {
169175
self.kind
170176
}
171177

178+
/// Parses this element into a [`RawBsonRef`] and returns an error if the underlying bytes are
179+
/// invalid.
172180
pub fn value(&self) -> Result<RawBsonRef<'a>> {
173181
Ok(match self.kind {
174182
ElementType::Null => RawBsonRef::Null,
@@ -270,6 +278,8 @@ impl<'a> RawElement<'a> {
270278
})
271279
}
272280

281+
/// Parses this element into [`RawBson`], replacing any invalid UTF-8 strings with the Unicode
282+
/// replacement character. Returns an error if the underlying bytes are invalid.
273283
pub fn value_utf8_lossy(&self) -> Result<RawBson> {
274284
match self.value_utf8_lossy_inner()? {
275285
Some(v) => Ok(v.into()),

src/serde_helpers.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::{
1313
/// - [`object_id::AsHexString`] — converts an [`crate::oid::ObjectId`] to and from a hex string.
1414
/// - [`object_id::FromHexString`] — converts a hex string to and from an [`crate::oid::ObjectId`].
1515
#[cfg(feature = "serde_with-3")]
16-
#[cfg_attr(docsrs, doc(cfg(feature = "serde_with-3")))]
1716
pub mod object_id {
1817
use crate::{macros::serde_conv_doc, oid::ObjectId};
1918
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -85,7 +84,6 @@ pub mod object_id {
8584
/// - [`datetime::FromTime03OffsetDateTime`] — converts a [`time::OffsetDateTime`] to and from a
8685
/// [`crate::DateTime`].
8786
#[cfg(feature = "serde_with-3")]
88-
#[cfg_attr(docsrs, doc(cfg(feature = "serde_with-3")))]
8987
pub mod datetime {
9088
use crate::{macros::serde_conv_doc, DateTime};
9189
use chrono::Utc;
@@ -177,7 +175,6 @@ pub mod datetime {
177175

178176
#[cfg(feature = "chrono-0_4")]
179177
serde_conv_doc!(
180-
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
181178
/// Converts a [`chrono::DateTime`] to and from a [`DateTime`].
182179
/// ```rust
183180
/// # #[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
@@ -205,7 +202,6 @@ pub mod datetime {
205202

206203
#[cfg(feature = "time-0_3")]
207204
serde_conv_doc!(
208-
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
209205
/// Converts a [`time::OffsetDateTime`] to and from a [`DateTime`].
210206
/// ```rust
211207
/// # #[cfg(all(feature = "time-0_3", feature = "serde_with-3"))]
@@ -239,7 +235,6 @@ pub mod datetime {
239235
/// - [`timestamp::AsU32`] — converts a [`crate::Timestamp`] to and from a `u32`.
240236
/// - [`timestamp::FromU32`] — converts a `u32` to and from a [`crate::Timestamp`].
241237
#[cfg(feature = "serde_with-3")]
242-
#[cfg_attr(docsrs, doc(cfg(feature = "serde_with-3")))]
243238
pub mod timestamp {
244239
use crate::{macros::serde_conv_doc, Timestamp};
245240
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -319,7 +314,6 @@ pub mod timestamp {
319314
/// - [`u32::AsI32`] — converts a `u32` to and from an `i32`.
320315
/// - [`u32::AsI64`] — converts a `u32` to and from an `i64`.
321316
#[cfg(feature = "serde_with-3")]
322-
#[cfg_attr(docsrs, doc(cfg(feature = "serde_with-3")))]
323317
pub mod u32 {
324318
use crate::macros::serde_conv_doc;
325319
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -421,7 +415,6 @@ pub mod u32 {
421415
/// - [`u64::AsI32`] — converts a `u64` to and from an `i32`.
422416
/// - [`u64::AsI64`] — converts a `u64` to and from an `i64`.
423417
#[cfg(feature = "serde_with-3")]
424-
#[cfg_attr(docsrs, doc(cfg(feature = "serde_with-3")))]
425418
pub mod u64 {
426419
use crate::macros::serde_conv_doc;
427420
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -533,7 +526,6 @@ pub mod u64 {
533526
/// - [`uuid_1::AsPythonLegacyBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`] in the
534527
/// legacy Python driver UUID format.
535528
#[cfg(all(feature = "serde_with-3", feature = "uuid-1"))]
536-
#[cfg_attr(docsrs, doc(cfg(all(feature = "serde_with-3", feature = "uuid-1"))))]
537529
pub mod uuid_1 {
538530
use crate::macros::serde_conv_doc;
539531
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -681,13 +673,11 @@ macro_rules! as_binary_mod {
681673
use $uu;
682674

683675
/// Serializes a Uuid as a Binary.
684-
#[cfg_attr(docsrs, doc($feat))]
685676
pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
686677
crate::uuid::Uuid::from(*val).serialize(serializer)
687678
}
688679

689680
/// Deserializes a Uuid from a Binary.
690-
#[cfg_attr(docsrs, doc($feat))]
691681
pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
692682
where
693683
D: Deserializer<'de>,
@@ -707,14 +697,12 @@ macro_rules! as_legacy_binary_mod {
707697
use $uu;
708698

709699
/// Serializes a Uuid as a Binary in the legacy UUID format.
710-
#[cfg_attr(docsrs, doc($feat))]
711700
pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
712701
let binary = Binary::from_uuid_with_representation(crate::uuid::Uuid::from(*val), $rep);
713702
binary.serialize(serializer)
714703
}
715704

716705
/// Deserializes a Uuid from a Binary in the legacy UUID format.
717-
#[cfg_attr(docsrs, doc($feat))]
718706
pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
719707
where
720708
D: Deserializer<'de>,

0 commit comments

Comments
 (0)