Skip to content

Commit

Permalink
Add serialization traits
Browse files Browse the repository at this point in the history
This commit adds experimental version of new serialization traits.
Blanket implementation are provided to support older traits.
  • Loading branch information
Lorak-mmk committed Sep 29, 2023
1 parent 07b262c commit b58f804
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scylla-cql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions scylla-cql/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod serialize;
6 changes: 6 additions & 0 deletions scylla-cql/src/types/serialize/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::{any::Any, sync::Arc};

pub mod row;
pub mod value;

type SerializationError = Arc<dyn Any + Send + Sync>;
49 changes: 49 additions & 0 deletions scylla-cql/src/types/serialize/row.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
) -> Result<(), SerializationError>;
}

impl<T: ValueList> SerializeRow for T {
fn preliminary_type_check(
_ctx: &RowSerializationContext<'_>,
) -> Result<(), SerializationError> {
Ok(())
}

fn serialize(
&self,
_ctx: &RowSerializationContext<'_>,
out: &mut Vec<u8>,
) -> Result<(), SerializationError> {
self.write_to_request(out)
.map_err(|err| Arc::new(err) as SerializationError)
}
}
22 changes: 22 additions & 0 deletions scylla-cql/src/types/serialize/value.rs
Original file line number Diff line number Diff line change
@@ -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<u8>) -> Result<(), SerializationError>;
}

impl<T: Value> SerializeCql for T {
fn preliminary_type_check(_typ: &ColumnType) -> Result<(), SerializationError> {
Ok(())
}

fn serialize(&self, _typ: &ColumnType, buf: &mut Vec<u8>) -> Result<(), SerializationError> {
self.serialize(buf)
.map_err(|err| Arc::new(err) as SerializationError)
}
}

0 comments on commit b58f804

Please sign in to comment.