Skip to content

Commit

Permalink
cargo: rename chrono feature to chrono-04
Browse files Browse the repository at this point in the history
  • Loading branch information
muzarski committed Jun 13, 2024
1 parent 61909c5 commit 31efc27
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 104 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions scylla-cql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
Expand Down
35 changes: 16 additions & 19 deletions scylla-cql/src/frame/response/cql_to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down Expand Up @@ -175,8 +172,8 @@ impl FromCqlVal<CqlValue> for bigdecimal_04::BigDecimal {
}
}

#[cfg(feature = "chrono")]
impl FromCqlVal<CqlValue> for NaiveDate {
#[cfg(feature = "chrono-04")]
impl FromCqlVal<CqlValue> for chrono_04::NaiveDate {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
match cql_val {
CqlValue::Date(cql_date) => cql_date.try_into().map_err(|_| FromCqlValError::BadVal),
Expand All @@ -195,8 +192,8 @@ impl FromCqlVal<CqlValue> for time_03::Date {
}
}

#[cfg(feature = "chrono")]
impl FromCqlVal<CqlValue> for NaiveTime {
#[cfg(feature = "chrono-04")]
impl FromCqlVal<CqlValue> for chrono_04::NaiveTime {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
match cql_val {
CqlValue::Time(cql_time) => cql_time.try_into().map_err(|_| FromCqlValError::BadVal),
Expand All @@ -215,8 +212,8 @@ impl FromCqlVal<CqlValue> for time_03::Time {
}
}

#[cfg(feature = "chrono")]
impl FromCqlVal<CqlValue> for DateTime<Utc> {
#[cfg(feature = "chrono-04")]
impl FromCqlVal<CqlValue> for chrono_04::DateTime<chrono_04::Utc> {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
cql_val
.as_cql_timestamp()
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 15 additions & 18 deletions scylla-cql/src/frame/response/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -187,8 +184,8 @@ impl CqlValue {
}
}

#[cfg(feature = "chrono")]
pub fn as_naive_date(&self) -> Option<NaiveDate> {
#[cfg(feature = "chrono-04")]
pub fn as_naive_date(&self) -> Option<chrono_04::NaiveDate> {
self.as_cql_date().and_then(|date| date.try_into().ok())
}

Expand All @@ -205,8 +202,8 @@ impl CqlValue {
}
}

#[cfg(feature = "chrono")]
pub fn as_datetime(&self) -> Option<DateTime<Utc>> {
#[cfg(feature = "chrono-04")]
pub fn as_datetime(&self) -> Option<chrono_04::DateTime<chrono_04::Utc>> {
self.as_cql_timestamp().and_then(|ts| ts.try_into().ok())
}

Expand All @@ -223,8 +220,8 @@ impl CqlValue {
}
}

#[cfg(feature = "chrono")]
pub fn as_naive_time(&self) -> Option<chrono::NaiveTime> {
#[cfg(feature = "chrono-04")]
pub fn as_naive_time(&self) -> Option<chrono_04::NaiveTime> {
self.as_cql_time().and_then(|ts| ts.try_into().ok())
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
67 changes: 33 additions & 34 deletions scylla-cql/src/frame/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<NaiveDate> for CqlDate {
fn from(value: NaiveDate) -> Self {
let unix_epoch = NaiveDate::from_yo_opt(1970, 1).unwrap();
#[cfg(feature = "chrono-04")]
impl From<chrono_04::NaiveDate> 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
Expand All @@ -477,50 +474,52 @@ impl From<NaiveDate> for CqlDate {
}
}

#[cfg(feature = "chrono")]
impl TryInto<NaiveDate> for CqlDate {
#[cfg(feature = "chrono-04")]
impl TryInto<chrono_04::NaiveDate> for CqlDate {
type Error = ValueOverflow;

fn try_into(self) -> Result<NaiveDate, Self::Error> {
fn try_into(self) -> Result<chrono_04::NaiveDate, Self::Error> {
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<DateTime<Utc>> for CqlTimestamp {
fn from(value: DateTime<Utc>) -> Self {
#[cfg(feature = "chrono-04")]
impl From<chrono_04::DateTime<chrono_04::Utc>> for CqlTimestamp {
fn from(value: chrono_04::DateTime<chrono_04::Utc>) -> Self {
Self(value.timestamp_millis())
}
}

#[cfg(feature = "chrono")]
impl TryInto<DateTime<Utc>> for CqlTimestamp {
#[cfg(feature = "chrono-04")]
impl TryInto<chrono_04::DateTime<chrono_04::Utc>> for CqlTimestamp {
type Error = ValueOverflow;

fn try_into(self) -> Result<DateTime<Utc>, Self::Error> {
match Utc.timestamp_millis_opt(self.0) {
chrono::LocalResult::Single(datetime) => Ok(datetime),
fn try_into(self) -> Result<chrono_04::DateTime<chrono_04::Utc>, 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<NaiveTime> for CqlTime {
#[cfg(feature = "chrono-04")]
impl TryFrom<chrono_04::NaiveTime> for CqlTime {
type Error = ValueOverflow;

fn try_from(value: NaiveTime) -> Result<Self, Self::Error> {
fn try_from(value: chrono_04::NaiveTime) -> Result<Self, Self::Error> {
let nanos = value
.signed_duration_since(chrono::NaiveTime::MIN)
.signed_duration_since(chrono_04::NaiveTime::MIN)
.num_nanoseconds()
.unwrap();

Expand All @@ -533,18 +532,18 @@ impl TryFrom<NaiveTime> for CqlTime {
}
}

#[cfg(feature = "chrono")]
impl TryInto<NaiveTime> for CqlTime {
#[cfg(feature = "chrono-04")]
impl TryInto<chrono_04::NaiveTime> for CqlTime {
type Error = ValueOverflow;

fn try_into(self) -> Result<NaiveTime, Self::Error> {
fn try_into(self) -> Result<chrono_04::NaiveTime, Self::Error> {
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)
}
}

Expand Down Expand Up @@ -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<u8>) -> Result<(), ValueTooBig> {
CqlDate::from(*self).serialize(buf)
}
Expand Down Expand Up @@ -1045,8 +1044,8 @@ impl Value for CqlTime {
}
}

#[cfg(feature = "chrono")]
impl Value for DateTime<Utc> {
#[cfg(feature = "chrono-04")]
impl Value for chrono_04::DateTime<chrono_04::Utc> {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
CqlTimestamp::from(*self).serialize(buf)
}
Expand All @@ -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<u8>) -> Result<(), ValueTooBig> {
CqlTime::try_from(*self)
.map_err(|_| ValueTooBig)?
Expand Down
Loading

0 comments on commit 31efc27

Please sign in to comment.