diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index bae8c8b8b..c211c859b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -44,12 +44,12 @@ 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 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 secrecy-08 feature + run: cargo check --all-targets --manifest-path "scylla/Cargo.toml" --features "secrecy-08" + - 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 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/Cargo.lock.msrv b/Cargo.lock.msrv index 4ddbe9dae..3168c5fcb 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/examples/Cargo.toml b/examples/Cargo.toml index d71e41367..bd44695aa 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -13,8 +13,8 @@ rustyline-derive = "0.6" scylla = { path = "../scylla", features = [ "ssl", "cloud", - "chrono", - "time", + "chrono-04", + "time-03", "num-bigint-03", "num-bigint-04", "bigdecimal-04", diff --git a/scylla-cql/Cargo.toml b/scylla-cql/Cargo.toml index 547404cae..2dae50d09 100644 --- a/scylla-cql/Cargo.toml +++ b/scylla-cql/Cargo.toml @@ -15,24 +15,24 @@ 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-08 = { package = "secrecy", version = "0.8", optional = true } snap = "1.0" uuid = "1.0" 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 } -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]] @@ -40,16 +40,16 @@ name = "benchmark" harness = false [features] -secret = ["secrecy"] -time = ["dep:time"] -chrono = ["dep:chrono"] +secrecy-08 = ["dep:secrecy-08"] +time-03 = ["dep:time-03"] +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", - "time", - "secret", + "chrono-04", + "time-03", + "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 c55dd3ee8..435d45efc 100644 --- a/scylla-cql/src/frame/response/cql_to_rust.rs +++ b/scylla-cql/src/frame/response/cql_to_rust.rs @@ -8,12 +8,6 @@ use std::net::IpAddr; use thiserror::Error; 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}")] @@ -178,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), @@ -188,8 +182,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), @@ -198,8 +192,8 @@ impl FromCqlVal for time::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), @@ -208,8 +202,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), @@ -218,8 +212,8 @@ impl FromCqlVal for time::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() @@ -229,8 +223,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() @@ -240,10 +234,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)?)) } } @@ -526,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!( @@ -568,43 +562,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) ); } @@ -628,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)); @@ -655,13 +655,13 @@ 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)); 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), @@ -669,20 +669,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 @@ -690,27 +693,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) ); } @@ -725,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) @@ -744,55 +747,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 f961f4b99..3afe7335d 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,13 +184,15 @@ impl CqlValue { } } - #[cfg(feature = "chrono")] - pub fn as_naive_date(&self) -> Option { + #[cfg(test)] + #[cfg(feature = "chrono-04")] + fn as_naive_date_04(&self) -> Option { self.as_cql_date().and_then(|date| date.try_into().ok()) } - #[cfg(feature = "time")] - pub fn as_date(&self) -> Option { + #[cfg(test)] + #[cfg(feature = "time-03")] + fn as_date_03(&self) -> Option { self.as_cql_date().and_then(|date| date.try_into().ok()) } @@ -204,13 +203,15 @@ impl CqlValue { } } - #[cfg(feature = "chrono")] - pub fn as_datetime(&self) -> Option> { + #[cfg(test)] + #[cfg(feature = "chrono-04")] + fn as_datetime_04(&self) -> Option> { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } - #[cfg(feature = "time")] - pub fn as_offset_date_time(&self) -> Option { + #[cfg(test)] + #[cfg(feature = "time-03")] + fn as_offset_date_time_03(&self) -> Option { self.as_cql_timestamp().and_then(|ts| ts.try_into().ok()) } @@ -221,13 +222,15 @@ impl CqlValue { } } - #[cfg(feature = "chrono")] - pub fn as_naive_time(&self) -> Option { + #[cfg(test)] + #[cfg(feature = "chrono-04")] + fn as_naive_time_04(&self) -> Option { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } - #[cfg(feature = "time")] - pub fn as_time(&self) -> Option { + #[cfg(test)] + #[cfg(feature = "time-03")] + fn as_time_03(&self) -> Option { self.as_cql_time().and_then(|ts| ts.try_into().ok()) } @@ -1269,10 +1272,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(); @@ -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,39 +1303,39 @@ 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 ); } - #[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()) .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::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, @@ -1340,9 +1343,9 @@ 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::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, @@ -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 ); } @@ -1392,17 +1395,17 @@ 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(); 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,21 +1431,20 @@ mod tests { ) .unwrap(); - assert_eq!(time.as_naive_time(), Some(midnight)); + assert_eq!(time.as_naive_time_04(), 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(); 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] @@ -1484,17 +1486,17 @@ 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(); 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); @@ -1509,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; @@ -1524,35 +1526,35 @@ 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 ); } - #[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(); 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 ); } diff --git a/scylla-cql/src/frame/value.rs b/scylla-cql/src/frame/value.rs index 6728675ef..a9c368d19 100644 --- a/scylla-cql/src/frame/value.rs +++ b/scylla-cql/src/frame/value.rs @@ -9,16 +9,10 @@ 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; -#[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 { @@ -467,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 @@ -480,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(); @@ -536,32 +532,34 @@ 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) } } -#[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; @@ -569,29 +567,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 @@ -599,7 +597,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 @@ -611,19 +609,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 @@ -633,17 +631,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, @@ -1008,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) } @@ -1023,8 +1021,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) } @@ -1046,22 +1044,22 @@ 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) } } -#[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) } } -#[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)? @@ -1069,16 +1067,17 @@ 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) } } -#[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 7af026d12..d060fde0b 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!( @@ -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(), @@ -409,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; @@ -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(), ), ]; @@ -489,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 @@ -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 @@ -872,11 +875,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 95103998e..f87aed66e 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-04", feature = "time-03"))] fn get_days_since_epoch_from_date_column( typ: &ColumnType, v: Option>, @@ -433,30 +433,29 @@ 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")] +#[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(|| { @@ -494,28 +493,27 @@ 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")] +#[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) @@ -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, @@ -560,13 +558,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)) } ); @@ -616,10 +614,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 +627,7 @@ where typ: &'frame ColumnType, v: Option>, ) -> Result { - >::deserialize(typ, v).map(secrecy::Secret::new) + >::deserialize(typ, v).map(secrecy_08::Secret::new) } } @@ -1972,34 +1970,34 @@ 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(), ); - #[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(), ); // 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(), ); - #[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(), ); @@ -2010,17 +2008,18 @@ 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(), ); - #[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 5ef6c8c6a..2a7040b78 100644 --- a/scylla-cql/src/types/serialize/value.rs +++ b/scylla-cql/src/types/serialize/value.rs @@ -9,12 +9,6 @@ use std::sync::Arc; use thiserror::Error; 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::{ @@ -22,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; @@ -160,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| { @@ -184,34 +178,35 @@ 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)? }); } -#[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 490a64c80..3096ff7d6 100644 --- a/scylla/Cargo.toml +++ b/scylla/Cargo.toml @@ -24,16 +24,16 @@ cloud = [ "dep:url", "dep:base64", ] -secret = ["scylla-cql/secret"] -chrono = ["scylla-cql/chrono"] -time = ["scylla-cql/time"] +secrecy-08 = ["scylla-cql/secrecy-08"] +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", - "time", - "secret", + "chrono-04", + "time-03", + "secrecy-08", "num-bigint-03", "num-bigint-04", "bigdecimal-04", diff --git a/scylla/src/transport/cql_types_test.rs b/scylla/src/transport/cql_types_test.rs index 748e4ec19..1beea8b3f 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; @@ -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::*}; @@ -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; @@ -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; @@ -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}; @@ -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};