scylla-rust-driver icon indicating copy to clipboard operation
scylla-rust-driver copied to clipboard

Scylla DB Timestamp: ValueList, FromRow, and Serialize without 3 layers of wrapping.

Open WarrenN1 opened this issue 1 year ago • 1 comments

I need to have a struct like:

#[derive(Debug, FromRow, ValueList, Serialize)]
struct Task
{
action : i64,
dt : chrono::Duration
}

The problem is that this does not implement ValueList nor Serialize for Serde even though Timestamp is represented as chrono::Duration. However, If i store it as

#[derive(Debug, FromRow, ValueList, Serialize)]
struct Task
{
action : i64,
dt : scylla::frame::value::Timestamp
}

In this scenario, the difficulty is that Timestamp does not implement Serialize from serde.

I would like to avoid having to construct a struct to wrap the scylla::frame::value::Timestamp which wraps the chrono::Duration, but that seems to be the requirement and I would like to avoid that if possible. Especially because this would require me to implement also FromRow and Value list for my wrapper on a wrapper on a wrapper on a i64.

WarrenN1 avatar Jun 21 '23 23:06 WarrenN1

Serde provides some workarounds for third-party types that do not implement Serialize/Deserialize; check out this page: https://serde.rs/field-attrs.html and the serialize_with, deserialize_with and with attributes.

I'd like to implement something similar for the new trait/procedural macro DeserializeCql which will be introduced when the work in https://github.com/scylladb/scylla-rust-driver/pull/665 gets merged.

piodul avatar Jun 22 '23 09:06 piodul