Skip to content

RUST-1129 Make the dependency on chrono optional and provide equivalent time interop #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ axes:
- id: "extra-rust-versions"
values:
- id: "min"
display_name: "1.48 (minimum supported version)"
display_name: "1.53 (minimum supported version)"
variables:
RUST_VERSION: "1.48.0"
RUST_VERSION: "1.53.0"
- id: "nightly"
display_name: "nightly"
variables:
Expand Down
2 changes: 1 addition & 1 deletion .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -o errexit

. ~/.cargo/env
RUST_BACKTRACE=1 cargo test
RUST_BACKTRACE=1 cargo test --features chrono-0_4,uuid-0_8
RUST_BACKTRACE=1 cargo test --all-features

cd serde-tests
RUST_BACKTRACE=1 cargo test
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ exclude = [
[features]
default = []
# if enabled, include API for interfacing with chrono 0.4
chrono-0_4 = []
chrono-0_4 = ["chrono"]
# if enabled, include API for interfacing with uuid 0.8
uuid-0_8 = []
# if enabled, include API for interfacing with time 0.3
time-0_3 = []
# if enabled, include serde_with interop.
# should be used in conjunction with chrono-0_4 or uuid-0_8.
# it's commented out here because Cargo implicitly adds a feature flag for
Expand All @@ -47,7 +49,7 @@ name = "bson"

[dependencies]
ahash = "0.7.2"
chrono = { version = "0.4.15", features = ["std"], default-features = false }
chrono = { version = "0.4.15", features = ["std"], default-features = false, optional = true }
rand = "0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
Expand All @@ -58,6 +60,7 @@ lazy_static = "1.4.0"
uuid = { version = "0.8.1", features = ["serde", "v4"] }
serde_bytes = "0.11.5"
serde_with = { version = "1", optional = true }
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }

[dev-dependencies]
assert_matches = "1.2"
Expand Down
23 changes: 16 additions & 7 deletions src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use std::{
fmt::{self, Debug, Display, Formatter},
};

use chrono::Datelike;
use serde_json::{json, Value};

pub use crate::document::Document;
Expand Down Expand Up @@ -311,6 +310,14 @@ impl From<oid::ObjectId> for Bson {
}
}

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

#[cfg(feature = "chrono-0_4")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Bson {
Expand Down Expand Up @@ -430,9 +437,10 @@ impl Bson {
})
}
Bson::ObjectId(v) => json!({"$oid": v.to_hex()}),
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_chrono().year() <= 99999 => {
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
json!({
"$date": v.to_rfc3339_string(),
// Unwrap safety: timestamps in the guarded range can always be formatted.
"$date": v.try_to_rfc3339_string().unwrap(),
})
}
Bson::DateTime(v) => json!({
Expand Down Expand Up @@ -592,9 +600,10 @@ impl Bson {
Bson::DateTime(v) if rawbson => doc! {
"$date": v.timestamp_millis(),
},
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_chrono().year() <= 9999 => {
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
doc! {
"$date": v.to_rfc3339_string(),
// Unwrap safety: timestamps in the guarded range can always be formatted.
"$date": v.try_to_rfc3339_string().unwrap(),
}
}
Bson::DateTime(v) => doc! {
Expand Down Expand Up @@ -776,8 +785,8 @@ impl Bson {
}

if let Ok(date) = doc.get_str("$date") {
if let Ok(date) = chrono::DateTime::parse_from_rfc3339(date) {
return Bson::DateTime(crate::DateTime::from_chrono(date));
if let Ok(dt) = crate::DateTime::parse_rfc3339_str(date) {
return Bson::DateTime(dt);
}
}
}
Expand Down
Loading