From b58f804e8dfe2f3bdc250ed751aaf46e2aa951e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Bary=C5=82a?= Date: Thu, 28 Sep 2023 16:21:08 +0200 Subject: [PATCH] Add serialization traits This commit adds experimental version of new serialization traits. Blanket implementation are provided to support older traits. --- scylla-cql/src/lib.rs | 2 + scylla-cql/src/types/mod.rs | 1 + scylla-cql/src/types/serialize/mod.rs | 6 +++ scylla-cql/src/types/serialize/row.rs | 49 +++++++++++++++++++++++++ scylla-cql/src/types/serialize/value.rs | 22 +++++++++++ 5 files changed, 80 insertions(+) create mode 100644 scylla-cql/src/types/mod.rs create mode 100644 scylla-cql/src/types/serialize/mod.rs create mode 100644 scylla-cql/src/types/serialize/row.rs create mode 100644 scylla-cql/src/types/serialize/value.rs diff --git a/scylla-cql/src/lib.rs b/scylla-cql/src/lib.rs index 47b58b4f4e..b8d7d28671 100644 --- a/scylla-cql/src/lib.rs +++ b/scylla-cql/src/lib.rs @@ -3,6 +3,8 @@ pub mod frame; #[macro_use] pub mod macros; +pub mod types; + pub use crate::frame::response::cql_to_rust; pub use crate::frame::response::cql_to_rust::FromRow; diff --git a/scylla-cql/src/types/mod.rs b/scylla-cql/src/types/mod.rs new file mode 100644 index 0000000000..ec9942a885 --- /dev/null +++ b/scylla-cql/src/types/mod.rs @@ -0,0 +1 @@ +pub mod serialize; diff --git a/scylla-cql/src/types/serialize/mod.rs b/scylla-cql/src/types/serialize/mod.rs new file mode 100644 index 0000000000..0cda84e252 --- /dev/null +++ b/scylla-cql/src/types/serialize/mod.rs @@ -0,0 +1,6 @@ +use std::{any::Any, sync::Arc}; + +pub mod row; +pub mod value; + +type SerializationError = Arc; diff --git a/scylla-cql/src/types/serialize/row.rs b/scylla-cql/src/types/serialize/row.rs new file mode 100644 index 0000000000..2e9832412d --- /dev/null +++ b/scylla-cql/src/types/serialize/row.rs @@ -0,0 +1,49 @@ +use std::sync::Arc; + +use crate::frame::response::result::ColumnSpec; +use crate::frame::value::ValueList; + +use super::SerializationError; + +pub struct RowSerializationContext<'a> { + columns: &'a [ColumnSpec], +} + +impl<'a> RowSerializationContext<'a> { + #[inline] + pub fn columns(&self) -> &'a [ColumnSpec] { + self.columns + } + + // TODO: change RowSerializationContext to make this faster + #[inline] + pub fn column_by_name(&self, target: &str) -> Option<&ColumnSpec> { + self.columns.iter().find(|&c| c.name == target) + } +} + +pub trait SerializeRow { + fn preliminary_type_check(ctx: &RowSerializationContext<'_>) -> Result<(), SerializationError>; + fn serialize( + &self, + ctx: &RowSerializationContext<'_>, + out: &mut Vec, + ) -> Result<(), SerializationError>; +} + +impl SerializeRow for T { + fn preliminary_type_check( + _ctx: &RowSerializationContext<'_>, + ) -> Result<(), SerializationError> { + Ok(()) + } + + fn serialize( + &self, + _ctx: &RowSerializationContext<'_>, + out: &mut Vec, + ) -> Result<(), SerializationError> { + self.write_to_request(out) + .map_err(|err| Arc::new(err) as SerializationError) + } +} diff --git a/scylla-cql/src/types/serialize/value.rs b/scylla-cql/src/types/serialize/value.rs new file mode 100644 index 0000000000..43eb9ef738 --- /dev/null +++ b/scylla-cql/src/types/serialize/value.rs @@ -0,0 +1,22 @@ +use std::sync::Arc; + +use crate::frame::response::result::ColumnType; +use crate::frame::value::Value; + +use super::SerializationError; + +pub trait SerializeCql { + fn preliminary_type_check(typ: &ColumnType) -> Result<(), SerializationError>; + fn serialize(&self, typ: &ColumnType, buf: &mut Vec) -> Result<(), SerializationError>; +} + +impl SerializeCql for T { + fn preliminary_type_check(_typ: &ColumnType) -> Result<(), SerializationError> { + Ok(()) + } + + fn serialize(&self, _typ: &ColumnType, buf: &mut Vec) -> Result<(), SerializationError> { + self.serialize(buf) + .map_err(|err| Arc::new(err) as SerializationError) + } +}