diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 9dd9cc2f96..c211c859b2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -46,8 +46,8 @@ jobs: run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --all-features - name: Cargo check with secrecy-08 feature run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "secrecy-08" - - name: Cargo check with chrono feature - run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "chrono" + - name: Cargo check with chrono-04 feature + run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "chrono-04" - name: Cargo check with time-03 feature run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "time-03" - name: Cargo check with num-bigint-03 feature diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 0ddc1ec99e..bd44695aae 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -13,7 +13,7 @@ rustyline-derive = "0.6" scylla = { path = "../scylla", features = [ "ssl", "cloud", - "chrono", + "chrono-04", "time-03", "num-bigint-03", "num-bigint-04", diff --git a/scylla-cql/Cargo.toml b/scylla-cql/Cargo.toml index 2c17478cf5..2dae50d09f 100644 --- a/scylla-cql/Cargo.toml +++ b/scylla-cql/Cargo.toml @@ -22,7 +22,7 @@ thiserror = "1.0" num-bigint-03 = { package = "num-bigint", version = "0.3", optional = true } num-bigint-04 = { package = "num-bigint", version = "0.4", optional = true } bigdecimal-04 = { package = "bigdecimal", version = "0.4", optional = true } -chrono = { version = "0.4.32", default-features = false, optional = true } +chrono-04 = { package = "chrono", version = "0.4.32", default-features = false, optional = true } lz4_flex = { version = "0.11.1" } async-trait = "0.1.57" serde = { version = "1.0", features = ["derive"], optional = true } @@ -42,12 +42,12 @@ harness = false [features] secrecy-08 = ["dep:secrecy-08"] time-03 = ["dep:time-03"] -chrono = ["dep:chrono"] +chrono-04 = ["dep:chrono-04"] num-bigint-03 = ["dep:num-bigint-03"] num-bigint-04 = ["dep:num-bigint-04"] bigdecimal-04 = ["dep:bigdecimal-04"] full-serialization = [ - "chrono", + "chrono-04", "time-03", "secrecy-08", "num-bigint-03", diff --git a/scylla-cql/src/frame/response/cql_to_rust.rs b/scylla-cql/src/frame/response/cql_to_rust.rs index a40283bad6..435d45efcf 100644 --- a/scylla-cql/src/frame/response/cql_to_rust.rs +++ b/scylla-cql/src/frame/response/cql_to_rust.rs @@ -8,9 +8,6 @@ use std::net::IpAddr; use thiserror::Error; use uuid::Uuid; -#[cfg(feature = "chrono")] -use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; - #[derive(Error, Debug, Clone, PartialEq, Eq)] pub enum FromRowError { #[error("{err} in the column with index {column}")] @@ -175,8 +172,8 @@ impl FromCqlVal for bigdecimal_04::BigDecimal { } } -#[cfg(feature = "chrono")] -impl FromCqlVal for NaiveDate { +#[cfg(feature = "chrono-04")] +impl FromCqlVal for chrono_04::NaiveDate { fn from_cql(cql_val: CqlValue) -> Result { match cql_val { CqlValue::Date(cql_date) => cql_date.try_into().map_err(|_| FromCqlValError::BadVal), @@ -195,8 +192,8 @@ impl FromCqlVal for time_03::Date { } } -#[cfg(feature = "chrono")] -impl FromCqlVal for NaiveTime { +#[cfg(feature = "chrono-04")] +impl FromCqlVal for chrono_04::NaiveTime { fn from_cql(cql_val: CqlValue) -> Result { match cql_val { CqlValue::Time(cql_time) => cql_time.try_into().map_err(|_| FromCqlValError::BadVal), @@ -215,8 +212,8 @@ impl FromCqlVal for time_03::Time { } } -#[cfg(feature = "chrono")] -impl FromCqlVal for DateTime { +#[cfg(feature = "chrono-04")] +impl FromCqlVal for chrono_04::DateTime { fn from_cql(cql_val: CqlValue) -> Result { cql_val .as_cql_timestamp() @@ -523,10 +520,10 @@ mod tests { assert_eq!(Ok(counter), Counter::from_cql(CqlValue::Counter(counter))); } - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] #[test] - fn naive_date_from_cql() { - use chrono::NaiveDate; + fn naive_date_04_from_cql() { + use chrono_04::NaiveDate; let unix_epoch: CqlValue = CqlValue::Date(CqlDate(2_u32.pow(31))); assert_eq!( @@ -631,10 +628,10 @@ mod tests { assert_eq!(time_ns, CqlTime::from_cql(cql_value).unwrap().0); } - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] #[test] - fn naive_time_from_cql() { - use chrono::NaiveTime; + fn naive_time_04_from_cql() { + use chrono_04::NaiveTime; // Midnight let midnight = CqlValue::Time(CqlTime(0)); @@ -658,7 +655,7 @@ mod tests { NaiveTime::from_cql(late_night) ); - // Bad values. Since value is out of `chrono::NaiveTime` range, it should return `BadVal` error + // Bad values. Since value is out of `chrono_04::NaiveTime` range, it should return `BadVal` error let bad_time1 = CqlValue::Time(CqlTime(-1)); assert_eq!(Err(FromCqlValError::BadVal), NaiveTime::from_cql(bad_time1)); let bad_time2 = CqlValue::Time(CqlTime(i64::MAX)); @@ -731,10 +728,10 @@ mod tests { ); } - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] #[test] - fn datetime_from_cql() { - use chrono::{DateTime, NaiveDate, Utc}; + fn datetime_04_from_cql() { + use chrono_04::{DateTime, NaiveDate, Utc}; let naivedatetime_utc = NaiveDate::from_ymd_opt(2022, 12, 31) .unwrap() .and_hms_opt(2, 0, 0) diff --git a/scylla-cql/src/frame/response/result.rs b/scylla-cql/src/frame/response/result.rs index e6a6712cab..35b43b5bb3 100644 --- a/scylla-cql/src/frame/response/result.rs +++ b/scylla-cql/src/frame/response/result.rs @@ -12,9 +12,6 @@ use std::borrow::Cow; use std::{convert::TryInto, net::IpAddr, result::Result as StdResult, str}; use uuid::Uuid; -#[cfg(feature = "chrono")] -use chrono::{DateTime, NaiveDate, Utc}; - #[derive(Debug)] pub struct SetKeyspace { pub keyspace_name: String, @@ -187,8 +184,8 @@ impl CqlValue { } } - #[cfg(feature = "chrono")] - pub fn as_naive_date(&self) -> Option { + #[cfg(feature = "chrono-04")] + pub fn as_naive_date(&self) -> Option { self.as_cql_date().and_then(|date| date.try_into().ok()) } @@ -205,8 +202,8 @@ impl CqlValue { } } - #[cfg(feature = "chrono")] - pub fn as_datetime(&self) -> Option> { + #[cfg(feature = "chrono-04")] + pub fn as_datetime(&self) -> Option> { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } @@ -223,8 +220,8 @@ impl CqlValue { } } - #[cfg(feature = "chrono")] - pub fn as_naive_time(&self) -> Option { + #[cfg(feature = "chrono-04")] + pub fn as_naive_time(&self) -> Option { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } @@ -1272,10 +1269,10 @@ mod tests { assert_eq!(date.as_cql_date(), Some(max_date)); } - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] #[test] - fn test_naive_date_from_cql() { - use chrono::NaiveDate; + fn test_naive_date_04_from_cql() { + use chrono_04::NaiveDate; // 2^31 when converted to NaiveDate is 1970-01-01 let unix_epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(); @@ -1395,10 +1392,10 @@ mod tests { } } - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] #[test] - fn test_naive_time_from_cql() { - use chrono::NaiveTime; + fn test_naive_time_04_from_cql() { + use chrono_04::NaiveTime; // 0 when converted to NaiveTime is 0:0:0.0 let midnight = NaiveTime::from_hms_nano_opt(0, 0, 0, 0).unwrap(); @@ -1486,10 +1483,10 @@ mod tests { } } - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] #[test] - fn test_datetime_from_cql() { - use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime}; + fn test_datetime_04_from_cql() { + use chrono_04::{DateTime, NaiveDate, NaiveDateTime, NaiveTime}; // 0 when converted to DateTime is 1970-01-01 0:00:00.00 let unix_epoch = DateTime::from_timestamp(0, 0).unwrap(); diff --git a/scylla-cql/src/frame/value.rs b/scylla-cql/src/frame/value.rs index 89382324a7..a9c368d195 100644 --- a/scylla-cql/src/frame/value.rs +++ b/scylla-cql/src/frame/value.rs @@ -9,9 +9,6 @@ use std::net::IpAddr; use thiserror::Error; use uuid::Uuid; -#[cfg(feature = "chrono")] -use chrono::{DateTime, NaiveDate, NaiveTime, TimeZone, Utc}; - use super::response::result::CqlValue; use super::types::vint_encode; use super::types::RawValue; @@ -464,10 +461,10 @@ pub struct CqlTimestamp(pub i64); #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct CqlTime(pub i64); -#[cfg(feature = "chrono")] -impl From for CqlDate { - fn from(value: NaiveDate) -> Self { - let unix_epoch = NaiveDate::from_yo_opt(1970, 1).unwrap(); +#[cfg(feature = "chrono-04")] +impl From for CqlDate { + fn from(value: chrono_04::NaiveDate) -> Self { + let unix_epoch = chrono_04::NaiveDate::from_yo_opt(1970, 1).unwrap(); // `NaiveDate` range is -262145-01-01 to 262143-12-31 // Both values are well within supported range @@ -477,50 +474,52 @@ impl From for CqlDate { } } -#[cfg(feature = "chrono")] -impl TryInto for CqlDate { +#[cfg(feature = "chrono-04")] +impl TryInto for CqlDate { type Error = ValueOverflow; - fn try_into(self) -> Result { + fn try_into(self) -> Result { let days_since_unix_epoch = self.0 as i64 - (1 << 31); // date_days is u32 then converted to i64 then we subtract 2^31; // Max value is 2^31, min value is -2^31. Both values can safely fit in chrono::Duration, this call won't panic - let duration_since_unix_epoch = chrono::Duration::try_days(days_since_unix_epoch).unwrap(); + let duration_since_unix_epoch = + chrono_04::Duration::try_days(days_since_unix_epoch).unwrap(); - NaiveDate::from_yo_opt(1970, 1) + chrono_04::NaiveDate::from_yo_opt(1970, 1) .unwrap() .checked_add_signed(duration_since_unix_epoch) .ok_or(ValueOverflow) } } -#[cfg(feature = "chrono")] -impl From> for CqlTimestamp { - fn from(value: DateTime) -> Self { +#[cfg(feature = "chrono-04")] +impl From> for CqlTimestamp { + fn from(value: chrono_04::DateTime) -> Self { Self(value.timestamp_millis()) } } -#[cfg(feature = "chrono")] -impl TryInto> for CqlTimestamp { +#[cfg(feature = "chrono-04")] +impl TryInto> for CqlTimestamp { type Error = ValueOverflow; - fn try_into(self) -> Result, Self::Error> { - match Utc.timestamp_millis_opt(self.0) { - chrono::LocalResult::Single(datetime) => Ok(datetime), + fn try_into(self) -> Result, Self::Error> { + use chrono_04::TimeZone; + match chrono_04::Utc.timestamp_millis_opt(self.0) { + chrono_04::LocalResult::Single(datetime) => Ok(datetime), _ => Err(ValueOverflow), } } } -#[cfg(feature = "chrono")] -impl TryFrom for CqlTime { +#[cfg(feature = "chrono-04")] +impl TryFrom for CqlTime { type Error = ValueOverflow; - fn try_from(value: NaiveTime) -> Result { + fn try_from(value: chrono_04::NaiveTime) -> Result { let nanos = value - .signed_duration_since(chrono::NaiveTime::MIN) + .signed_duration_since(chrono_04::NaiveTime::MIN) .num_nanoseconds() .unwrap(); @@ -533,18 +532,18 @@ impl TryFrom for CqlTime { } } -#[cfg(feature = "chrono")] -impl TryInto for CqlTime { +#[cfg(feature = "chrono-04")] +impl TryInto for CqlTime { type Error = ValueOverflow; - fn try_into(self) -> Result { + fn try_into(self) -> Result { let secs = (self.0 / 1_000_000_000) .try_into() .map_err(|_| ValueOverflow)?; let nanos = (self.0 % 1_000_000_000) .try_into() .map_err(|_| ValueOverflow)?; - NaiveTime::from_num_seconds_from_midnight_opt(secs, nanos).ok_or(ValueOverflow) + chrono_04::NaiveTime::from_num_seconds_from_midnight_opt(secs, nanos).ok_or(ValueOverflow) } } @@ -1007,8 +1006,8 @@ impl Value for bigdecimal_04::BigDecimal { } } -#[cfg(feature = "chrono")] -impl Value for NaiveDate { +#[cfg(feature = "chrono-04")] +impl Value for chrono_04::NaiveDate { fn serialize(&self, buf: &mut Vec) -> Result<(), ValueTooBig> { CqlDate::from(*self).serialize(buf) } @@ -1045,8 +1044,8 @@ impl Value for CqlTime { } } -#[cfg(feature = "chrono")] -impl Value for DateTime { +#[cfg(feature = "chrono-04")] +impl Value for chrono_04::DateTime { fn serialize(&self, buf: &mut Vec) -> Result<(), ValueTooBig> { CqlTimestamp::from(*self).serialize(buf) } @@ -1059,8 +1058,8 @@ impl Value for time_03::OffsetDateTime { } } -#[cfg(feature = "chrono")] -impl Value for NaiveTime { +#[cfg(feature = "chrono-04")] +impl Value for chrono_04::NaiveTime { fn serialize(&self, buf: &mut Vec) -> Result<(), ValueTooBig> { CqlTime::try_from(*self) .map_err(|_| ValueTooBig)? diff --git a/scylla-cql/src/frame/value_tests.rs b/scylla-cql/src/frame/value_tests.rs index 561d9571b9..d060fde0b4 100644 --- a/scylla-cql/src/frame/value_tests.rs +++ b/scylla-cql/src/frame/value_tests.rs @@ -306,10 +306,10 @@ fn ipaddr_serialization() { ); } -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] #[test] -fn naive_date_serialization() { - use chrono::NaiveDate; +fn naive_date_04_serialization() { + use chrono_04::NaiveDate; // 1970-01-31 is 2^31 let unix_epoch: NaiveDate = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(); assert_eq!( @@ -412,10 +412,10 @@ fn cql_time_serialization() { } } -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] #[test] -fn naive_time_serialization() { - use chrono::NaiveTime; +fn naive_time_04_serialization() { + use chrono_04::NaiveTime; let midnight_time: i64 = 0; let max_time: i64 = 24 * 60 * 60 * 1_000_000_000 - 1; @@ -492,10 +492,10 @@ fn cql_timestamp_serialization() { } } -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] #[test] -fn date_time_serialization() { - use chrono::{DateTime, Utc}; +fn date_time_04_serialization() { + use chrono_04::{DateTime, Utc}; let test_cases: [(DateTime, [u8; 8]); 7] = [ ( // Max time serialized without error diff --git a/scylla-cql/src/types/serialize/value.rs b/scylla-cql/src/types/serialize/value.rs index c589e0d09d..2a7040b789 100644 --- a/scylla-cql/src/types/serialize/value.rs +++ b/scylla-cql/src/types/serialize/value.rs @@ -9,9 +9,6 @@ use std::sync::Arc; use thiserror::Error; use uuid::Uuid; -#[cfg(feature = "chrono")] -use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; - use crate::frame::response::result::{ColumnType, CqlValue}; use crate::frame::types::vint_encode; use crate::frame::value::{ @@ -19,7 +16,7 @@ use crate::frame::value::{ MaybeUnset, Unset, Value, }; -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] use crate::frame::value::ValueOverflow; use super::writers::WrittenCellProof; @@ -157,22 +154,22 @@ impl SerializeValue for CqlTime { writer.set_value(me.0.to_be_bytes().as_slice()).unwrap() }); } -#[cfg(feature = "chrono")] -impl SerializeValue for NaiveDate { +#[cfg(feature = "chrono-04")] +impl SerializeValue for chrono_04::NaiveDate { impl_serialize_via_writer!(|me, typ, writer| { exact_type_check!(typ, Date); ::serialize(&(*me).into(), typ, writer)? }); } -#[cfg(feature = "chrono")] -impl SerializeValue for DateTime { +#[cfg(feature = "chrono-04")] +impl SerializeValue for chrono_04::DateTime { impl_serialize_via_writer!(|me, typ, writer| { exact_type_check!(typ, Timestamp); ::serialize(&(*me).into(), typ, writer)? }); } -#[cfg(feature = "chrono")] -impl SerializeValue for NaiveTime { +#[cfg(feature = "chrono-04")] +impl SerializeValue for chrono_04::NaiveTime { impl_serialize_via_writer!(|me, typ, writer| { exact_type_check!(typ, Time); let cql_time = CqlTime::try_from(*me).map_err(|_: ValueOverflow| { diff --git a/scylla/Cargo.toml b/scylla/Cargo.toml index a49aff72d4..3096ff7d6d 100644 --- a/scylla/Cargo.toml +++ b/scylla/Cargo.toml @@ -25,13 +25,13 @@ cloud = [ "dep:base64", ] secrecy-08 = ["scylla-cql/secrecy-08"] -chrono = ["scylla-cql/chrono"] +chrono-04 = ["scylla-cql/chrono-04"] time-03 = ["scylla-cql/time-03"] num-bigint-03 = ["scylla-cql/num-bigint-03"] num-bigint-04 = ["scylla-cql/num-bigint-04"] bigdecimal-04 = ["scylla-cql/bigdecimal-04"] full-serialization = [ - "chrono", + "chrono-04", "time-03", "secrecy-08", "num-bigint-03", diff --git a/scylla/src/transport/cql_types_test.rs b/scylla/src/transport/cql_types_test.rs index 3e253f233e..1beea8b3fd 100644 --- a/scylla/src/transport/cql_types_test.rs +++ b/scylla/src/transport/cql_types_test.rs @@ -293,9 +293,9 @@ async fn test_counter() { } } -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] #[tokio::test] -async fn test_naive_date() { +async fn test_naive_date_04() { setup_tracing(); use chrono::Datelike; use chrono::NaiveDate; @@ -620,9 +620,9 @@ async fn test_cql_time() { } } -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] #[tokio::test] -async fn test_naive_time() { +async fn test_naive_time_04() { setup_tracing(); use chrono::NaiveTime; @@ -842,9 +842,9 @@ async fn test_cql_timestamp() { } } -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] #[tokio::test] -async fn test_date_time() { +async fn test_date_time_04() { setup_tracing(); use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};