Skip to content

Commit

Permalink
f_errors: derive Clone
Browse files Browse the repository at this point in the history
In the following commit we want to introduce a
CqlResponseParseError type which will be a new variant
of `[Query/NewSession]Error`. These two error types
implement a `Clone` trait.

This is why, we need to derive Clone for all of the types
introduces in previous commits.

Unfortunately, the `std::io::Error` does not implement Clone.
This is why we need to wrap any appearing `std::io::Error` with
`Arc`, so the enum types encapsulating it can derive Clone.
  • Loading branch information
muzarski committed Jul 1, 2024
1 parent dcd644b commit fc433ad
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions scylla-cql/src/frame/frame_errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use super::TryFromPrimitiveError;
use crate::cql_to_rust::CqlTypeError;
use crate::frame::value::SerializeValuesError;
Expand Down Expand Up @@ -71,7 +73,7 @@ pub enum ParseError {

/// An error type returned when deserialization of ERROR response fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum CqlErrorParseError {
#[error("Malformed error code: {0}")]
ErrorCodeParseError(LowLevelDeserializationError),
Expand All @@ -83,39 +85,39 @@ pub enum CqlErrorParseError {

/// An error type returned when deserialization of AUTH_CHALLENGE response fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum CqlAuthChallengeParseError {
#[error("Malformed authenticate message: {0}")]
AuthMessageParseError(LowLevelDeserializationError),
}

/// An error type returned when deserialization of AUTH_SUCCESS response fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum CqlAuthSuccessParseError {
#[error("Malformed success message: {0}")]
SuccessMessageParseError(LowLevelDeserializationError),
}

/// An error type returned when deserialization of AUTHENTICATE response fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum CqlAuthenticateParseError {
#[error("Malformed authenticator name: {0}")]
AuthNameParseError(LowLevelDeserializationError),
}

/// An error type returned when deserialization of SUPPORTED response fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum CqlSupportedParseError {
#[error("Malformed options map: {0}")]
OptionsMapDeserialization(LowLevelDeserializationError),
}

/// An error type returned when deserialization of RESULT response fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum CqlResultParseError {
#[error("Malformed RESULT response id: {0}")]
ResultIdParseError(LowLevelDeserializationError),
Expand All @@ -132,7 +134,7 @@ pub enum CqlResultParseError {
}

#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum SetKeyspaceParseError {
#[error("Malformed keyspace name: {0}")]
StringDeserializationError(#[from] LowLevelDeserializationError),
Expand All @@ -141,7 +143,7 @@ pub enum SetKeyspaceParseError {
/// An error type returned when deserialization of
/// `EVENT` response fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum CqlEventParseError {
#[error("Malformed event type string: {0}")]
EventTypeParseError(LowLevelDeserializationError),
Expand All @@ -158,7 +160,7 @@ pub enum CqlEventParseError {
/// An error type returned when deserialization of
/// SchemaChangeEvent fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum SchemaChangeEventParseError {
#[error("Malformed type of schema change string: {0}")]
TypeOfChangeParseError(LowLevelDeserializationError),
Expand All @@ -182,7 +184,7 @@ pub enum SchemaChangeEventParseError {

/// An error type returned when deserialization of [Status/Topology]ChangeEvent fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum ClusterChangeEventParseError {
#[error("Malformed type of status change: {0}")]
TypeOfChangeParseError(LowLevelDeserializationError),
Expand All @@ -195,7 +197,7 @@ pub enum ClusterChangeEventParseError {
/// An error type returned when deserialization
/// of `RESULT::`Prepared` response fails.
#[non_exhaustive]
#[derive(Debug, Error)]
#[derive(Debug, Error, Clone)]
pub enum PreparedParseError {
#[error("Malformed prepared statement's id length: {0}")]
IdLengthParseError(LowLevelDeserializationError),
Expand All @@ -208,7 +210,7 @@ pub enum PreparedParseError {
/// An error type returned when deserialization
/// of `RESULT::Rows` response fails.
#[non_exhaustive]
#[derive(Debug, Error)]
#[derive(Debug, Error, Clone)]
pub enum RowsParseError {
#[error("Invalid result metadata: {0}")]
ResultMetadataParseError(#[from] ResultMetadataParseError),
Expand All @@ -226,7 +228,7 @@ pub enum RowsParseError {
/// An error type returned when deserialization
/// of `[Result/Prepared]Metadata` failed.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum ResultMetadataParseError {
#[error("Malformed metadata flags: {0}")]
FlagsParseError(LowLevelDeserializationError),
Expand All @@ -247,7 +249,7 @@ pub enum ResultMetadataParseError {
/// An error type returned when deserialization
/// of table specification fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum TableSpecParseError {
#[error("Malformed keyspace name: {0}")]
KeyspaceNameParseError(LowLevelDeserializationError),
Expand All @@ -258,7 +260,7 @@ pub enum TableSpecParseError {
/// An error type returned when deserialization
/// of table column specifications fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum ColumnSpecsParseError {
#[error("Column spec deserialization failed, column index: {column_index}, error: {kind}")]
ColumnSpecParseError {
Expand All @@ -270,7 +272,7 @@ pub enum ColumnSpecsParseError {
/// The type of error that appeared during deserialization
/// of a column specification.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum ColumnSpecParseErrorKind {
#[error("Invalid table spec: {0}")]
TableSpecParseError(#[from] TableSpecParseError),
Expand All @@ -282,7 +284,7 @@ pub enum ColumnSpecParseErrorKind {

/// An error type returned when deserialization of CQL type name fails.
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum TypeParseError {
#[error("Malformed type id: {0}")]
TypeIdParseError(LowLevelDeserializationError),
Expand Down Expand Up @@ -310,7 +312,7 @@ pub enum TypeParseError {
/// Possible error kinds:
/// - out of range integer conversion
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum LowLevelSerializationError {
#[error(transparent)]
TryFromIntError(#[from] std::num::TryFromIntError),
Expand All @@ -327,10 +329,10 @@ pub enum LowLevelSerializationError {
/// - conversion errors - e.g. slice-to-array or primitive-to-enum
/// - not enough bytes in the buffer to deserialize a value
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum LowLevelDeserializationError {
#[error(transparent)]
IoError(#[from] std::io::Error),
IoError(Arc<std::io::Error>),
#[error(transparent)]
TryFromIntError(#[from] std::num::TryFromIntError),
#[error("Failed to convert slice into array: {0}")]
Expand All @@ -346,3 +348,9 @@ pub enum LowLevelDeserializationError {
#[error("UTF8 deserialization failed: {0}")]
UTF8DeserializationError(#[from] std::str::Utf8Error),
}

impl From<std::io::Error> for LowLevelDeserializationError {
fn from(value: std::io::Error) -> Self {
Self::IoError(Arc::new(value))
}
}

0 comments on commit fc433ad

Please sign in to comment.