Skip to content

Added serde Derive Traits to Scylla Value Types. #1413

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scylla-cql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ harness = false

[features]
secrecy-08 = ["dep:secrecy-08"]
serde = ["dep:serde", "uuid/serde"]
time-03 = ["dep:time-03"]
chrono-04 = []
num-bigint-03 = ["dep:num-bigint-03"]
Expand Down
53 changes: 33 additions & 20 deletions scylla-cql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use std::net::IpAddr;
use std::result::Result as StdResult;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

Expand Down Expand Up @@ -33,6 +35,7 @@ pub struct ValueOverflow;
pub struct Unset;

/// Represents an counter value
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Counter(pub i64);

Expand Down Expand Up @@ -61,6 +64,7 @@ impl<V> MaybeUnset<V> {
///
/// This type has custom comparison logic which follows ScyllaDB/Cassandra semantics.
/// For details, see [`Ord` implementation](#impl-Ord-for-CqlTimeuuid).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Eq)]
pub struct CqlTimeuuid(Uuid);

Expand Down Expand Up @@ -277,13 +281,15 @@ impl std::hash::Hash for CqlTimeuuid {
///
/// The implementation of [`PartialEq`], however, normalizes the underlying bytes
/// before comparison. For details, check [examples](#impl-PartialEq-for-CqlVarint).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, Debug)]
pub struct CqlVarint(Vec<u8>);

/// A borrowed version of native CQL `varint` representation.
///
/// Refer to the documentation of [`CqlVarint`].
/// Especially, see the disclaimer about [non-normalized values](CqlVarint#db-data-format).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, Debug)]
pub struct CqlVarintBorrowed<'b>(&'b [u8]);

Expand Down Expand Up @@ -511,6 +517,7 @@ impl From<CqlVarintBorrowed<'_>> for num_bigint_04::BigInt {
/// Notice that [constructors](CqlDecimal#impl-CqlDecimal)
/// don't perform any normalization on the provided data.
/// For more details, see [`CqlVarint`] documentation.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CqlDecimal {
int_val: CqlVarint,
Expand All @@ -525,8 +532,10 @@ pub struct CqlDecimal {
///
/// Refer to the documentation of [`CqlDecimal`].
/// Especially, see the disclaimer about [non-normalized values](CqlDecimal#db-data-format).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CqlDecimalBorrowed<'b> {
#[cfg_attr(feature = "serde", serde(borrow))]
int_val: CqlVarintBorrowed<'b>,
scale: i32,
}
Expand Down Expand Up @@ -632,18 +641,21 @@ impl TryFrom<bigdecimal_04::BigDecimal> for CqlDecimal {
/// Native CQL date representation that allows for a bigger range of dates (-262145-1-1 to 262143-12-31).
///
/// Represented as number of days since -5877641-06-23 i.e. 2^31 days before unix epoch.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CqlDate(pub u32);

/// Native CQL timestamp representation that allows full supported timestamp range.
///
/// Represented as signed milliseconds since unix epoch.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CqlTimestamp(pub i64);

/// Native CQL time representation.
///
/// Represented as nanoseconds since midnight.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CqlTime(pub i64);

Expand Down Expand Up @@ -852,6 +864,7 @@ impl TryInto<time_03::Time> for CqlTime {
}

/// Represents a CQL Duration value
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub struct CqlDuration {
/// Number of months.
Expand Down Expand Up @@ -1355,83 +1368,83 @@ pub fn deser_cql_value(

Ok(match typ {
Native(Ascii) => {
let s = String::deserialize(typ, v)?;
let s = DeserializeValue::deserialize(typ, v)?;
CqlValue::Ascii(s)
}
Native(Boolean) => {
let b = bool::deserialize(typ, v)?;
let b = DeserializeValue::deserialize(typ, v)?;
CqlValue::Boolean(b)
}
Native(Blob) => {
let b = Vec::<u8>::deserialize(typ, v)?;
let b = DeserializeValue::deserialize(typ, v)?;
CqlValue::Blob(b)
}
Native(Date) => {
let d = CqlDate::deserialize(typ, v)?;
let d = DeserializeValue::deserialize(typ, v)?;
CqlValue::Date(d)
}
Native(Counter) => {
let c = crate::value::Counter::deserialize(typ, v)?;
let c = DeserializeValue::deserialize(typ, v)?;
CqlValue::Counter(c)
}
Native(Decimal) => {
let d = CqlDecimal::deserialize(typ, v)?;
let d = DeserializeValue::deserialize(typ, v)?;
CqlValue::Decimal(d)
}
Native(Double) => {
let d = f64::deserialize(typ, v)?;
let d = DeserializeValue::deserialize(typ, v)?;
CqlValue::Double(d)
}
Native(Float) => {
let f = f32::deserialize(typ, v)?;
let f = DeserializeValue::deserialize(typ, v)?;
CqlValue::Float(f)
}
Native(Int) => {
let i = i32::deserialize(typ, v)?;
let i = DeserializeValue::deserialize(typ, v)?;
CqlValue::Int(i)
}
Native(SmallInt) => {
let si = i16::deserialize(typ, v)?;
let si = DeserializeValue::deserialize(typ, v)?;
CqlValue::SmallInt(si)
}
Native(TinyInt) => {
let ti = i8::deserialize(typ, v)?;
let ti = DeserializeValue::deserialize(typ, v)?;
CqlValue::TinyInt(ti)
}
Native(BigInt) => {
let bi = i64::deserialize(typ, v)?;
let bi = DeserializeValue::deserialize(typ, v)?;
CqlValue::BigInt(bi)
}
Native(Text) => {
let s = String::deserialize(typ, v)?;
let s = DeserializeValue::deserialize(typ, v)?;
CqlValue::Text(s)
}
Native(Timestamp) => {
let t = CqlTimestamp::deserialize(typ, v)?;
let t = DeserializeValue::deserialize(typ, v)?;
CqlValue::Timestamp(t)
}
Native(Time) => {
let t = CqlTime::deserialize(typ, v)?;
let t = DeserializeValue::deserialize(typ, v)?;
CqlValue::Time(t)
}
Native(Timeuuid) => {
let t = CqlTimeuuid::deserialize(typ, v)?;
let t = DeserializeValue::deserialize(typ, v)?;
CqlValue::Timeuuid(t)
}
Native(Duration) => {
let d = CqlDuration::deserialize(typ, v)?;
let d = DeserializeValue::deserialize(typ, v)?;
CqlValue::Duration(d)
}
Native(Inet) => {
let i = IpAddr::deserialize(typ, v)?;
let i = DeserializeValue::deserialize(typ, v)?;
CqlValue::Inet(i)
}
Native(Uuid) => {
let uuid = uuid::Uuid::deserialize(typ, v)?;
let uuid = DeserializeValue::deserialize(typ, v)?;
CqlValue::Uuid(uuid)
}
Native(Varint) => {
let vi = CqlVarint::deserialize(typ, v)?;
let vi = DeserializeValue::deserialize(typ, v)?;
CqlValue::Varint(vi)
}
Collection {
Expand Down
1 change: 1 addition & 0 deletions scylla/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
default = []
openssl-010 = ["dep:tokio-openssl", "dep:openssl"]
rustls-023 = ["dep:tokio-rustls", "dep:rustls"]
serde = ["scylla-cql/serde"]
unstable-cloud = [
"scylla-cql/serde",
"dep:serde_yaml",
Expand Down
Loading