From 909d617736dec840dad2d92edc82bc1c79055506 Mon Sep 17 00:00:00 2001 From: muzarski Date: Thu, 22 Feb 2024 15:58:33 +0100 Subject: [PATCH 1/6] cargo: bump `secrecy` version to 0.8 --- Cargo.lock.msrv | 4 ++-- scylla-cql/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock.msrv b/Cargo.lock.msrv index 4ddbe9dae1..3168c5fcb7 100644 --- a/Cargo.lock.msrv +++ b/Cargo.lock.msrv @@ -1577,9 +1577,9 @@ dependencies = [ [[package]] name = "secrecy" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0673d6a6449f5e7d12a1caf424fd9363e2af3a4953023ed455e3c4beef4597c0" +checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e" dependencies = [ "zeroize", ] diff --git a/scylla-cql/Cargo.toml b/scylla-cql/Cargo.toml index 547404cae7..7c1727cf0e 100644 --- a/scylla-cql/Cargo.toml +++ b/scylla-cql/Cargo.toml @@ -15,7 +15,7 @@ byteorder = "1.3.4" bytes = "1.0.1" # FIXME: Remove <1.38 once https://github.com/tokio-rs/tokio/issues/6610 is fixed tokio = { version = ">=1.34, <1.38", features = ["io-util", "time"] } -secrecy = { version = "0.7.0", optional = true } +secrecy = { version = "0.8", optional = true } snap = "1.0" uuid = "1.0" thiserror = "1.0" From 9e014b7df1b3335edcc5e5f2370436f86320727e Mon Sep 17 00:00:00 2001 From: muzarski Date: Thu, 22 Feb 2024 16:19:15 +0100 Subject: [PATCH 2/6] cargo: rename `secret` feature to `secrecy-08` --- .github/workflows/rust.yml | 4 ++-- scylla-cql/Cargo.toml | 6 +++--- scylla-cql/src/frame/response/cql_to_rust.rs | 9 +++------ scylla-cql/src/frame/value.rs | 8 +++----- scylla-cql/src/frame/value_tests.rs | 5 ++--- scylla-cql/src/types/deserialize/value.rs | 8 ++++---- scylla-cql/src/types/serialize/value.rs | 8 +++----- scylla/Cargo.toml | 4 ++-- 8 files changed, 22 insertions(+), 30 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index bae8c8b8b1..2f7e813773 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -44,8 +44,8 @@ jobs: run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "full-serialization" - name: Cargo check with all features run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --all-features - - name: Cargo check with secret feature - run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "secret" + - 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 time feature diff --git a/scylla-cql/Cargo.toml b/scylla-cql/Cargo.toml index 7c1727cf0e..f0969200ea 100644 --- a/scylla-cql/Cargo.toml +++ b/scylla-cql/Cargo.toml @@ -15,7 +15,7 @@ byteorder = "1.3.4" bytes = "1.0.1" # FIXME: Remove <1.38 once https://github.com/tokio-rs/tokio/issues/6610 is fixed tokio = { version = ">=1.34, <1.38", features = ["io-util", "time"] } -secrecy = { version = "0.8", optional = true } +secrecy-08 = { package = "secrecy", version = "0.8", optional = true } snap = "1.0" uuid = "1.0" thiserror = "1.0" @@ -40,7 +40,7 @@ name = "benchmark" harness = false [features] -secret = ["secrecy"] +secrecy-08 = ["dep:secrecy-08"] time = ["dep:time"] chrono = ["dep:chrono"] num-bigint-03 = ["dep:num-bigint-03"] @@ -49,7 +49,7 @@ bigdecimal-04 = ["dep:bigdecimal-04"] full-serialization = [ "chrono", "time", - "secret", + "secrecy-08", "num-bigint-03", "num-bigint-04", "bigdecimal-04", diff --git a/scylla-cql/src/frame/response/cql_to_rust.rs b/scylla-cql/src/frame/response/cql_to_rust.rs index c55dd3ee85..d78bcec6e2 100644 --- a/scylla-cql/src/frame/response/cql_to_rust.rs +++ b/scylla-cql/src/frame/response/cql_to_rust.rs @@ -11,9 +11,6 @@ use uuid::Uuid; #[cfg(feature = "chrono")] use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; -#[cfg(feature = "secret")] -use secrecy::{Secret, Zeroize}; - #[derive(Error, Debug, Clone, PartialEq, Eq)] pub enum FromRowError { #[error("{err} in the column with index {column}")] @@ -240,10 +237,10 @@ impl FromCqlVal for time::OffsetDateTime { } } -#[cfg(feature = "secret")] -impl + Zeroize> FromCqlVal for Secret { +#[cfg(feature = "secrecy-08")] +impl + secrecy_08::Zeroize> FromCqlVal for secrecy_08::Secret { fn from_cql(cql_val: CqlValue) -> Result { - Ok(Secret::new(FromCqlVal::from_cql(cql_val)?)) + Ok(secrecy_08::Secret::new(FromCqlVal::from_cql(cql_val)?)) } } diff --git a/scylla-cql/src/frame/value.rs b/scylla-cql/src/frame/value.rs index 6728675ef0..a1445ad276 100644 --- a/scylla-cql/src/frame/value.rs +++ b/scylla-cql/src/frame/value.rs @@ -16,9 +16,6 @@ use super::response::result::CqlValue; use super::types::vint_encode; use super::types::RawValue; -#[cfg(feature = "secret")] -use secrecy::{ExposeSecret, Secret, Zeroize}; - /// Every value being sent in a query must implement this trait /// serialize() should write the Value as [bytes] to the provided buffer pub trait Value { @@ -1076,9 +1073,10 @@ impl Value for time::Time { } } -#[cfg(feature = "secret")] -impl Value for Secret { +#[cfg(feature = "secrecy-08")] +impl Value for secrecy_08::Secret { fn serialize(&self, buf: &mut Vec) -> Result<(), ValueTooBig> { + use secrecy_08::ExposeSecret; self.expose_secret().serialize(buf) } } diff --git a/scylla-cql/src/frame/value_tests.rs b/scylla-cql/src/frame/value_tests.rs index 7af026d124..52328ed73e 100644 --- a/scylla-cql/src/frame/value_tests.rs +++ b/scylla-cql/src/frame/value_tests.rs @@ -872,11 +872,10 @@ fn cqlvalue_serialization() { ); } -#[cfg(feature = "secret")] +#[cfg(feature = "secrecy-08")] #[test] fn secret_serialization() { - use secrecy::Secret; - let secret = Secret::new(987654i32); + let secret = secrecy_08::Secret::new(987654i32); assert_eq!( serialized(secret, ColumnType::Int), vec![0, 0, 0, 4, 0x00, 0x0f, 0x12, 0x06] diff --git a/scylla-cql/src/types/deserialize/value.rs b/scylla-cql/src/types/deserialize/value.rs index 95103998e2..28124e7bc4 100644 --- a/scylla-cql/src/types/deserialize/value.rs +++ b/scylla-cql/src/types/deserialize/value.rs @@ -616,10 +616,10 @@ impl_emptiable_strict_type!( ); // secrecy -#[cfg(feature = "secret")] -impl<'frame, T> DeserializeValue<'frame> for secrecy::Secret +#[cfg(feature = "secrecy-08")] +impl<'frame, T> DeserializeValue<'frame> for secrecy_08::Secret where - T: DeserializeValue<'frame> + secrecy::Zeroize, + T: DeserializeValue<'frame> + secrecy_08::Zeroize, { fn type_check(typ: &ColumnType) -> Result<(), TypeCheckError> { >::type_check(typ) @@ -629,7 +629,7 @@ where typ: &'frame ColumnType, v: Option>, ) -> Result { - >::deserialize(typ, v).map(secrecy::Secret::new) + >::deserialize(typ, v).map(secrecy_08::Secret::new) } } diff --git a/scylla-cql/src/types/serialize/value.rs b/scylla-cql/src/types/serialize/value.rs index 5ef6c8c6a8..cdb75c7076 100644 --- a/scylla-cql/src/types/serialize/value.rs +++ b/scylla-cql/src/types/serialize/value.rs @@ -12,9 +12,6 @@ use uuid::Uuid; #[cfg(feature = "chrono")] use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; -#[cfg(feature = "secret")] -use secrecy::{ExposeSecret, Secret, Zeroize}; - use crate::frame::response::result::{ColumnType, CqlValue}; use crate::frame::types::vint_encode; use crate::frame::value::{ @@ -205,13 +202,14 @@ impl SerializeValue for time::Time { ::serialize(&(*me).into(), typ, writer)? }); } -#[cfg(feature = "secret")] -impl SerializeValue for Secret { +#[cfg(feature = "secrecy-08")] +impl SerializeValue for secrecy_08::Secret { fn serialize<'b>( &self, typ: &ColumnType, writer: CellWriter<'b>, ) -> Result, SerializationError> { + use secrecy_08::ExposeSecret; V::serialize(self.expose_secret(), typ, writer) } } diff --git a/scylla/Cargo.toml b/scylla/Cargo.toml index 490a64c80c..2d39c91441 100644 --- a/scylla/Cargo.toml +++ b/scylla/Cargo.toml @@ -24,7 +24,7 @@ cloud = [ "dep:url", "dep:base64", ] -secret = ["scylla-cql/secret"] +secrecy-08 = ["scylla-cql/secrecy-08"] chrono = ["scylla-cql/chrono"] time = ["scylla-cql/time"] num-bigint-03 = ["scylla-cql/num-bigint-03"] @@ -33,7 +33,7 @@ bigdecimal-04 = ["scylla-cql/bigdecimal-04"] full-serialization = [ "chrono", "time", - "secret", + "secrecy-08", "num-bigint-03", "num-bigint-04", "bigdecimal-04", From 248d2db36a681422e810ef79dc71c084894eb0b6 Mon Sep 17 00:00:00 2001 From: muzarski Date: Thu, 22 Feb 2024 16:29:20 +0100 Subject: [PATCH 3/6] cargo: rename `time` feature to `time-03`. --- .github/workflows/rust.yml | 4 +- examples/Cargo.toml | 2 +- scylla-cql/Cargo.toml | 8 +- scylla-cql/src/frame/response/cql_to_rust.rs | 109 ++++++++++--------- scylla-cql/src/frame/response/result.rs | 38 +++---- scylla-cql/src/frame/value.rs | 72 ++++++------ scylla-cql/src/frame/value_tests.rs | 41 +++---- scylla-cql/src/types/deserialize/value.rs | 36 +++--- scylla-cql/src/types/serialize/value.rs | 12 +- scylla/Cargo.toml | 4 +- scylla/src/transport/cql_types_test.rs | 12 +- 11 files changed, 176 insertions(+), 162 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2f7e813773..9dd9cc2f96 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -48,8 +48,8 @@ jobs: 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 time feature - run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "time" + - 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 run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "num-bigint-03" - name: Cargo check with num-bigint-04 feature diff --git a/examples/Cargo.toml b/examples/Cargo.toml index d71e413675..0ddc1ec99e 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -14,7 +14,7 @@ scylla = { path = "../scylla", features = [ "ssl", "cloud", "chrono", - "time", + "time-03", "num-bigint-03", "num-bigint-04", "bigdecimal-04", diff --git a/scylla-cql/Cargo.toml b/scylla-cql/Cargo.toml index f0969200ea..2c17478cf5 100644 --- a/scylla-cql/Cargo.toml +++ b/scylla-cql/Cargo.toml @@ -26,13 +26,13 @@ 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 } -time = { version = "0.3", optional = true } +time-03 = { package = "time", version = "0.3", optional = true } [dev-dependencies] assert_matches = "1.5.0" criterion = "0.4" # Note: v0.5 needs at least rust 1.70.0 # Use large-dates feature to test potential edge cases -time = { version = "0.3.21", features = ["large-dates"] } +time-03 = { package = "time", version = "0.3.21", features = ["large-dates"] } uuid = { version = "1.0", features = ["v4"] } [[bench]] @@ -41,14 +41,14 @@ harness = false [features] secrecy-08 = ["dep:secrecy-08"] -time = ["dep:time"] +time-03 = ["dep:time-03"] chrono = ["dep:chrono"] num-bigint-03 = ["dep:num-bigint-03"] num-bigint-04 = ["dep:num-bigint-04"] bigdecimal-04 = ["dep:bigdecimal-04"] full-serialization = [ "chrono", - "time", + "time-03", "secrecy-08", "num-bigint-03", "num-bigint-04", diff --git a/scylla-cql/src/frame/response/cql_to_rust.rs b/scylla-cql/src/frame/response/cql_to_rust.rs index d78bcec6e2..a40283bad6 100644 --- a/scylla-cql/src/frame/response/cql_to_rust.rs +++ b/scylla-cql/src/frame/response/cql_to_rust.rs @@ -185,8 +185,8 @@ impl FromCqlVal for NaiveDate { } } -#[cfg(feature = "time")] -impl FromCqlVal for time::Date { +#[cfg(feature = "time-03")] +impl FromCqlVal for time_03::Date { fn from_cql(cql_val: CqlValue) -> Result { match cql_val { CqlValue::Date(cql_date) => cql_date.try_into().map_err(|_| FromCqlValError::BadVal), @@ -205,8 +205,8 @@ impl FromCqlVal for NaiveTime { } } -#[cfg(feature = "time")] -impl FromCqlVal for time::Time { +#[cfg(feature = "time-03")] +impl FromCqlVal for time_03::Time { fn from_cql(cql_val: CqlValue) -> Result { match cql_val { CqlValue::Time(cql_time) => cql_time.try_into().map_err(|_| FromCqlValError::BadVal), @@ -226,8 +226,8 @@ impl FromCqlVal for DateTime { } } -#[cfg(feature = "time")] -impl FromCqlVal for time::OffsetDateTime { +#[cfg(feature = "time-03")] +impl FromCqlVal for time_03::OffsetDateTime { fn from_cql(cql_val: CqlValue) -> Result { cql_val .as_cql_timestamp() @@ -565,43 +565,49 @@ mod tests { assert_eq!(Ok(CqlDate(u32::MAX)), CqlDate::from_cql(max_date)); } - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] #[test] - fn date_from_cql() { + fn date_03_from_cql() { // UNIX epoch let unix_epoch = CqlValue::Date(CqlDate(1 << 31)); assert_eq!( - Ok(time::Date::from_ordinal_date(1970, 1).unwrap()), - time::Date::from_cql(unix_epoch) + Ok(time_03::Date::from_ordinal_date(1970, 1).unwrap()), + time_03::Date::from_cql(unix_epoch) ); // 7 days after UNIX epoch let after_epoch = CqlValue::Date(CqlDate((1 << 31) + 7)); assert_eq!( - Ok(time::Date::from_ordinal_date(1970, 8).unwrap()), - time::Date::from_cql(after_epoch) + Ok(time_03::Date::from_ordinal_date(1970, 8).unwrap()), + time_03::Date::from_cql(after_epoch) ); // 3 days before UNIX epoch let before_epoch = CqlValue::Date(CqlDate((1 << 31) - 3)); assert_eq!( - Ok(time::Date::from_ordinal_date(1969, 363).unwrap()), - time::Date::from_cql(before_epoch) + Ok(time_03::Date::from_ordinal_date(1969, 363).unwrap()), + time_03::Date::from_cql(before_epoch) ); - // Min possible stored date. Since value is out of `time::Date` range, it should return `BadVal` error + // Min possible stored date. Since value is out of `time_03::Date` range, it should return `BadVal` error let min_date = CqlValue::Date(CqlDate(u32::MIN)); - assert_eq!(Err(FromCqlValError::BadVal), time::Date::from_cql(min_date)); + assert_eq!( + Err(FromCqlValError::BadVal), + time_03::Date::from_cql(min_date) + ); - // Max possible stored date. Since value is out of `time::Date` range, it should return `BadVal` error + // Max possible stored date. Since value is out of `time_03::Date` range, it should return `BadVal` error let max_date = CqlValue::Date(CqlDate(u32::MAX)); - assert_eq!(Err(FromCqlValError::BadVal), time::Date::from_cql(max_date)); + assert_eq!( + Err(FromCqlValError::BadVal), + time_03::Date::from_cql(max_date) + ); - // Different CQL type. Since value can't be casted, it should return `BadCqlType` error + // Different CQL type. Since value can't be cast, it should return `BadCqlType` error let bad_type = CqlValue::Double(0.5); assert_eq!( Err(FromCqlValError::BadCqlType), - time::Date::from_cql(bad_type) + time_03::Date::from_cql(bad_type) ); } @@ -658,7 +664,7 @@ mod tests { let bad_time2 = CqlValue::Time(CqlTime(i64::MAX)); assert_eq!(Err(FromCqlValError::BadVal), NaiveTime::from_cql(bad_time2)); - // Different CQL type. Since value can't be casted, it should return `BadCqlType` error + // Different CQL type. Since value can't be cast, it should return `BadCqlType` error let bad_type = CqlValue::Double(0.5); assert_eq!( Err(FromCqlValError::BadCqlType), @@ -666,20 +672,23 @@ mod tests { ); } - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] #[test] - fn time_from_cql() { + fn time_03_from_cql() { // Midnight let midnight = CqlValue::Time(CqlTime(0)); - assert_eq!(Ok(time::Time::MIDNIGHT), time::Time::from_cql(midnight)); + assert_eq!( + Ok(time_03::Time::MIDNIGHT), + time_03::Time::from_cql(midnight) + ); // 7:15:21.123456789 let morning = CqlValue::Time(CqlTime( (7 * 3600 + 15 * 60 + 21) * 1_000_000_000 + 123_456_789, )); assert_eq!( - Ok(time::Time::from_hms_nano(7, 15, 21, 123_456_789).unwrap()), - time::Time::from_cql(morning) + Ok(time_03::Time::from_hms_nano(7, 15, 21, 123_456_789).unwrap()), + time_03::Time::from_cql(morning) ); // 23:59:59.999999999 @@ -687,27 +696,27 @@ mod tests { (23 * 3600 + 59 * 60 + 59) * 1_000_000_000 + 999_999_999, )); assert_eq!( - Ok(time::Time::from_hms_nano(23, 59, 59, 999_999_999).unwrap()), - time::Time::from_cql(late_night) + Ok(time_03::Time::from_hms_nano(23, 59, 59, 999_999_999).unwrap()), + time_03::Time::from_cql(late_night) ); - // Bad values. Since value is out of `time::Time` range, it should return `BadVal` error + // Bad values. Since value is out of `time_03::Time` range, it should return `BadVal` error let bad_time1 = CqlValue::Time(CqlTime(-1)); assert_eq!( Err(FromCqlValError::BadVal), - time::Time::from_cql(bad_time1) + time_03::Time::from_cql(bad_time1) ); let bad_time2 = CqlValue::Time(CqlTime(i64::MAX)); assert_eq!( Err(FromCqlValError::BadVal), - time::Time::from_cql(bad_time2) + time_03::Time::from_cql(bad_time2) ); - // Different CQL type. Since value can't be casted, it should return `BadCqlType` error + // Different CQL type. Since value can't be cast, it should return `BadCqlType` error let bad_type = CqlValue::Double(0.5); assert_eq!( Err(FromCqlValError::BadCqlType), - time::Time::from_cql(bad_type) + time_03::Time::from_cql(bad_type) ); } @@ -741,55 +750,55 @@ mod tests { ); } - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] #[test] - fn offset_datetime_from_cql() { + fn offset_datetime_03_from_cql() { // UNIX epoch let unix_epoch = CqlValue::Timestamp(CqlTimestamp(0)); assert_eq!( - Ok(time::OffsetDateTime::UNIX_EPOCH), - time::OffsetDateTime::from_cql(unix_epoch) + Ok(time_03::OffsetDateTime::UNIX_EPOCH), + time_03::OffsetDateTime::from_cql(unix_epoch) ); // 1 day 2 hours 3 minutes 4 seconds and 5 nanoseconds before UNIX epoch let before_epoch = CqlValue::Timestamp(CqlTimestamp(-(26 * 3600 + 3 * 60 + 4) * 1000 - 5)); assert_eq!( - Ok(time::OffsetDateTime::UNIX_EPOCH - - time::Duration::new(26 * 3600 + 3 * 60 + 4, 5 * 1_000_000)), - time::OffsetDateTime::from_cql(before_epoch) + Ok(time_03::OffsetDateTime::UNIX_EPOCH + - time_03::Duration::new(26 * 3600 + 3 * 60 + 4, 5 * 1_000_000)), + time_03::OffsetDateTime::from_cql(before_epoch) ); // 6 days 7 hours 8 minutes 9 seconds and 10 nanoseconds after UNIX epoch let after_epoch = CqlValue::Timestamp(CqlTimestamp(((6 * 24 + 7) * 3600 + 8 * 60 + 9) * 1000 + 10)); assert_eq!( - Ok(time::PrimitiveDateTime::new( - time::Date::from_ordinal_date(1970, 7).unwrap(), - time::Time::from_hms_milli(7, 8, 9, 10).unwrap() + Ok(time_03::PrimitiveDateTime::new( + time_03::Date::from_ordinal_date(1970, 7).unwrap(), + time_03::Time::from_hms_milli(7, 8, 9, 10).unwrap() ) .assume_utc()), - time::OffsetDateTime::from_cql(after_epoch) + time_03::OffsetDateTime::from_cql(after_epoch) ); - // Min possible stored timestamp. Since value is out of `time::OffsetDateTime` range, it should return `BadVal` error + // Min possible stored timestamp. Since value is out of `time_03::OffsetDateTime` range, it should return `BadVal` error let min_timestamp = CqlValue::Timestamp(CqlTimestamp(i64::MIN)); assert_eq!( Err(FromCqlValError::BadVal), - time::OffsetDateTime::from_cql(min_timestamp) + time_03::OffsetDateTime::from_cql(min_timestamp) ); - // Max possible stored timestamp. Since value is out of `time::OffsetDateTime` range, it should return `BadVal` error + // Max possible stored timestamp. Since value is out of `time_03::OffsetDateTime` range, it should return `BadVal` error let max_timestamp = CqlValue::Timestamp(CqlTimestamp(i64::MAX)); assert_eq!( Err(FromCqlValError::BadVal), - time::OffsetDateTime::from_cql(max_timestamp) + time_03::OffsetDateTime::from_cql(max_timestamp) ); - // Different CQL type. Since value can't be casted, it should return `BadCqlType` error + // Different CQL type. Since value can't be cast, it should return `BadCqlType` error let bad_type = CqlValue::Double(0.5); assert_eq!( Err(FromCqlValError::BadCqlType), - time::OffsetDateTime::from_cql(bad_type) + time_03::OffsetDateTime::from_cql(bad_type) ); } diff --git a/scylla-cql/src/frame/response/result.rs b/scylla-cql/src/frame/response/result.rs index f961f4b99e..e5815d9daa 100644 --- a/scylla-cql/src/frame/response/result.rs +++ b/scylla-cql/src/frame/response/result.rs @@ -192,8 +192,8 @@ impl CqlValue { self.as_cql_date().and_then(|date| date.try_into().ok()) } - #[cfg(feature = "time")] - pub fn as_date(&self) -> Option { + #[cfg(feature = "time-03")] + pub fn as_date(&self) -> Option { self.as_cql_date().and_then(|date| date.try_into().ok()) } @@ -209,8 +209,8 @@ impl CqlValue { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } - #[cfg(feature = "time")] - pub fn as_offset_date_time(&self) -> Option { + #[cfg(feature = "time-03")] + pub fn as_offset_date_time(&self) -> Option { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } @@ -226,8 +226,8 @@ impl CqlValue { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } - #[cfg(feature = "time")] - pub fn as_time(&self) -> Option { + #[cfg(feature = "time-03")] + pub fn as_time(&self) -> Option { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } @@ -1318,13 +1318,13 @@ mod tests { ); } - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] #[test] - fn test_date_from_cql() { - use time::Date; - use time::Month::*; + fn test_date_03_from_cql() { + use time_03::Date; + use time_03::Month::*; - // 2^31 when converted to time::Date is 1970-01-01 + // 2^31 when converted to time_03::Date is 1970-01-01 let unix_epoch = Date::from_calendar_date(1970, January, 1).unwrap(); let date = super::deser_cql_value(&ColumnType::Date, &mut (1u32 << 31).to_be_bytes().as_ref()) @@ -1332,7 +1332,7 @@ mod tests { assert_eq!(date.as_date(), Some(unix_epoch)); - // 2^31 - 30 when converted to time::Date is 1969-12-02 + // 2^31 - 30 when converted to time_03::Date is 1969-12-02 let before_epoch = Date::from_calendar_date(1969, December, 2).unwrap(); let date = super::deser_cql_value( &ColumnType::Date, @@ -1342,7 +1342,7 @@ mod tests { assert_eq!(date.as_date(), Some(before_epoch)); - // 2^31 + 30 when converted to time::Date is 1970-01-31 + // 2^31 + 30 when converted to time_03::Date is 1970-01-31 let after_epoch = Date::from_calendar_date(1970, January, 31).unwrap(); let date = super::deser_cql_value( &ColumnType::Date, @@ -1431,10 +1431,10 @@ mod tests { assert_eq!(time.as_naive_time(), Some(midnight)); } - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] #[test] - fn test_primitive_time_from_cql() { - use time::Time; + fn test_primitive_time_03_from_cql() { + use time_03::Time; // 0 when converted to NaiveTime is 0:0:0.0 let midnight = Time::from_hms_nano(0, 0, 0, 0).unwrap(); @@ -1542,10 +1542,10 @@ mod tests { ); } - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] #[test] - fn test_offset_datetime_from_cql() { - use time::{Date, Month::*, OffsetDateTime, PrimitiveDateTime, Time}; + fn test_offset_datetime_03_from_cql() { + use time_03::{Date, Month::*, OffsetDateTime, PrimitiveDateTime, Time}; // 0 when converted to OffsetDateTime is 1970-01-01 0:00:00.00 let unix_epoch = OffsetDateTime::from_unix_timestamp(0).unwrap(); diff --git a/scylla-cql/src/frame/value.rs b/scylla-cql/src/frame/value.rs index a1445ad276..89382324a7 100644 --- a/scylla-cql/src/frame/value.rs +++ b/scylla-cql/src/frame/value.rs @@ -548,17 +548,19 @@ impl TryInto for CqlTime { } } -#[cfg(feature = "time")] -impl From for CqlDate { - fn from(value: time::Date) -> Self { +#[cfg(feature = "time-03")] +impl From for CqlDate { + fn from(value: time_03::Date) -> Self { const JULIAN_DAY_OFFSET: i64 = - (1 << 31) - time::OffsetDateTime::UNIX_EPOCH.date().to_julian_day() as i64; + (1 << 31) - time_03::OffsetDateTime::UNIX_EPOCH.date().to_julian_day() as i64; // Statically assert that no possible value will ever overflow - const _: () = - assert!(time::Date::MAX.to_julian_day() as i64 + JULIAN_DAY_OFFSET < u32::MAX as i64); - const _: () = - assert!(time::Date::MIN.to_julian_day() as i64 + JULIAN_DAY_OFFSET > u32::MIN as i64); + const _: () = assert!( + time_03::Date::MAX.to_julian_day() as i64 + JULIAN_DAY_OFFSET < u32::MAX as i64 + ); + const _: () = assert!( + time_03::Date::MIN.to_julian_day() as i64 + JULIAN_DAY_OFFSET > u32::MIN as i64 + ); let days = value.to_julian_day() as i64 + JULIAN_DAY_OFFSET; @@ -566,29 +568,29 @@ impl From for CqlDate { } } -#[cfg(feature = "time")] -impl TryInto for CqlDate { +#[cfg(feature = "time-03")] +impl TryInto for CqlDate { type Error = ValueOverflow; - fn try_into(self) -> Result { + fn try_into(self) -> Result { const JULIAN_DAY_OFFSET: i64 = - (1 << 31) - time::OffsetDateTime::UNIX_EPOCH.date().to_julian_day() as i64; + (1 << 31) - time_03::OffsetDateTime::UNIX_EPOCH.date().to_julian_day() as i64; let julian_days = (self.0 as i64 - JULIAN_DAY_OFFSET) .try_into() .map_err(|_| ValueOverflow)?; - time::Date::from_julian_day(julian_days).map_err(|_| ValueOverflow) + time_03::Date::from_julian_day(julian_days).map_err(|_| ValueOverflow) } } -#[cfg(feature = "time")] -impl From for CqlTimestamp { - fn from(value: time::OffsetDateTime) -> Self { +#[cfg(feature = "time-03")] +impl From for CqlTimestamp { + fn from(value: time_03::OffsetDateTime) -> Self { // Statically assert that no possible value will ever overflow. OffsetDateTime doesn't allow offset to overflow // the UTC PrimitiveDateTime value value const _: () = assert!( - time::PrimitiveDateTime::MAX + time_03::PrimitiveDateTime::MAX .assume_utc() .unix_timestamp_nanos() // Nanos to millis @@ -596,7 +598,7 @@ impl From for CqlTimestamp { < i64::MAX as i128 ); const _: () = assert!( - time::PrimitiveDateTime::MIN + time_03::PrimitiveDateTime::MIN .assume_utc() .unix_timestamp_nanos() / 1_000_000 @@ -608,19 +610,19 @@ impl From for CqlTimestamp { } } -#[cfg(feature = "time")] -impl TryInto for CqlTimestamp { +#[cfg(feature = "time-03")] +impl TryInto for CqlTimestamp { type Error = ValueOverflow; - fn try_into(self) -> Result { - time::OffsetDateTime::from_unix_timestamp_nanos(self.0 as i128 * 1_000_000) + fn try_into(self) -> Result { + time_03::OffsetDateTime::from_unix_timestamp_nanos(self.0 as i128 * 1_000_000) .map_err(|_| ValueOverflow) } } -#[cfg(feature = "time")] -impl From for CqlTime { - fn from(value: time::Time) -> Self { +#[cfg(feature = "time-03")] +impl From for CqlTime { + fn from(value: time_03::Time) -> Self { let (h, m, s, n) = value.as_hms_nano(); // no need for checked arithmetic as all these types are guaranteed to fit in i64 without overflow @@ -630,17 +632,17 @@ impl From for CqlTime { } } -#[cfg(feature = "time")] -impl TryInto for CqlTime { +#[cfg(feature = "time-03")] +impl TryInto for CqlTime { type Error = ValueOverflow; - fn try_into(self) -> Result { + fn try_into(self) -> Result { let h = self.0 / 3_600_000_000_000; let m = self.0 / 60_000_000_000 % 60; let s = self.0 / 1_000_000_000 % 60; let n = self.0 % 1_000_000_000; - time::Time::from_hms_nano( + time_03::Time::from_hms_nano( h.try_into().map_err(|_| ValueOverflow)?, m as u8, s as u8, @@ -1020,8 +1022,8 @@ impl Value for CqlDate { } } -#[cfg(feature = "time")] -impl Value for time::Date { +#[cfg(feature = "time-03")] +impl Value for time_03::Date { fn serialize(&self, buf: &mut Vec) -> Result<(), ValueTooBig> { CqlDate::from(*self).serialize(buf) } @@ -1050,8 +1052,8 @@ impl Value for DateTime { } } -#[cfg(feature = "time")] -impl Value for time::OffsetDateTime { +#[cfg(feature = "time-03")] +impl Value for time_03::OffsetDateTime { fn serialize(&self, buf: &mut Vec) -> Result<(), ValueTooBig> { CqlTimestamp::from(*self).serialize(buf) } @@ -1066,8 +1068,8 @@ impl Value for NaiveTime { } } -#[cfg(feature = "time")] -impl Value for time::Time { +#[cfg(feature = "time-03")] +impl Value for time_03::Time { fn serialize(&self, buf: &mut Vec) -> Result<(), ValueTooBig> { CqlTime::from(*self).serialize(buf) } diff --git a/scylla-cql/src/frame/value_tests.rs b/scylla-cql/src/frame/value_tests.rs index 52328ed73e..561d9571b9 100644 --- a/scylla-cql/src/frame/value_tests.rs +++ b/scylla-cql/src/frame/value_tests.rs @@ -335,11 +335,11 @@ fn naive_date_serialization() { assert_eq!((2_u32.pow(31) + 30).to_be_bytes(), [128, 0, 0, 30]); } -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] #[test] -fn date_serialization() { +fn date_03_serialization() { // 1970-01-31 is 2^31 - let unix_epoch = time::Date::from_ordinal_date(1970, 1).unwrap(); + let unix_epoch = time_03::Date::from_ordinal_date(1970, 1).unwrap(); assert_eq!( serialized(unix_epoch, ColumnType::Date), vec![0, 0, 0, 4, 128, 0, 0, 0] @@ -347,7 +347,8 @@ fn date_serialization() { assert_eq!(2_u32.pow(31).to_be_bytes(), [128, 0, 0, 0]); // 1969-12-02 is 2^31 - 30 - let before_epoch = time::Date::from_calendar_date(1969, time::Month::December, 2).unwrap(); + let before_epoch = + time_03::Date::from_calendar_date(1969, time_03::Month::December, 2).unwrap(); assert_eq!( serialized(before_epoch, ColumnType::Date), vec![0, 0, 0, 4, 127, 255, 255, 226] @@ -355,15 +356,16 @@ fn date_serialization() { assert_eq!((2_u32.pow(31) - 30).to_be_bytes(), [127, 255, 255, 226]); // 1970-01-31 is 2^31 + 30 - let after_epoch = time::Date::from_calendar_date(1970, time::Month::January, 31).unwrap(); + let after_epoch = time_03::Date::from_calendar_date(1970, time_03::Month::January, 31).unwrap(); assert_eq!( serialized(after_epoch, ColumnType::Date), vec![0, 0, 0, 4, 128, 0, 0, 30] ); assert_eq!((2_u32.pow(31) + 30).to_be_bytes(), [128, 0, 0, 30]); - // Min date represented by time::Date (without large-dates feature) - let long_before_epoch = time::Date::from_calendar_date(-9999, time::Month::January, 1).unwrap(); + // Min date represented by time_03::Date (without large-dates feature) + let long_before_epoch = + time_03::Date::from_calendar_date(-9999, time_03::Month::January, 1).unwrap(); let days_till_epoch = (unix_epoch - long_before_epoch).whole_days(); assert_eq!( (2_u32.pow(31) - days_till_epoch as u32).to_be_bytes(), @@ -374,8 +376,9 @@ fn date_serialization() { vec![0, 0, 0, 4, 127, 189, 75, 125] ); - // Max date represented by time::Date (without large-dates feature) - let long_after_epoch = time::Date::from_calendar_date(9999, time::Month::December, 31).unwrap(); + // Max date represented by time_03::Date (without large-dates feature) + let long_after_epoch = + time_03::Date::from_calendar_date(9999, time_03::Month::December, 31).unwrap(); let days_since_epoch = (long_after_epoch - unix_epoch).whole_days(); assert_eq!( (2_u32.pow(31) + days_since_epoch as u32).to_be_bytes(), @@ -446,20 +449,20 @@ fn naive_time_serialization() { ) } -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] #[test] -fn time_serialization() { +fn time_03_serialization() { let midnight_time: i64 = 0; let max_time: i64 = 24 * 60 * 60 * 1_000_000_000 - 1; let any_time: i64 = (3600 + 2 * 60 + 3) * 1_000_000_000 + 4; let test_cases = [ - (time::Time::MIDNIGHT, midnight_time.to_be_bytes()), + (time_03::Time::MIDNIGHT, midnight_time.to_be_bytes()), ( - time::Time::from_hms_nano(23, 59, 59, 999_999_999).unwrap(), + time_03::Time::from_hms_nano(23, 59, 59, 999_999_999).unwrap(), max_time.to_be_bytes(), ), ( - time::Time::from_hms_nano(1, 2, 3, 4).unwrap(), + time_03::Time::from_hms_nano(1, 2, 3, 4).unwrap(), any_time.to_be_bytes(), ), ]; @@ -541,14 +544,14 @@ fn date_time_serialization() { } } -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] #[test] -fn offset_date_time_serialization() { - use time::{Date, Month, OffsetDateTime, PrimitiveDateTime, Time}; +fn offset_date_time_03_serialization() { + use time_03::{Date, Month, OffsetDateTime, PrimitiveDateTime, Time}; let offset_max = - PrimitiveDateTime::MAX.assume_offset(time::UtcOffset::from_hms(-23, -59, -59).unwrap()); + PrimitiveDateTime::MAX.assume_offset(time_03::UtcOffset::from_hms(-23, -59, -59).unwrap()); let offset_min = - PrimitiveDateTime::MIN.assume_offset(time::UtcOffset::from_hms(23, 59, 59).unwrap()); + PrimitiveDateTime::MIN.assume_offset(time_03::UtcOffset::from_hms(23, 59, 59).unwrap()); let test_cases = [ ( // Max time serialized without error diff --git a/scylla-cql/src/types/deserialize/value.rs b/scylla-cql/src/types/deserialize/value.rs index 28124e7bc4..05bb66f6c6 100644 --- a/scylla-cql/src/types/deserialize/value.rs +++ b/scylla-cql/src/types/deserialize/value.rs @@ -421,7 +421,7 @@ impl_emptiable_strict_type!( } ); -#[cfg(any(feature = "chrono", feature = "time"))] +#[cfg(any(feature = "chrono", feature = "time-03"))] fn get_days_since_epoch_from_date_column( typ: &ColumnType, v: Option>, @@ -449,14 +449,14 @@ impl_emptiable_strict_type!( } ); -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] impl_emptiable_strict_type!( - time::Date, + time_03::Date, Date, |typ: &'frame ColumnType, v: Option>| { let days_since_epoch = - time::Duration::days(get_days_since_epoch_from_date_column::(typ, v)?); - time::Date::from_calendar_date(1970, time::Month::January, 1) + time_03::Duration::days(get_days_since_epoch_from_date_column::(typ, v)?); + time_03::Date::from_calendar_date(1970, time_03::Month::January, 1) .unwrap() .checked_add(days_since_epoch) .ok_or_else(|| { @@ -508,14 +508,14 @@ impl_emptiable_strict_type!( } ); -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] impl_emptiable_strict_type!( - time::Time, + time_03::Time, Time, |typ: &'frame ColumnType, v: Option>| { - let nanoseconds = get_nanos_from_time_column::(typ, v)?; + let nanoseconds = get_nanos_from_time_column::(typ, v)?; - let time: time::Time = CqlTime(nanoseconds).try_into().map_err(|_| { + let time: time_03::Time = CqlTime(nanoseconds).try_into().map_err(|_| { mk_deser_err::(typ, BuiltinDeserializationErrorKind::ValueOverflow) })?; Ok(time) @@ -560,13 +560,13 @@ impl_emptiable_strict_type!( } ); -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] impl_emptiable_strict_type!( - time::OffsetDateTime, + time_03::OffsetDateTime, Timestamp, |typ: &'frame ColumnType, v: Option>| { let millis = get_millis_from_timestamp_column::(typ, v)?; - time::OffsetDateTime::from_unix_timestamp_nanos(millis as i128 * 1_000_000) + time_03::OffsetDateTime::from_unix_timestamp_nanos(millis as i128 * 1_000_000) .map_err(|_| mk_deser_err::(typ, BuiltinDeserializationErrorKind::ValueOverflow)) } ); @@ -1979,10 +1979,10 @@ pub(super) mod tests { &mut Bytes::new(), ); - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] assert_ser_de_identity( &ColumnType::Date, - &time::Date::from_ordinal_date(1999, 99).unwrap(), + &time_03::Date::from_ordinal_date(1999, 99).unwrap(), &mut Bytes::new(), ); @@ -1996,10 +1996,10 @@ pub(super) mod tests { &mut Bytes::new(), ); - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] assert_ser_de_identity( &ColumnType::Time, - &time::Time::from_hms_micro(21, 37, 21, 37).unwrap(), + &time_03::Time::from_hms_micro(21, 37, 21, 37).unwrap(), &mut Bytes::new(), ); @@ -2017,10 +2017,10 @@ pub(super) mod tests { &mut Bytes::new(), ); - #[cfg(feature = "time")] + #[cfg(feature = "time-03")] assert_ser_de_identity( &ColumnType::Timestamp, - &time::OffsetDateTime::from_unix_timestamp(0xdead_cafe).unwrap(), + &time_03::OffsetDateTime::from_unix_timestamp(0xdead_cafe).unwrap(), &mut Bytes::new(), ); } diff --git a/scylla-cql/src/types/serialize/value.rs b/scylla-cql/src/types/serialize/value.rs index cdb75c7076..c589e0d09d 100644 --- a/scylla-cql/src/types/serialize/value.rs +++ b/scylla-cql/src/types/serialize/value.rs @@ -181,22 +181,22 @@ impl SerializeValue for NaiveTime { ::serialize(&cql_time, typ, writer)? }); } -#[cfg(feature = "time")] -impl SerializeValue for time::Date { +#[cfg(feature = "time-03")] +impl SerializeValue for time_03::Date { impl_serialize_via_writer!(|me, typ, writer| { exact_type_check!(typ, Date); ::serialize(&(*me).into(), typ, writer)? }); } -#[cfg(feature = "time")] -impl SerializeValue for time::OffsetDateTime { +#[cfg(feature = "time-03")] +impl SerializeValue for time_03::OffsetDateTime { impl_serialize_via_writer!(|me, typ, writer| { exact_type_check!(typ, Timestamp); ::serialize(&(*me).into(), typ, writer)? }); } -#[cfg(feature = "time")] -impl SerializeValue for time::Time { +#[cfg(feature = "time-03")] +impl SerializeValue for time_03::Time { impl_serialize_via_writer!(|me, typ, writer| { exact_type_check!(typ, Time); ::serialize(&(*me).into(), typ, writer)? diff --git a/scylla/Cargo.toml b/scylla/Cargo.toml index 2d39c91441..a49aff72d4 100644 --- a/scylla/Cargo.toml +++ b/scylla/Cargo.toml @@ -26,13 +26,13 @@ cloud = [ ] secrecy-08 = ["scylla-cql/secrecy-08"] chrono = ["scylla-cql/chrono"] -time = ["scylla-cql/time"] +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", - "time", + "time-03", "secrecy-08", "num-bigint-03", "num-bigint-04", diff --git a/scylla/src/transport/cql_types_test.rs b/scylla/src/transport/cql_types_test.rs index 748e4ec199..3e253f233e 100644 --- a/scylla/src/transport/cql_types_test.rs +++ b/scylla/src/transport/cql_types_test.rs @@ -448,9 +448,9 @@ async fn test_cql_date() { .unwrap_err(); } -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] #[tokio::test] -async fn test_date() { +async fn test_date_03() { setup_tracing(); use time::{Date, Month::*}; @@ -700,9 +700,9 @@ async fn test_naive_time() { .unwrap_err(); } -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] #[tokio::test] -async fn test_time() { +async fn test_time_03() { setup_tracing(); use time::Time; @@ -1003,9 +1003,9 @@ async fn test_date_time() { .unwrap_err(); } -#[cfg(feature = "time")] +#[cfg(feature = "time-03")] #[tokio::test] -async fn test_offset_date_time() { +async fn test_offset_date_time_03() { setup_tracing(); use time::{Date, Month::*, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset}; From fabfee75e84b0e6fb652d690ead52674fe586b53 Mon Sep 17 00:00:00 2001 From: muzarski Date: Thu, 25 Apr 2024 16:18:06 +0200 Subject: [PATCH 4/6] cqlvalue: make conversion to time_03 test utils Since some other version of time_03 might be supported in the future, we cannot have multiple methods such as `as_date()` defined. I don't think it's good idea to expose version specific methods (e.g. `as_date_03()`) to the users. This is why we make them test utilities only. --- scylla-cql/src/frame/response/result.rs | 36 +++++++++++++------------ 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/scylla-cql/src/frame/response/result.rs b/scylla-cql/src/frame/response/result.rs index e5815d9daa..e6a6712cab 100644 --- a/scylla-cql/src/frame/response/result.rs +++ b/scylla-cql/src/frame/response/result.rs @@ -192,8 +192,9 @@ impl CqlValue { self.as_cql_date().and_then(|date| date.try_into().ok()) } + #[cfg(test)] #[cfg(feature = "time-03")] - pub fn as_date(&self) -> Option { + fn as_date_03(&self) -> Option { self.as_cql_date().and_then(|date| date.try_into().ok()) } @@ -209,8 +210,9 @@ impl CqlValue { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } + #[cfg(test)] #[cfg(feature = "time-03")] - pub fn as_offset_date_time(&self) -> Option { + fn as_offset_date_time_03(&self) -> Option { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } @@ -226,8 +228,9 @@ impl CqlValue { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } + #[cfg(test)] #[cfg(feature = "time-03")] - pub fn as_time(&self) -> Option { + fn as_time_03(&self) -> Option { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } @@ -1330,7 +1333,7 @@ mod tests { super::deser_cql_value(&ColumnType::Date, &mut (1u32 << 31).to_be_bytes().as_ref()) .unwrap(); - assert_eq!(date.as_date(), Some(unix_epoch)); + assert_eq!(date.as_date_03(), Some(unix_epoch)); // 2^31 - 30 when converted to time_03::Date is 1969-12-02 let before_epoch = Date::from_calendar_date(1969, December, 2).unwrap(); @@ -1340,7 +1343,7 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_date(), Some(before_epoch)); + assert_eq!(date.as_date_03(), Some(before_epoch)); // 2^31 + 30 when converted to time_03::Date is 1970-01-31 let after_epoch = Date::from_calendar_date(1970, January, 31).unwrap(); @@ -1350,20 +1353,20 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_date(), Some(after_epoch)); + assert_eq!(date.as_date_03(), Some(after_epoch)); // 0 and u32::MAX are out of NaiveDate range, fails with an error, not panics assert_eq!( super::deser_cql_value(&ColumnType::Date, &mut 0_u32.to_be_bytes().as_ref()) .unwrap() - .as_date(), + .as_date_03(), None ); assert_eq!( super::deser_cql_value(&ColumnType::Date, &mut u32::MAX.to_be_bytes().as_ref()) .unwrap() - .as_date(), + .as_date_03(), None ); } @@ -1441,8 +1444,7 @@ mod tests { let time = super::deser_cql_value(&ColumnType::Time, &mut (0i64).to_be_bytes().as_ref()).unwrap(); - dbg!(&time); - assert_eq!(time.as_time(), Some(midnight)); + assert_eq!(time.as_time_03(), Some(midnight)); // 10:10:30.500,000,001 let (h, m, s, n) = (10, 10, 30, 500_000_001); @@ -1455,7 +1457,7 @@ mod tests { ) .unwrap(); - assert_eq!(time.as_time(), Some(midnight)); + assert_eq!(time.as_time_03(), Some(midnight)); // 23:59:59.999,999,999 let (h, m, s, n) = (23, 59, 59, 999_999_999); @@ -1468,7 +1470,7 @@ mod tests { ) .unwrap(); - assert_eq!(time.as_time(), Some(midnight)); + assert_eq!(time.as_time_03(), Some(midnight)); } #[test] @@ -1552,7 +1554,7 @@ mod tests { let date = super::deser_cql_value(&ColumnType::Timestamp, &mut 0i64.to_be_bytes().as_ref()) .unwrap(); - assert_eq!(date.as_offset_date_time(), Some(unix_epoch)); + assert_eq!(date.as_offset_date_time_03(), Some(unix_epoch)); // When converted to NaiveDateTime, this is 1969-12-01 11:29:29.5 let timestamp: i64 = -((((30 * 24 + 12) * 60 + 30) * 60 + 30) * 1000 + 500); @@ -1567,7 +1569,7 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_offset_date_time(), Some(before_epoch)); + assert_eq!(date.as_offset_date_time_03(), Some(before_epoch)); // when converted to NaiveDateTime, this is is 1970-01-31 12:30:30.5 let timestamp: i64 = (((30 * 24 + 12) * 60 + 30) * 60 + 30) * 1000 + 500; @@ -1582,20 +1584,20 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_offset_date_time(), Some(after_epoch)); + assert_eq!(date.as_offset_date_time_03(), Some(after_epoch)); // 0 and u32::MAX are out of NaiveDate range, fails with an error, not panics assert_eq!( super::deser_cql_value(&ColumnType::Timestamp, &mut i64::MIN.to_be_bytes().as_ref()) .unwrap() - .as_offset_date_time(), + .as_offset_date_time_03(), None ); assert_eq!( super::deser_cql_value(&ColumnType::Timestamp, &mut i64::MAX.to_be_bytes().as_ref()) .unwrap() - .as_offset_date_time(), + .as_offset_date_time_03(), None ); } From c737490defaec39aeea1b183a9b882f4b785c67c Mon Sep 17 00:00:00 2001 From: muzarski Date: Thu, 22 Feb 2024 16:33:38 +0100 Subject: [PATCH 5/6] cargo: rename `chrono` feature to `chrono-04` --- .github/workflows/rust.yml | 4 +- examples/Cargo.toml | 2 +- scylla-cql/Cargo.toml | 6 +- scylla-cql/src/frame/response/cql_to_rust.rs | 35 ++++----- scylla-cql/src/frame/response/result.rs | 33 ++++----- scylla-cql/src/frame/value.rs | 67 +++++++++-------- scylla-cql/src/frame/value_tests.rs | 18 ++--- scylla-cql/src/types/deserialize/value.rs | 77 ++++++++++---------- scylla-cql/src/types/serialize/value.rs | 17 ++--- scylla/Cargo.toml | 4 +- scylla/src/transport/cql_types_test.rs | 12 +-- 11 files changed, 132 insertions(+), 143 deletions(-) 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/deserialize/value.rs b/scylla-cql/src/types/deserialize/value.rs index 05bb66f6c6..f87aed66e6 100644 --- a/scylla-cql/src/types/deserialize/value.rs +++ b/scylla-cql/src/types/deserialize/value.rs @@ -421,7 +421,7 @@ impl_emptiable_strict_type!( } ); -#[cfg(any(feature = "chrono", feature = "time-03"))] +#[cfg(any(feature = "chrono-04", feature = "time-03"))] fn get_days_since_epoch_from_date_column( typ: &ColumnType, v: Option>, @@ -433,21 +433,20 @@ fn get_days_since_epoch_from_date_column( Ok(days_since_epoch) } -#[cfg(feature = "chrono")] -impl_emptiable_strict_type!( - chrono::NaiveDate, - Date, - |typ: &'frame ColumnType, v: Option>| { - let fail = || mk_deser_err::(typ, BuiltinDeserializationErrorKind::ValueOverflow); - let days_since_epoch = - chrono::Duration::try_days(get_days_since_epoch_from_date_column::(typ, v)?) - .ok_or_else(fail)?; - chrono::NaiveDate::from_ymd_opt(1970, 1, 1) - .unwrap() - .checked_add_signed(days_since_epoch) - .ok_or_else(fail) - } -); +#[cfg(feature = "chrono-04")] +impl_emptiable_strict_type!(chrono_04::NaiveDate, Date, |typ: &'frame ColumnType, + v: Option< + FrameSlice<'frame>, +>| { + let fail = || mk_deser_err::(typ, BuiltinDeserializationErrorKind::ValueOverflow); + let days_since_epoch = + chrono_04::Duration::try_days(get_days_since_epoch_from_date_column::(typ, v)?) + .ok_or_else(fail)?; + chrono_04::NaiveDate::from_ymd_opt(1970, 1, 1) + .unwrap() + .checked_add_signed(days_since_epoch) + .ok_or_else(fail) +}); #[cfg(feature = "time-03")] impl_emptiable_strict_type!( @@ -494,19 +493,18 @@ impl_emptiable_strict_type!( } ); -#[cfg(feature = "chrono")] -impl_emptiable_strict_type!( - chrono::NaiveTime, - Time, - |typ: &'frame ColumnType, v: Option>| { - let nanoseconds = get_nanos_from_time_column::(typ, v)?; +#[cfg(feature = "chrono-04")] +impl_emptiable_strict_type!(chrono_04::NaiveTime, Time, |typ: &'frame ColumnType, + v: Option< + FrameSlice<'frame>, +>| { + let nanoseconds = get_nanos_from_time_column::(typ, v)?; - let naive_time: chrono::NaiveTime = CqlTime(nanoseconds).try_into().map_err(|_| { - mk_deser_err::(typ, BuiltinDeserializationErrorKind::ValueOverflow) - })?; - Ok(naive_time) - } -); + let naive_time: chrono_04::NaiveTime = CqlTime(nanoseconds) + .try_into() + .map_err(|_| mk_deser_err::(typ, BuiltinDeserializationErrorKind::ValueOverflow))?; + Ok(naive_time) +}); #[cfg(feature = "time-03")] impl_emptiable_strict_type!( @@ -542,16 +540,16 @@ impl_emptiable_strict_type!( } ); -#[cfg(feature = "chrono")] +#[cfg(feature = "chrono-04")] impl_emptiable_strict_type!( - chrono::DateTime, + chrono_04::DateTime, Timestamp, |typ: &'frame ColumnType, v: Option>| { - use chrono::TimeZone as _; + use chrono_04::TimeZone as _; let millis = get_millis_from_timestamp_column::(typ, v)?; - match chrono::Utc.timestamp_millis_opt(millis) { - chrono::LocalResult::Single(datetime) => Ok(datetime), + match chrono_04::Utc.timestamp_millis_opt(millis) { + chrono_04::LocalResult::Single(datetime) => Ok(datetime), _ => Err(mk_deser_err::( typ, BuiltinDeserializationErrorKind::ValueOverflow, @@ -1972,10 +1970,10 @@ pub(super) mod tests { // date assert_ser_de_identity(&ColumnType::Date, &CqlDate(0xbeaf), &mut Bytes::new()); - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] assert_ser_de_identity( &ColumnType::Date, - &chrono::NaiveDate::from_yo_opt(1999, 99).unwrap(), + &chrono_04::NaiveDate::from_yo_opt(1999, 99).unwrap(), &mut Bytes::new(), ); @@ -1989,10 +1987,10 @@ pub(super) mod tests { // time assert_ser_de_identity(&ColumnType::Time, &CqlTime(0xdeed), &mut Bytes::new()); - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] assert_ser_de_identity( &ColumnType::Time, - &chrono::NaiveTime::from_hms_micro_opt(21, 37, 21, 37).unwrap(), + &chrono_04::NaiveTime::from_hms_micro_opt(21, 37, 21, 37).unwrap(), &mut Bytes::new(), ); @@ -2010,10 +2008,11 @@ pub(super) mod tests { &mut Bytes::new(), ); - #[cfg(feature = "chrono")] + #[cfg(feature = "chrono-04")] assert_ser_de_identity( &ColumnType::Timestamp, - &chrono::DateTime::::from_timestamp_millis(0xdead_cafe_deaf).unwrap(), + &chrono_04::DateTime::::from_timestamp_millis(0xdead_cafe_deaf) + .unwrap(), &mut Bytes::new(), ); 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}; From cc705ad18b7aeb8102bdffe2c9f70d468df9d896 Mon Sep 17 00:00:00 2001 From: muzarski Date: Wed, 24 Apr 2024 17:39:52 +0200 Subject: [PATCH 6/6] cqlvalue: make chrono conversions test utility Made `CqlValue`-to-chrono types conversion methods a private utility methods used only for tests. --- scylla-cql/src/frame/response/result.rs | 35 ++++++++++++++----------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/scylla-cql/src/frame/response/result.rs b/scylla-cql/src/frame/response/result.rs index 35b43b5bb3..3afe7335d3 100644 --- a/scylla-cql/src/frame/response/result.rs +++ b/scylla-cql/src/frame/response/result.rs @@ -184,8 +184,9 @@ impl CqlValue { } } + #[cfg(test)] #[cfg(feature = "chrono-04")] - pub fn as_naive_date(&self) -> Option { + fn as_naive_date_04(&self) -> Option { self.as_cql_date().and_then(|date| date.try_into().ok()) } @@ -202,8 +203,9 @@ impl CqlValue { } } + #[cfg(test)] #[cfg(feature = "chrono-04")] - pub fn as_datetime(&self) -> Option> { + fn as_datetime_04(&self) -> Option> { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } @@ -220,8 +222,9 @@ impl CqlValue { } } + #[cfg(test)] #[cfg(feature = "chrono-04")] - pub fn as_naive_time(&self) -> Option { + fn as_naive_time_04(&self) -> Option { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } @@ -1280,7 +1283,7 @@ mod tests { super::deser_cql_value(&ColumnType::Date, &mut (1u32 << 31).to_be_bytes().as_ref()) .unwrap(); - assert_eq!(date.as_naive_date(), Some(unix_epoch)); + assert_eq!(date.as_naive_date_04(), Some(unix_epoch)); // 2^31 - 30 when converted to NaiveDate is 1969-12-02 let before_epoch = NaiveDate::from_ymd_opt(1969, 12, 2).unwrap(); @@ -1290,7 +1293,7 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_naive_date(), Some(before_epoch)); + assert_eq!(date.as_naive_date_04(), Some(before_epoch)); // 2^31 + 30 when converted to NaiveDate is 1970-01-31 let after_epoch = NaiveDate::from_ymd_opt(1970, 1, 31).unwrap(); @@ -1300,20 +1303,20 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_naive_date(), Some(after_epoch)); + assert_eq!(date.as_naive_date_04(), Some(after_epoch)); // 0 and u32::MAX are out of NaiveDate range, fails with an error, not panics assert_eq!( super::deser_cql_value(&ColumnType::Date, &mut 0_u32.to_be_bytes().as_ref()) .unwrap() - .as_naive_date(), + .as_naive_date_04(), None ); assert_eq!( super::deser_cql_value(&ColumnType::Date, &mut u32::MAX.to_be_bytes().as_ref()) .unwrap() - .as_naive_date(), + .as_naive_date_04(), None ); } @@ -1402,7 +1405,7 @@ mod tests { let time = super::deser_cql_value(&ColumnType::Time, &mut (0i64).to_be_bytes().as_ref()).unwrap(); - assert_eq!(time.as_naive_time(), Some(midnight)); + assert_eq!(time.as_naive_time_04(), Some(midnight)); // 10:10:30.500,000,001 let (h, m, s, n) = (10, 10, 30, 500_000_001); @@ -1415,7 +1418,7 @@ mod tests { ) .unwrap(); - assert_eq!(time.as_naive_time(), Some(midnight)); + assert_eq!(time.as_naive_time_04(), Some(midnight)); // 23:59:59.999,999,999 let (h, m, s, n) = (23, 59, 59, 999_999_999); @@ -1428,7 +1431,7 @@ mod tests { ) .unwrap(); - assert_eq!(time.as_naive_time(), Some(midnight)); + assert_eq!(time.as_naive_time_04(), Some(midnight)); } #[cfg(feature = "time-03")] @@ -1493,7 +1496,7 @@ mod tests { let date = super::deser_cql_value(&ColumnType::Timestamp, &mut 0i64.to_be_bytes().as_ref()) .unwrap(); - assert_eq!(date.as_datetime(), Some(unix_epoch)); + assert_eq!(date.as_datetime_04(), Some(unix_epoch)); // When converted to NaiveDateTime, this is 1969-12-01 11:29:29.5 let timestamp: i64 = -((((30 * 24 + 12) * 60 + 30) * 60 + 30) * 1000 + 500); @@ -1508,7 +1511,7 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_datetime(), Some(before_epoch)); + assert_eq!(date.as_datetime_04(), Some(before_epoch)); // when converted to NaiveDateTime, this is is 1970-01-31 12:30:30.5 let timestamp: i64 = (((30 * 24 + 12) * 60 + 30) * 60 + 30) * 1000 + 500; @@ -1523,20 +1526,20 @@ mod tests { ) .unwrap(); - assert_eq!(date.as_datetime(), Some(after_epoch)); + assert_eq!(date.as_datetime_04(), Some(after_epoch)); // 0 and u32::MAX are out of NaiveDate range, fails with an error, not panics assert_eq!( super::deser_cql_value(&ColumnType::Timestamp, &mut i64::MIN.to_be_bytes().as_ref()) .unwrap() - .as_datetime(), + .as_datetime_04(), None ); assert_eq!( super::deser_cql_value(&ColumnType::Timestamp, &mut i64::MAX.to_be_bytes().as_ref()) .unwrap() - .as_datetime(), + .as_datetime_04(), None ); }