From 3f7a8e08e6f36e56cae1ce59f00989e07be94d6e Mon Sep 17 00:00:00 2001 From: muzarski Date: Mon, 1 Jul 2024 10:51:16 +0200 Subject: [PATCH] f_errors: derive Clone 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. They require a `Clone`, since there is some cloning of errors in `history` module. 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. --- scylla-cql/src/frame/frame_errors.rs | 46 ++++++++++++++++------------ 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/scylla-cql/src/frame/frame_errors.rs b/scylla-cql/src/frame/frame_errors.rs index 3c6dd36b2f..2edf8bcc7a 100644 --- a/scylla-cql/src/frame/frame_errors.rs +++ b/scylla-cql/src/frame/frame_errors.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use super::TryFromPrimitiveError; use crate::cql_to_rust::CqlTypeError; use crate::frame::value::SerializeValuesError; @@ -69,7 +71,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), @@ -85,7 +87,7 @@ 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), @@ -93,7 +95,7 @@ pub enum CqlAuthChallengeParseError { /// 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), @@ -101,7 +103,7 @@ pub enum CqlAuthSuccessParseError { /// 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), @@ -109,7 +111,7 @@ pub enum CqlAuthenticateParseError { /// 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), @@ -117,7 +119,7 @@ pub enum CqlSupportedParseError { /// 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), @@ -136,7 +138,7 @@ pub enum CqlResultParseError { } #[non_exhaustive] -#[derive(Error, Debug)] +#[derive(Error, Debug, Clone)] pub enum SetKeyspaceParseError { #[error("Malformed keyspace name: {0}")] MalformedKeyspaceName(#[from] LowLevelDeserializationError), @@ -145,7 +147,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), @@ -162,7 +164,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 schema change type string: {0}")] TypeOfChangeParseError(LowLevelDeserializationError), @@ -186,7 +188,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 change: {0}")] TypeOfChangeParseError(LowLevelDeserializationError), @@ -199,7 +201,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), @@ -212,7 +214,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), @@ -230,7 +232,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), @@ -251,7 +253,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}")] MalformedKeyspaceName(LowLevelDeserializationError), @@ -262,7 +264,7 @@ pub enum TableSpecParseError { /// An error type returned when deserialization /// of table column specifications fails. #[non_exhaustive] -#[derive(Error, Debug)] +#[derive(Error, Debug, Clone)] #[error("Column spec deserialization failed, column index: {column_index}, error: {kind}")] pub struct ColumnSpecParseError { pub column_index: usize, @@ -272,7 +274,7 @@ pub struct ColumnSpecParseError { /// 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), @@ -284,7 +286,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 CqlTypeParseError { #[error("Malformed type id: {0}")] TypeIdParseError(LowLevelDeserializationError), @@ -315,10 +317,10 @@ pub enum CqlTypeParseError { /// - 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), #[error(transparent)] TryFromIntError(#[from] std::num::TryFromIntError), #[error(transparent)] @@ -334,3 +336,9 @@ pub enum LowLevelDeserializationError { #[error("UTF8 deserialization failed: {0}")] UTF8DeserializationError(#[from] std::str::Utf8Error), } + +impl From for LowLevelDeserializationError { + fn from(value: std::io::Error) -> Self { + Self::IoError(Arc::new(value)) + } +}