pub mod authenticate;
pub mod custom_type_parser;
pub use scylla_cql_core::frame::response::error;
pub mod event;
pub mod result;
pub mod supported;
use std::sync::Arc;
pub use error::Error;
pub use scylla_cql_core::frame::response::CqlResponseKind;
pub use supported::Supported;
use crate::frame::TryFromPrimitiveError;
use crate::frame::frame_errors::ResultMetadataAndRowsCountParseError;
use crate::frame::protocol_features::ProtocolFeatures;
use crate::frame::response::result::ResultMetadata;
use super::frame_errors::CqlResponseParseError;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ResponseOpcode {
Error = 0x00,
Ready = 0x02,
Authenticate = 0x03,
Supported = 0x06,
Result = 0x08,
Event = 0x0C,
AuthChallenge = 0x0E,
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::new("ResponseOpcode", value)),
}
}
}
#[derive(Debug)]
pub enum Response {
Error(Error),
Ready,
Result(result::Result),
Authenticate(authenticate::Authenticate),
AuthSuccess(authenticate::AuthSuccess),
AuthChallenge(authenticate::AuthChallenge),
Supported(Supported),
Event(event::Event),
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ResponseV2 {
Error(Error),
Ready,
Result(result::Result),
Authenticate(authenticate::Authenticate),
AuthSuccess(authenticate::AuthSuccess),
AuthChallenge(authenticate::AuthChallenge),
Supported(Supported),
Event(event::EventV2),
}
impl Response {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
Response::Error(_) => CqlResponseKind::Error,
Response::Ready => CqlResponseKind::Ready,
Response::Result(_) => CqlResponseKind::Result,
Response::Authenticate(_) => CqlResponseKind::Authenticate,
Response::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
Response::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
Response::Supported(_) => CqlResponseKind::Supported,
Response::Event(_) => CqlResponseKind::Event,
}
}
pub fn deserialize(
features: &ProtocolFeatures,
opcode: ResponseOpcode,
buf_bytes: bytes::Bytes,
cached_metadata: Option<&Arc<ResultMetadata<'static>>>,
) -> Result<Response, CqlResponseParseError> {
let buf = &mut &*buf_bytes;
let response = match opcode {
ResponseOpcode::Error => Response::Error(Error::deserialize(features, buf)?),
ResponseOpcode::Ready => Response::Ready,
ResponseOpcode::Authenticate => {
Response::Authenticate(authenticate::Authenticate::deserialize(buf)?)
}
ResponseOpcode::Supported => Response::Supported(Supported::deserialize(buf)?),
ResponseOpcode::Result => Response::Result(result::deserialize_with_features(
buf_bytes,
cached_metadata,
features,
)?),
ResponseOpcode::Event => Response::Event(event::Event::deserialize(buf)?),
ResponseOpcode::AuthChallenge => {
Response::AuthChallenge(authenticate::AuthChallenge::deserialize(buf)?)
}
ResponseOpcode::AuthSuccess => {
Response::AuthSuccess(authenticate::AuthSuccess::deserialize(buf)?)
}
};
Ok(response)
}
pub fn deserialize_metadata(
self,
) -> Result<ResponseWithDeserializedMetadata, ResultMetadataAndRowsCountParseError> {
let result = match self {
Self::Error(e) => ResponseWithDeserializedMetadata::Error(e),
Self::Ready => ResponseWithDeserializedMetadata::Ready,
Self::Result(res) => {
ResponseWithDeserializedMetadata::Result(res.deserialize_metadata()?)
}
Self::Authenticate(auth) => ResponseWithDeserializedMetadata::Authenticate(auth),
Self::AuthSuccess(auth_succ) => {
ResponseWithDeserializedMetadata::AuthSuccess(auth_succ)
}
Self::AuthChallenge(auth_chal) => {
ResponseWithDeserializedMetadata::AuthChallenge(auth_chal)
}
Self::Supported(sup) => ResponseWithDeserializedMetadata::Supported(sup),
Self::Event(eve) => ResponseWithDeserializedMetadata::Event(eve),
};
Ok(result)
}
pub fn into_non_error_response(self) -> Result<NonErrorResponse, error::Error> {
let non_error_response = match self {
Response::Error(e) => return Err(e),
Response::Ready => NonErrorResponse::Ready,
Response::Result(res) => NonErrorResponse::Result(res),
Response::Authenticate(auth) => NonErrorResponse::Authenticate(auth),
Response::AuthSuccess(auth_succ) => NonErrorResponse::AuthSuccess(auth_succ),
Response::AuthChallenge(auth_chal) => NonErrorResponse::AuthChallenge(auth_chal),
Response::Supported(sup) => NonErrorResponse::Supported(sup),
Response::Event(eve) => NonErrorResponse::Event(eve),
};
Ok(non_error_response)
}
}
impl ResponseV2 {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
Self::Error(_) => CqlResponseKind::Error,
Self::Ready => CqlResponseKind::Ready,
Self::Result(_) => CqlResponseKind::Result,
Self::Authenticate(_) => CqlResponseKind::Authenticate,
Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
Self::Supported(_) => CqlResponseKind::Supported,
Self::Event(_) => CqlResponseKind::Event,
}
}
pub fn deserialize(
features: &ProtocolFeatures,
opcode: ResponseOpcode,
buf_bytes: bytes::Bytes,
cached_metadata: Option<&Arc<ResultMetadata<'static>>>,
) -> Result<Self, CqlResponseParseError> {
let buf = &mut &*buf_bytes;
let response = match opcode {
ResponseOpcode::Error => Self::Error(Error::deserialize(features, buf)?),
ResponseOpcode::Ready => Self::Ready,
ResponseOpcode::Authenticate => {
Self::Authenticate(authenticate::Authenticate::deserialize(buf)?)
}
ResponseOpcode::Supported => Self::Supported(Supported::deserialize(buf)?),
ResponseOpcode::Result => Self::Result(result::deserialize_with_features(
buf_bytes,
cached_metadata,
features,
)?),
ResponseOpcode::Event => Self::Event(event::EventV2::deserialize(buf)?),
ResponseOpcode::AuthChallenge => {
Self::AuthChallenge(authenticate::AuthChallenge::deserialize(buf)?)
}
ResponseOpcode::AuthSuccess => {
Self::AuthSuccess(authenticate::AuthSuccess::deserialize(buf)?)
}
};
Ok(response)
}
pub fn deserialize_metadata(
self,
) -> Result<ResponseWithDeserializedMetadataV2, ResultMetadataAndRowsCountParseError> {
let result = match self {
Self::Error(e) => ResponseWithDeserializedMetadataV2::Error(e),
Self::Ready => ResponseWithDeserializedMetadataV2::Ready,
Self::Result(res) => {
ResponseWithDeserializedMetadataV2::Result(res.deserialize_metadata()?)
}
Self::Authenticate(auth) => ResponseWithDeserializedMetadataV2::Authenticate(auth),
Self::AuthSuccess(auth_succ) => {
ResponseWithDeserializedMetadataV2::AuthSuccess(auth_succ)
}
Self::AuthChallenge(auth_chal) => {
ResponseWithDeserializedMetadataV2::AuthChallenge(auth_chal)
}
Self::Supported(sup) => ResponseWithDeserializedMetadataV2::Supported(sup),
Self::Event(eve) => ResponseWithDeserializedMetadataV2::Event(eve),
};
Ok(result)
}
}
#[derive(Debug)]
pub enum ResponseWithDeserializedMetadata {
Error(Error),
Ready,
Result(result::ResultWithDeserializedMetadata),
Authenticate(authenticate::Authenticate),
AuthSuccess(authenticate::AuthSuccess),
AuthChallenge(authenticate::AuthChallenge),
Supported(Supported),
Event(event::Event),
}
impl ResponseWithDeserializedMetadata {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
Self::Error(_) => CqlResponseKind::Error,
Self::Ready => CqlResponseKind::Ready,
Self::Result(_) => CqlResponseKind::Result,
Self::Authenticate(_) => CqlResponseKind::Authenticate,
Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
Self::Supported(_) => CqlResponseKind::Supported,
Self::Event(_) => CqlResponseKind::Event,
}
}
pub fn into_non_error_response(
self,
) -> Result<NonErrorResponseWithDeserializedMetadata, error::Error> {
let non_error_response = match self {
Self::Error(e) => return Err(e),
Self::Ready => NonErrorResponseWithDeserializedMetadata::Ready,
Self::Result(res) => NonErrorResponseWithDeserializedMetadata::Result(res),
Self::Authenticate(auth) => {
NonErrorResponseWithDeserializedMetadata::Authenticate(auth)
}
Self::AuthSuccess(auth_succ) => {
NonErrorResponseWithDeserializedMetadata::AuthSuccess(auth_succ)
}
Self::AuthChallenge(auth_chal) => {
NonErrorResponseWithDeserializedMetadata::AuthChallenge(auth_chal)
}
Self::Supported(sup) => NonErrorResponseWithDeserializedMetadata::Supported(sup),
Self::Event(eve) => NonErrorResponseWithDeserializedMetadata::Event(eve),
};
Ok(non_error_response)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ResponseWithDeserializedMetadataV2 {
Error(Error),
Ready,
Result(result::ResultWithDeserializedMetadata),
Authenticate(authenticate::Authenticate),
AuthSuccess(authenticate::AuthSuccess),
AuthChallenge(authenticate::AuthChallenge),
Supported(Supported),
Event(event::EventV2),
}
impl ResponseWithDeserializedMetadataV2 {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
Self::Error(_) => CqlResponseKind::Error,
Self::Ready => CqlResponseKind::Ready,
Self::Result(_) => CqlResponseKind::Result,
Self::Authenticate(_) => CqlResponseKind::Authenticate,
Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
Self::Supported(_) => CqlResponseKind::Supported,
Self::Event(_) => CqlResponseKind::Event,
}
}
pub fn into_non_error_response(
self,
) -> Result<NonErrorResponseWithDeserializedMetadataV2, error::Error> {
let non_error_response = match self {
Self::Error(e) => return Err(e),
Self::Ready => NonErrorResponseWithDeserializedMetadataV2::Ready,
Self::Result(res) => NonErrorResponseWithDeserializedMetadataV2::Result(res),
Self::Authenticate(auth) => {
NonErrorResponseWithDeserializedMetadataV2::Authenticate(auth)
}
Self::AuthSuccess(auth_succ) => {
NonErrorResponseWithDeserializedMetadataV2::AuthSuccess(auth_succ)
}
Self::AuthChallenge(auth_chal) => {
NonErrorResponseWithDeserializedMetadataV2::AuthChallenge(auth_chal)
}
Self::Supported(sup) => NonErrorResponseWithDeserializedMetadataV2::Supported(sup),
Self::Event(eve) => NonErrorResponseWithDeserializedMetadataV2::Event(eve),
};
Ok(non_error_response)
}
}
#[derive(Debug)]
pub enum NonErrorResponse {
Ready,
Result(result::Result),
Authenticate(authenticate::Authenticate),
AuthSuccess(authenticate::AuthSuccess),
AuthChallenge(authenticate::AuthChallenge),
Supported(Supported),
Event(event::Event),
}
impl NonErrorResponse {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
NonErrorResponse::Ready => CqlResponseKind::Ready,
NonErrorResponse::Result(_) => CqlResponseKind::Result,
NonErrorResponse::Authenticate(_) => CqlResponseKind::Authenticate,
NonErrorResponse::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
NonErrorResponse::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
NonErrorResponse::Supported(_) => CqlResponseKind::Supported,
NonErrorResponse::Event(_) => CqlResponseKind::Event,
}
}
}
#[derive(Debug)]
pub enum NonErrorResponseWithDeserializedMetadata {
Ready,
Result(result::ResultWithDeserializedMetadata),
Authenticate(authenticate::Authenticate),
AuthSuccess(authenticate::AuthSuccess),
AuthChallenge(authenticate::AuthChallenge),
Supported(Supported),
Event(event::Event),
}
impl NonErrorResponseWithDeserializedMetadata {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
Self::Ready => CqlResponseKind::Ready,
Self::Result(_) => CqlResponseKind::Result,
Self::Authenticate(_) => CqlResponseKind::Authenticate,
Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
Self::Supported(_) => CqlResponseKind::Supported,
Self::Event(_) => CqlResponseKind::Event,
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum NonErrorResponseWithDeserializedMetadataV2 {
Ready,
Result(result::ResultWithDeserializedMetadata),
Authenticate(authenticate::Authenticate),
AuthSuccess(authenticate::AuthSuccess),
AuthChallenge(authenticate::AuthChallenge),
Supported(Supported),
Event(event::EventV2),
}
impl NonErrorResponseWithDeserializedMetadataV2 {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
Self::Ready => CqlResponseKind::Ready,
Self::Result(_) => CqlResponseKind::Result,
Self::Authenticate(_) => CqlResponseKind::Authenticate,
Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
Self::Supported(_) => CqlResponseKind::Supported,
Self::Event(_) => CqlResponseKind::Event,
}
}
}