diff --git a/Cargo.lock.msrv b/Cargo.lock.msrv index 956c585541..8e18ce9123 100644 --- a/Cargo.lock.msrv +++ b/Cargo.lock.msrv @@ -1002,48 +1002,6 @@ dependencies = [ "libc", ] -[[package]] -name = "num_enum" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f646caf906c20226733ed5b1374287eb97e3c2a5c227ce668c1f2ce20ae57c9" -dependencies = [ - "num_enum_derive 0.5.11", -] - -[[package]] -name = "num_enum" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a015b430d3c108a207fd776d2e2196aaf8b1cf8cf93253e3a097ff3085076a1" -dependencies = [ - "num_enum_derive 0.6.1", -] - -[[package]] -name = "num_enum_derive" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799" -dependencies = [ - "proc-macro-crate", - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "num_enum_derive" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6" -dependencies = [ - "proc-macro-crate", - "proc-macro2", - "quote", - "syn 2.0.32", -] - [[package]] name = "object" version = "0.32.1" @@ -1493,7 +1451,6 @@ dependencies = [ "ntest", "num-bigint 0.3.3", "num-bigint 0.4.4", - "num_enum 0.6.1", "openssl", "rand", "rand_chacha", @@ -1531,7 +1488,6 @@ dependencies = [ "lz4_flex", "num-bigint 0.3.3", "num-bigint 0.4.4", - "num_enum 0.6.1", "scylla-macros", "secrecy", "serde", @@ -1564,7 +1520,6 @@ dependencies = [ "futures", "ntest", "num-bigint 0.3.3", - "num_enum 0.5.11", "rand", "scylla-cql", "thiserror", diff --git a/scylla-cql/Cargo.toml b/scylla-cql/Cargo.toml index 139e429cef..edbfcd3c8f 100644 --- a/scylla-cql/Cargo.toml +++ b/scylla-cql/Cargo.toml @@ -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" diff --git a/scylla-cql/src/frame/frame_errors.rs b/scylla-cql/src/frame/frame_errors.rs index 9a3b228505..a203762c53 100644 --- a/scylla-cql/src/frame/frame_errors.rs +++ b/scylla-cql/src/frame/frame_errors.rs @@ -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; @@ -25,7 +25,7 @@ pub enum FrameError { #[error(transparent)] StdIoError(#[from] std::io::Error), #[error("Unrecognized opcode{0}")] - TryFromPrimitiveError(#[from] num_enum::TryFromPrimitiveError), + TryFromPrimitiveError(#[from] TryFromPrimitiveError), #[error("Error compressing lz4 data {0}")] Lz4CompressError(#[from] lz4_flex::block::CompressError), #[error("Error decompressing lz4 data {0}")] diff --git a/scylla-cql/src/frame/mod.rs b/scylla-cql/src/frame/mod.rs index 52e6dad238..0ed81ec593 100644 --- a/scylla-cql/src/frame/mod.rs +++ b/scylla-cql/src/frame/mod.rs @@ -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; @@ -259,6 +260,14 @@ fn decompress(mut comp_body: &[u8], compression: Compression) -> Result, } } +/// 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 { + enum_name: &'static str, + primitive: T, +} + #[cfg(test)] mod test { use super::*; diff --git a/scylla-cql/src/frame/request/mod.rs b/scylla-cql/src/frame/request/mod.rs index e0146156a2..37549513e1 100644 --- a/scylla-cql/src/frame/request/mod.rs +++ b/scylla-cql/src/frame/request/mod.rs @@ -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; @@ -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, @@ -37,6 +37,27 @@ pub enum RequestOpcode { AuthResponse = 0x0F, } +impl TryFrom for RequestOpcode { + type Error = TryFromPrimitiveError; + + fn try_from(value: u8) -> Result { + 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; diff --git a/scylla-cql/src/frame/response/mod.rs b/scylla-cql/src/frame/response/mod.rs index c8c4ec104d..5acec7f34b 100644 --- a/scylla-cql/src/frame/response/mod.rs +++ b/scylla-cql/src/frame/response/mod.rs @@ -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, @@ -25,6 +26,27 @@ pub enum ResponseOpcode { AuthSuccess = 0x10, } +impl TryFrom for ResponseOpcode { + type Error = TryFromPrimitiveError; + + fn try_from(value: u8) -> Result> { + 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), diff --git a/scylla-cql/src/frame/types.rs b/scylla-cql/src/frame/types.rs index 5de8124111..b5d3351478 100644 --- a/scylla-cql/src/frame/types.rs +++ b/scylla-cql/src/frame/types.rs @@ -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; @@ -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)] @@ -35,7 +35,31 @@ pub enum Consistency { LocalSerial = 0x0009, } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive)] +impl TryFrom for Consistency { + type Error = TryFromPrimitiveError; + + fn try_from(value: u16) -> Result { + 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)] @@ -44,6 +68,21 @@ pub enum SerialConsistency { LocalSerial = 0x0009, } +impl TryFrom for SerialConsistency { + type Error = TryFromPrimitiveError; + + fn try_from(value: i16) -> Result { + 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) diff --git a/scylla-proxy/Cargo.toml b/scylla-proxy/Cargo.toml index f114a9f4ea..e268da0c9e 100644 --- a/scylla-proxy/Cargo.toml +++ b/scylla-proxy/Cargo.toml @@ -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" diff --git a/scylla/Cargo.toml b/scylla/Cargo.toml index 5377373c35..dda2d12427 100644 --- a/scylla/Cargo.toml +++ b/scylla/Cargo.toml @@ -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"] }