Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove num_enum dependency #931

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion scylla-cql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ license = "MIT OR Apache-2.0"
scylla-macros = { version = "0.4.0", path = "../scylla-macros" }
byteorder = "1.3.4"
bytes = "1.0.1"
num_enum = "0.6"
tokio = { version = "1.12", features = ["io-util", "time"] }
secrecy = { version = "0.7.0", optional = true }
snap = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions scylla-cql/src/frame/frame_errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::response;
use super::TryFromPrimitiveError;
use crate::cql_to_rust::CqlTypeError;
use crate::frame::value::SerializeValuesError;
use crate::types::serialize::SerializationError;
Expand All @@ -25,7 +25,7 @@ pub enum FrameError {
#[error(transparent)]
StdIoError(#[from] std::io::Error),
#[error("Unrecognized opcode{0}")]
TryFromPrimitiveError(#[from] num_enum::TryFromPrimitiveError<response::ResponseOpcode>),
TryFromPrimitiveError(#[from] TryFromPrimitiveError<u8>),
#[error("Error compressing lz4 data {0}")]
Lz4CompressError(#[from] lz4_flex::block::CompressError),
#[error("Error decompressing lz4 data {0}")]
Expand Down
9 changes: 9 additions & 0 deletions scylla-cql/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod value_tests;

use crate::frame::frame_errors::FrameError;
use bytes::{Buf, BufMut, Bytes};
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncReadExt};
use uuid::Uuid;

Expand Down Expand Up @@ -259,6 +260,14 @@ fn decompress(mut comp_body: &[u8], compression: Compression) -> Result<Vec<u8>,
}
}

/// An error type for parsing an enum value from a primitive.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[error("No discrimant in enum `{enum_name}` matches the value `{primitive:?}`")]
pub struct TryFromPrimitiveError<T: Copy + std::fmt::Debug> {
enum_name: &'static str,
primitive: T,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
25 changes: 23 additions & 2 deletions scylla-cql/src/frame/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod startup;
use crate::types::serialize::row::SerializedValues;
use crate::{frame::frame_errors::ParseError, Consistency};
use bytes::Bytes;
use num_enum::TryFromPrimitive;

pub use auth_response::AuthResponse;
pub use batch::Batch;
Expand All @@ -23,8 +22,9 @@ pub use startup::Startup;
use self::batch::BatchStatement;

use super::types::SerialConsistency;
use super::TryFromPrimitiveError;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum RequestOpcode {
Startup = 0x01,
Expand All @@ -37,6 +37,27 @@ pub enum RequestOpcode {
AuthResponse = 0x0F,
}

impl TryFrom<u8> for RequestOpcode {
type Error = TryFromPrimitiveError<u8>;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x01 => Ok(Self::Startup),
0x05 => Ok(Self::Options),
0x07 => Ok(Self::Query),
0x09 => Ok(Self::Prepare),
0x0A => Ok(Self::Execute),
0x0B => Ok(Self::Register),
0x0D => Ok(Self::Batch),
0x0F => Ok(Self::AuthResponse),
_ => Err(TryFromPrimitiveError {
enum_name: "RequestOpcode",
primitive: value,
}),
}
}
}

pub trait SerializableRequest {
const OPCODE: RequestOpcode;

Expand Down
26 changes: 24 additions & 2 deletions scylla-cql/src/frame/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ pub mod result;
pub mod supported;

use crate::{errors::QueryError, frame::frame_errors::ParseError};
use num_enum::TryFromPrimitive;

use crate::frame::protocol_features::ProtocolFeatures;
pub use error::Error;
pub use supported::Supported;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive)]
use super::TryFromPrimitiveError;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ResponseOpcode {
Error = 0x00,
Expand All @@ -25,6 +26,27 @@ pub enum ResponseOpcode {
AuthSuccess = 0x10,
}

impl TryFrom<u8> for ResponseOpcode {
type Error = TryFromPrimitiveError<u8>;

fn try_from(value: u8) -> Result<Self, TryFromPrimitiveError<u8>> {
match value {
0x00 => Ok(Self::Error),
0x02 => Ok(Self::Ready),
0x03 => Ok(Self::Authenticate),
0x06 => Ok(Self::Supported),
0x08 => Ok(Self::Result),
0x0C => Ok(Self::Event),
0x0E => Ok(Self::AuthChallenge),
0x10 => Ok(Self::AuthSuccess),
_ => Err(TryFromPrimitiveError {
enum_name: "ResponseOpcode",
primitive: value,
}),
}
}
}

#[derive(Debug)]
pub enum Response {
Error(Error),
Expand Down
45 changes: 42 additions & 3 deletions scylla-cql/src/frame/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! CQL binary protocol in-wire types.

use super::frame_errors::ParseError;
use super::TryFromPrimitiveError;
use byteorder::{BigEndian, ReadBytesExt};
use bytes::{Buf, BufMut};
use num_enum::TryFromPrimitive;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::convert::TryInto;
Expand All @@ -13,7 +13,7 @@ use std::str;
use thiserror::Error;
use uuid::Uuid;

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive)]
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "SCREAMING_SNAKE_CASE"))]
#[repr(u16)]
Expand All @@ -35,7 +35,31 @@ pub enum Consistency {
LocalSerial = 0x0009,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive)]
impl TryFrom<u16> for Consistency {
type Error = TryFromPrimitiveError<u16>;

fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
0x0000 => Ok(Consistency::Any),
0x0001 => Ok(Consistency::One),
0x0002 => Ok(Consistency::Two),
0x0003 => Ok(Consistency::Three),
0x0004 => Ok(Consistency::Quorum),
0x0005 => Ok(Consistency::All),
0x0006 => Ok(Consistency::LocalQuorum),
0x0007 => Ok(Consistency::EachQuorum),
0x000A => Ok(Consistency::LocalOne),
0x0008 => Ok(Consistency::Serial),
0x0009 => Ok(Consistency::LocalSerial),
_ => Err(TryFromPrimitiveError {
enum_name: "Consistency",
primitive: value,
}),
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "SCREAMING_SNAKE_CASE"))]
#[repr(i16)]
Expand All @@ -44,6 +68,21 @@ pub enum SerialConsistency {
LocalSerial = 0x0009,
}

impl TryFrom<i16> for SerialConsistency {
type Error = TryFromPrimitiveError<i16>;

fn try_from(value: i16) -> Result<Self, Self::Error> {
match value {
0x0008 => Ok(Self::Serial),
0x0009 => Ok(Self::LocalSerial),
_ => Err(TryFromPrimitiveError {
enum_name: "SerialConsistency",
primitive: value,
}),
}
}
}

impl Consistency {
pub fn is_serial(&self) -> bool {
matches!(self, Consistency::Serial | Consistency::LocalSerial)
Expand Down
1 change: 0 additions & 1 deletion scylla-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ scylla-cql = { version = "0.1.0", path = "../scylla-cql" }
byteorder = "1.3.4"
bytes = "1.2.0"
futures = "0.3.6"
num_enum = "0.5"
tokio = { version = "1.12", features = ["net", "time", "io-util", "sync", "rt", "macros", "rt-multi-thread"] }
uuid = "1.0"
thiserror = "1.0.32"
Expand Down
1 change: 0 additions & 1 deletion scylla/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ byteorder = "1.3.4"
bytes = "1.0.1"
futures = "0.3.6"
histogram = "0.6.9"
num_enum = "0.6"
tokio = { version = "1.27", features = ["net", "time", "io-util", "sync", "rt", "macros"] }
snap = "1.0"
uuid = { version = "1.0", features = ["v4"] }
Expand Down
Loading