Skip to content

Commit

Permalink
cargo: rename time feature to time-03.
Browse files Browse the repository at this point in the history
  • Loading branch information
muzarski committed Apr 24, 2024
1 parent f5f7b5c commit 751a6c7
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- 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"
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
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ futures = "0.3.6"
openssl = "0.10.32"
rustyline = "9"
rustyline-derive = "0.6"
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time", "num-bigint-03", "num-bigint-04", "bigdecimal-04"]}
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time-03", "num-bigint-03", "num-bigint-04", "bigdecimal-04"]}
tokio = {version = "1.1.0", features = ["full"]}
tracing = "0.1.25"
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
Expand Down
8 changes: 4 additions & 4 deletions scylla-cql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ 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]
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"] }

[[bench]]
name = "benchmark"
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", "secrecy-08", "num-bigint-03", "num-bigint-04", "bigdecimal-04"]
full-serialization = ["chrono", "time-03", "secrecy-08", "num-bigint-03", "num-bigint-04", "bigdecimal-04"]

[lints.rust]
unreachable_pub = "warn"
95 changes: 52 additions & 43 deletions scylla-cql/src/frame/response/cql_to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ impl FromCqlVal<CqlValue> for NaiveDate {
}
}

#[cfg(feature = "time")]
impl FromCqlVal<CqlValue> for time::Date {
#[cfg(feature = "time-03")]
impl FromCqlVal<CqlValue> for time_03::Date {
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 @@ -208,8 +208,8 @@ impl FromCqlVal<CqlValue> for NaiveTime {
}
}

#[cfg(feature = "time")]
impl FromCqlVal<CqlValue> for time::Time {
#[cfg(feature = "time-03")]
impl FromCqlVal<CqlValue> for time_03::Time {
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 @@ -229,8 +229,8 @@ impl FromCqlVal<CqlValue> for DateTime<Utc> {
}
}

#[cfg(feature = "time")]
impl FromCqlVal<CqlValue> for time::OffsetDateTime {
#[cfg(feature = "time-03")]
impl FromCqlVal<CqlValue> for time_03::OffsetDateTime {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
cql_val
.as_cql_timestamp()
Expand Down Expand Up @@ -568,43 +568,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() {
// 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
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)
);
}

Expand Down Expand Up @@ -669,48 +675,51 @@ mod tests {
);
}

#[cfg(feature = "time")]
#[cfg(feature = "time-03")]
#[test]
fn time_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
let late_night = CqlValue::Time(CqlTime(
(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
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)
);
}

Expand Down Expand Up @@ -744,55 +753,55 @@ mod tests {
);
}

#[cfg(feature = "time")]
#[cfg(feature = "time-03")]
#[test]
fn offset_datetime_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
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)
);
}

Expand Down
32 changes: 16 additions & 16 deletions scylla-cql/src/frame/response/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ impl CqlValue {
self.as_cql_date().and_then(|date| date.try_into().ok())
}

#[cfg(feature = "time")]
pub fn as_date(&self) -> Option<time::Date> {
#[cfg(feature = "time-03")]
pub fn as_date(&self) -> Option<time_03::Date> {
self.as_cql_date().and_then(|date| date.try_into().ok())
}

Expand All @@ -178,8 +178,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<time::OffsetDateTime> {
#[cfg(feature = "time-03")]
pub fn as_offset_date_time(&self) -> Option<time_03::OffsetDateTime> {
self.as_cql_timestamp().and_then(|ts| ts.try_into().ok())
}

Expand All @@ -195,8 +195,8 @@ impl CqlValue {
self.as_cql_time().and_then(|ts| ts.try_into().ok())
}

#[cfg(feature = "time")]
pub fn as_time(&self) -> Option<time::Time> {
#[cfg(feature = "time-03")]
pub fn as_time(&self) -> Option<time_03::Time> {
self.as_cql_time().and_then(|ts| ts.try_into().ok())
}

Expand Down Expand Up @@ -1411,21 +1411,21 @@ mod tests {
);
}

#[cfg(feature = "time")]
#[cfg(feature = "time-03")]
#[test]
fn test_date_from_cql() {
use time::Date;
use time::Month::*;
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));

// 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,
Expand All @@ -1435,7 +1435,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,
Expand Down Expand Up @@ -1524,10 +1524,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;
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();
Expand Down Expand Up @@ -1635,10 +1635,10 @@ mod tests {
);
}

#[cfg(feature = "time")]
#[cfg(feature = "time-03")]
#[test]
fn test_offset_datetime_from_cql() {
use time::{Date, Month::*, OffsetDateTime, PrimitiveDateTime, Time};
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();
Expand Down
Loading

0 comments on commit 751a6c7

Please sign in to comment.