Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement FromCqlVal and SerializeValue for PrimitiveDatetime #1115

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions scylla-cql/src/frame/response/cql_to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ impl FromCqlVal<CqlValue> for time_03::OffsetDateTime {
}
}

#[cfg(feature = "time-03")]
impl FromCqlVal<CqlValue> for time_03::PrimitiveDateTime {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
let datetime = time_03::OffsetDateTime::from_cql(cql_val)?;

Ok(Self::new(datetime.date(), datetime.time()))
}
}

#[cfg(feature = "secrecy-08")]
impl<V: FromCqlVal<CqlValue> + secrecy_08::Zeroize> FromCqlVal<CqlValue> for secrecy_08::Secret<V> {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
Expand Down
7 changes: 7 additions & 0 deletions scylla-cql/src/frame/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,13 @@ impl From<time_03::OffsetDateTime> for CqlTimestamp {
}
}

#[cfg(feature = "time-03")]
impl From<time_03::PrimitiveDateTime> for CqlTimestamp {
fn from(value: time_03::PrimitiveDateTime) -> Self {
value.assume_utc().into()
}
}

#[cfg(feature = "time-03")]
impl TryInto<time_03::OffsetDateTime> for CqlTimestamp {
type Error = ValueOverflow;
Expand Down
10 changes: 10 additions & 0 deletions scylla-cql/src/types/deserialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@ impl_emptiable_strict_type!(
}
);

#[cfg(feature = "time-03")]
impl_emptiable_strict_type!(
time_03::PrimitiveDateTime,
Timestamp,
|typ: &'metadata ColumnType<'metadata>, v: Option<FrameSlice<'frame>>| {
let time = time_03::OffsetDateTime::deserialize(typ, v)?;
Ok(Self::new(time.date(), time.time()))
}
);

// inet

impl_emptiable_strict_type!(
Expand Down
14 changes: 14 additions & 0 deletions scylla-cql/src/types/deserialize/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,20 @@ fn test_date_time_types() {
&time_03::OffsetDateTime::from_unix_timestamp(0xdead_cafe).unwrap(),
&mut Bytes::new(),
);

#[cfg(feature = "time-03")]
assert_ser_de_identity(
&ColumnType::Timestamp,
&time_03::PrimitiveDateTime::new(
time_03::OffsetDateTime::from_unix_timestamp(0xdead_cafe)
.unwrap()
.date(),
time_03::OffsetDateTime::from_unix_timestamp(0xdead_cafe)
.unwrap()
.time(),
),
&mut Bytes::new(),
);
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions scylla-cql/src/types/serialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ impl SerializeValue for time_03::OffsetDateTime {
<CqlTimestamp as SerializeValue>::serialize(&(*me).into(), typ, writer)?
});
}

#[cfg(feature = "time-03")]
impl SerializeValue for time_03::PrimitiveDateTime {
impl_serialize_via_writer!(|me, typ, writer| {
exact_type_check!(typ, Timestamp);
<CqlTimestamp as SerializeValue>::serialize(&(*me).into(), typ, writer)?
});
}

#[cfg(feature = "time-03")]
impl SerializeValue for time_03::Time {
impl_serialize_via_writer!(|me, typ, writer| {
Expand Down
Loading