Skip to content

Commit

Permalink
Merge pull request #865 from Lorak-mmk/serialization_error_newtype
Browse files Browse the repository at this point in the history
Make SerializationError a newtype
  • Loading branch information
piodul authored Dec 1, 2023
2 parents 46e33c9 + 7bcc461 commit 2f78d46
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
12 changes: 10 additions & 2 deletions scylla-cql/src/types/serialize/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{any::Any, sync::Arc};
use std::{error::Error, fmt::Display, sync::Arc};

use thiserror::Error;

pub mod row;
pub mod value;
Expand All @@ -8,5 +10,11 @@ pub use writers::{
BufBackedCellValueBuilder, BufBackedCellWriter, BufBackedRowWriter, CellValueBuilder,
CellWriter, CountingWriter, RowWriter,
};
#[derive(Debug, Clone, Error)]
pub struct SerializationError(Arc<dyn Error + Send + Sync>);

type SerializationError = Arc<dyn Any + Send + Sync>;
impl Display for SerializationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SerializationError: {}", self.0)
}
}
10 changes: 6 additions & 4 deletions scylla-cql/src/types/serialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn serialize_legacy_row<T: ValueList>(
writer: &mut impl RowWriter,
) -> Result<(), SerializationError> {
let serialized =
<T as ValueList>::serialized(r).map_err(|err| Arc::new(err) as SerializationError)?;
<T as ValueList>::serialized(r).map_err(|err| SerializationError(Arc::new(err)))?;

let mut append_value = |value: RawValue| {
let cell_writer = writer.make_cell_writer();
Expand All @@ -93,9 +93,11 @@ pub fn serialize_legacy_row<T: ValueList>(

for col in ctx.columns() {
let val = values_by_name.get(col.name.as_str()).ok_or_else(|| {
Arc::new(ValueListToSerializeRowAdapterError::NoBindMarkerWithName {
name: col.name.clone(),
}) as SerializationError
SerializationError(Arc::new(
ValueListToSerializeRowAdapterError::NoBindMarkerWithName {
name: col.name.clone(),
},
))
})?;
append_value(*val);
}
Expand Down
16 changes: 8 additions & 8 deletions scylla-cql/src/types/serialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ pub fn serialize_legacy_value<T: Value, W: CellWriter>(
) -> Result<W::WrittenCellProof, SerializationError> {
// It's an inefficient and slightly tricky but correct implementation.
let mut buf = Vec::new();
<T as Value>::serialize(v, &mut buf).map_err(|err| Arc::new(err) as SerializationError)?;
<T as Value>::serialize(v, &mut buf).map_err(|err| SerializationError(Arc::new(err)))?;

// Analyze the output.
// All this dance shows how unsafe our previous interface was...
if buf.len() < 4 {
return Err(Arc::new(ValueToSerializeCqlAdapterError::TooShort {
size: buf.len(),
}));
return Err(SerializationError(Arc::new(
ValueToSerializeCqlAdapterError::TooShort { size: buf.len() },
)));
}

let (len_bytes, contents) = buf.split_at(4);
Expand All @@ -66,19 +66,19 @@ pub fn serialize_legacy_value<T: Value, W: CellWriter>(
-1 => Ok(writer.set_null()),
len if len >= 0 => {
if contents.len() != len as usize {
Err(Arc::new(
Err(SerializationError(Arc::new(
ValueToSerializeCqlAdapterError::DeclaredVsActualSizeMismatch {
declared: len as usize,
actual: contents.len(),
},
))
)))
} else {
Ok(writer.set_value(contents))
}
}
_ => Err(Arc::new(
_ => Err(SerializationError(Arc::new(
ValueToSerializeCqlAdapterError::InvalidDeclaredSize { size: len },
)),
))),
}
}

Expand Down

0 comments on commit 2f78d46

Please sign in to comment.