scylla_cql/frame/response/
mod.rs

1pub mod authenticate;
2pub mod error;
3pub mod event;
4pub mod result;
5pub mod supported;
6
7use std::sync::Arc;
8
9pub use error::Error;
10pub use supported::Supported;
11
12use crate::frame::protocol_features::ProtocolFeatures;
13use crate::frame::response::result::ResultMetadata;
14use crate::frame::TryFromPrimitiveError;
15
16use super::frame_errors::CqlResponseParseError;
17
18/// Possible CQL responses received from the server
19#[derive(Debug, Copy, Clone)]
20#[non_exhaustive]
21pub enum CqlResponseKind {
22    Error,
23    Ready,
24    Authenticate,
25    Supported,
26    Result,
27    Event,
28    AuthChallenge,
29    AuthSuccess,
30}
31
32impl std::fmt::Display for CqlResponseKind {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        let kind_str = match self {
35            CqlResponseKind::Error => "ERROR",
36            CqlResponseKind::Ready => "READY",
37            CqlResponseKind::Authenticate => "AUTHENTICATE",
38            CqlResponseKind::Supported => "SUPPORTED",
39            CqlResponseKind::Result => "RESULT",
40            CqlResponseKind::Event => "EVENT",
41            CqlResponseKind::AuthChallenge => "AUTH_CHALLENGE",
42            CqlResponseKind::AuthSuccess => "AUTH_SUCCESS",
43        };
44
45        f.write_str(kind_str)
46    }
47}
48
49#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
50#[repr(u8)]
51pub enum ResponseOpcode {
52    Error = 0x00,
53    Ready = 0x02,
54    Authenticate = 0x03,
55    Supported = 0x06,
56    Result = 0x08,
57    Event = 0x0C,
58    AuthChallenge = 0x0E,
59    AuthSuccess = 0x10,
60}
61
62impl TryFrom<u8> for ResponseOpcode {
63    type Error = TryFromPrimitiveError<u8>;
64
65    fn try_from(value: u8) -> Result<Self, TryFromPrimitiveError<u8>> {
66        match value {
67            0x00 => Ok(Self::Error),
68            0x02 => Ok(Self::Ready),
69            0x03 => Ok(Self::Authenticate),
70            0x06 => Ok(Self::Supported),
71            0x08 => Ok(Self::Result),
72            0x0C => Ok(Self::Event),
73            0x0E => Ok(Self::AuthChallenge),
74            0x10 => Ok(Self::AuthSuccess),
75            _ => Err(TryFromPrimitiveError {
76                enum_name: "ResponseOpcode",
77                primitive: value,
78            }),
79        }
80    }
81}
82
83#[derive(Debug)]
84pub enum Response {
85    Error(Error),
86    Ready,
87    Result(result::Result),
88    Authenticate(authenticate::Authenticate),
89    AuthSuccess(authenticate::AuthSuccess),
90    AuthChallenge(authenticate::AuthChallenge),
91    Supported(Supported),
92    Event(event::Event),
93}
94
95impl Response {
96    pub fn to_response_kind(&self) -> CqlResponseKind {
97        match self {
98            Response::Error(_) => CqlResponseKind::Error,
99            Response::Ready => CqlResponseKind::Ready,
100            Response::Result(_) => CqlResponseKind::Result,
101            Response::Authenticate(_) => CqlResponseKind::Authenticate,
102            Response::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
103            Response::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
104            Response::Supported(_) => CqlResponseKind::Supported,
105            Response::Event(_) => CqlResponseKind::Event,
106        }
107    }
108
109    pub fn deserialize(
110        features: &ProtocolFeatures,
111        opcode: ResponseOpcode,
112        buf_bytes: bytes::Bytes,
113        cached_metadata: Option<&Arc<ResultMetadata<'static>>>,
114    ) -> Result<Response, CqlResponseParseError> {
115        let buf = &mut &*buf_bytes;
116        let response = match opcode {
117            ResponseOpcode::Error => Response::Error(Error::deserialize(features, buf)?),
118            ResponseOpcode::Ready => Response::Ready,
119            ResponseOpcode::Authenticate => {
120                Response::Authenticate(authenticate::Authenticate::deserialize(buf)?)
121            }
122            ResponseOpcode::Supported => Response::Supported(Supported::deserialize(buf)?),
123            ResponseOpcode::Result => {
124                Response::Result(result::deserialize(buf_bytes, cached_metadata)?)
125            }
126            ResponseOpcode::Event => Response::Event(event::Event::deserialize(buf)?),
127            ResponseOpcode::AuthChallenge => {
128                Response::AuthChallenge(authenticate::AuthChallenge::deserialize(buf)?)
129            }
130            ResponseOpcode::AuthSuccess => {
131                Response::AuthSuccess(authenticate::AuthSuccess::deserialize(buf)?)
132            }
133        };
134
135        Ok(response)
136    }
137
138    pub fn into_non_error_response(self) -> Result<NonErrorResponse, error::Error> {
139        let non_error_response = match self {
140            Response::Error(e) => return Err(e),
141            Response::Ready => NonErrorResponse::Ready,
142            Response::Result(res) => NonErrorResponse::Result(res),
143            Response::Authenticate(auth) => NonErrorResponse::Authenticate(auth),
144            Response::AuthSuccess(auth_succ) => NonErrorResponse::AuthSuccess(auth_succ),
145            Response::AuthChallenge(auth_chal) => NonErrorResponse::AuthChallenge(auth_chal),
146            Response::Supported(sup) => NonErrorResponse::Supported(sup),
147            Response::Event(eve) => NonErrorResponse::Event(eve),
148        };
149
150        Ok(non_error_response)
151    }
152}
153
154// A Response which can not be Response::Error
155#[derive(Debug)]
156pub enum NonErrorResponse {
157    Ready,
158    Result(result::Result),
159    Authenticate(authenticate::Authenticate),
160    AuthSuccess(authenticate::AuthSuccess),
161    AuthChallenge(authenticate::AuthChallenge),
162    Supported(Supported),
163    Event(event::Event),
164}
165
166impl NonErrorResponse {
167    pub fn to_response_kind(&self) -> CqlResponseKind {
168        match self {
169            NonErrorResponse::Ready => CqlResponseKind::Ready,
170            NonErrorResponse::Result(_) => CqlResponseKind::Result,
171            NonErrorResponse::Authenticate(_) => CqlResponseKind::Authenticate,
172            NonErrorResponse::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
173            NonErrorResponse::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
174            NonErrorResponse::Supported(_) => CqlResponseKind::Supported,
175            NonErrorResponse::Event(_) => CqlResponseKind::Event,
176        }
177    }
178}