1pub mod authenticate;
4pub mod custom_type_parser;
5pub use scylla_cql_core::frame::response::error;
6pub mod event;
7pub mod result;
8pub mod supported;
9
10use std::sync::Arc;
11
12pub use error::Error;
13pub use scylla_cql_core::frame::response::CqlResponseKind;
14pub use supported::Supported;
15
16use crate::frame::TryFromPrimitiveError;
17use crate::frame::frame_errors::ResultMetadataAndRowsCountParseError;
18use crate::frame::protocol_features::ProtocolFeatures;
19use crate::frame::response::result::ResultMetadata;
20
21use super::frame_errors::CqlResponseParseError;
22
23#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
25#[repr(u8)]
26pub enum ResponseOpcode {
27 Error = 0x00,
29 Ready = 0x02,
31 Authenticate = 0x03,
33 Supported = 0x06,
35 Result = 0x08,
37 Event = 0x0C,
39 AuthChallenge = 0x0E,
41 AuthSuccess = 0x10,
43}
44
45impl TryFrom<u8> for ResponseOpcode {
46 type Error = TryFromPrimitiveError<u8>;
47
48 fn try_from(value: u8) -> Result<Self, TryFromPrimitiveError<u8>> {
49 match value {
50 0x00 => Ok(Self::Error),
51 0x02 => Ok(Self::Ready),
52 0x03 => Ok(Self::Authenticate),
53 0x06 => Ok(Self::Supported),
54 0x08 => Ok(Self::Result),
55 0x0C => Ok(Self::Event),
56 0x0E => Ok(Self::AuthChallenge),
57 0x10 => Ok(Self::AuthSuccess),
58 _ => Err(TryFromPrimitiveError::new("ResponseOpcode", value)),
59 }
60 }
61}
62
63#[derive(Debug)]
65pub enum Response {
66 Error(Error),
68 Ready,
71 Result(result::Result),
73 Authenticate(authenticate::Authenticate),
75 AuthSuccess(authenticate::AuthSuccess),
77 AuthChallenge(authenticate::AuthChallenge),
79 Supported(Supported),
81 Event(event::Event),
83}
84
85#[derive(Debug)]
87#[non_exhaustive]
88pub enum ResponseV2 {
89 Error(Error),
91 Ready,
94 Result(result::Result),
96 Authenticate(authenticate::Authenticate),
98 AuthSuccess(authenticate::AuthSuccess),
100 AuthChallenge(authenticate::AuthChallenge),
102 Supported(Supported),
104 Event(event::EventV2),
106}
107
108impl Response {
109 pub fn to_response_kind(&self) -> CqlResponseKind {
111 match self {
112 Response::Error(_) => CqlResponseKind::Error,
113 Response::Ready => CqlResponseKind::Ready,
114 Response::Result(_) => CqlResponseKind::Result,
115 Response::Authenticate(_) => CqlResponseKind::Authenticate,
116 Response::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
117 Response::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
118 Response::Supported(_) => CqlResponseKind::Supported,
119 Response::Event(_) => CqlResponseKind::Event,
120 }
121 }
122
123 pub fn deserialize(
125 features: &ProtocolFeatures,
126 opcode: ResponseOpcode,
127 buf_bytes: bytes::Bytes,
128 cached_metadata: Option<&Arc<ResultMetadata<'static>>>,
129 ) -> Result<Response, CqlResponseParseError> {
130 let buf = &mut &*buf_bytes;
131 let response = match opcode {
132 ResponseOpcode::Error => Response::Error(Error::deserialize(features, buf)?),
133 ResponseOpcode::Ready => Response::Ready,
134 ResponseOpcode::Authenticate => {
135 Response::Authenticate(authenticate::Authenticate::deserialize(buf)?)
136 }
137 ResponseOpcode::Supported => Response::Supported(Supported::deserialize(buf)?),
138 ResponseOpcode::Result => Response::Result(result::deserialize_with_features(
139 buf_bytes,
140 cached_metadata,
141 features,
142 )?),
143 ResponseOpcode::Event => Response::Event(event::Event::deserialize(buf)?),
144 ResponseOpcode::AuthChallenge => {
145 Response::AuthChallenge(authenticate::AuthChallenge::deserialize(buf)?)
146 }
147 ResponseOpcode::AuthSuccess => {
148 Response::AuthSuccess(authenticate::AuthSuccess::deserialize(buf)?)
149 }
150 };
151
152 Ok(response)
153 }
154
155 pub fn deserialize_metadata(
156 self,
157 ) -> Result<ResponseWithDeserializedMetadata, ResultMetadataAndRowsCountParseError> {
158 let result = match self {
159 Self::Error(e) => ResponseWithDeserializedMetadata::Error(e),
160 Self::Ready => ResponseWithDeserializedMetadata::Ready,
161 Self::Result(res) => {
162 ResponseWithDeserializedMetadata::Result(res.deserialize_metadata()?)
163 }
164 Self::Authenticate(auth) => ResponseWithDeserializedMetadata::Authenticate(auth),
165 Self::AuthSuccess(auth_succ) => {
166 ResponseWithDeserializedMetadata::AuthSuccess(auth_succ)
167 }
168 Self::AuthChallenge(auth_chal) => {
169 ResponseWithDeserializedMetadata::AuthChallenge(auth_chal)
170 }
171 Self::Supported(sup) => ResponseWithDeserializedMetadata::Supported(sup),
172 Self::Event(eve) => ResponseWithDeserializedMetadata::Event(eve),
173 };
174 Ok(result)
175 }
176
177 pub fn into_non_error_response(self) -> Result<NonErrorResponse, error::Error> {
179 let non_error_response = match self {
180 Response::Error(e) => return Err(e),
181 Response::Ready => NonErrorResponse::Ready,
182 Response::Result(res) => NonErrorResponse::Result(res),
183 Response::Authenticate(auth) => NonErrorResponse::Authenticate(auth),
184 Response::AuthSuccess(auth_succ) => NonErrorResponse::AuthSuccess(auth_succ),
185 Response::AuthChallenge(auth_chal) => NonErrorResponse::AuthChallenge(auth_chal),
186 Response::Supported(sup) => NonErrorResponse::Supported(sup),
187 Response::Event(eve) => NonErrorResponse::Event(eve),
188 };
189
190 Ok(non_error_response)
191 }
192}
193
194impl ResponseV2 {
195 pub fn to_response_kind(&self) -> CqlResponseKind {
197 match self {
198 Self::Error(_) => CqlResponseKind::Error,
199 Self::Ready => CqlResponseKind::Ready,
200 Self::Result(_) => CqlResponseKind::Result,
201 Self::Authenticate(_) => CqlResponseKind::Authenticate,
202 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
203 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
204 Self::Supported(_) => CqlResponseKind::Supported,
205 Self::Event(_) => CqlResponseKind::Event,
206 }
207 }
208
209 pub fn deserialize(
211 features: &ProtocolFeatures,
212 opcode: ResponseOpcode,
213 buf_bytes: bytes::Bytes,
214 cached_metadata: Option<&Arc<ResultMetadata<'static>>>,
215 ) -> Result<Self, CqlResponseParseError> {
216 let buf = &mut &*buf_bytes;
217 let response = match opcode {
218 ResponseOpcode::Error => Self::Error(Error::deserialize(features, buf)?),
219 ResponseOpcode::Ready => Self::Ready,
220 ResponseOpcode::Authenticate => {
221 Self::Authenticate(authenticate::Authenticate::deserialize(buf)?)
222 }
223 ResponseOpcode::Supported => Self::Supported(Supported::deserialize(buf)?),
224 ResponseOpcode::Result => Self::Result(result::deserialize_with_features(
225 buf_bytes,
226 cached_metadata,
227 features,
228 )?),
229 ResponseOpcode::Event => Self::Event(event::EventV2::deserialize(buf)?),
230 ResponseOpcode::AuthChallenge => {
231 Self::AuthChallenge(authenticate::AuthChallenge::deserialize(buf)?)
232 }
233 ResponseOpcode::AuthSuccess => {
234 Self::AuthSuccess(authenticate::AuthSuccess::deserialize(buf)?)
235 }
236 };
237
238 Ok(response)
239 }
240
241 pub fn deserialize_metadata(
242 self,
243 ) -> Result<ResponseWithDeserializedMetadataV2, ResultMetadataAndRowsCountParseError> {
244 let result = match self {
245 Self::Error(e) => ResponseWithDeserializedMetadataV2::Error(e),
246 Self::Ready => ResponseWithDeserializedMetadataV2::Ready,
247 Self::Result(res) => {
248 ResponseWithDeserializedMetadataV2::Result(res.deserialize_metadata()?)
249 }
250 Self::Authenticate(auth) => ResponseWithDeserializedMetadataV2::Authenticate(auth),
251 Self::AuthSuccess(auth_succ) => {
252 ResponseWithDeserializedMetadataV2::AuthSuccess(auth_succ)
253 }
254 Self::AuthChallenge(auth_chal) => {
255 ResponseWithDeserializedMetadataV2::AuthChallenge(auth_chal)
256 }
257 Self::Supported(sup) => ResponseWithDeserializedMetadataV2::Supported(sup),
258 Self::Event(eve) => ResponseWithDeserializedMetadataV2::Event(eve),
259 };
260 Ok(result)
261 }
262}
263
264#[derive(Debug)]
266pub enum ResponseWithDeserializedMetadata {
267 Error(Error),
269 Ready,
272 Result(result::ResultWithDeserializedMetadata),
274 Authenticate(authenticate::Authenticate),
276 AuthSuccess(authenticate::AuthSuccess),
278 AuthChallenge(authenticate::AuthChallenge),
280 Supported(Supported),
282 Event(event::Event),
284}
285
286impl ResponseWithDeserializedMetadata {
287 pub fn to_response_kind(&self) -> CqlResponseKind {
289 match self {
290 Self::Error(_) => CqlResponseKind::Error,
291 Self::Ready => CqlResponseKind::Ready,
292 Self::Result(_) => CqlResponseKind::Result,
293 Self::Authenticate(_) => CqlResponseKind::Authenticate,
294 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
295 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
296 Self::Supported(_) => CqlResponseKind::Supported,
297 Self::Event(_) => CqlResponseKind::Event,
298 }
299 }
300
301 pub fn into_non_error_response(
303 self,
304 ) -> Result<NonErrorResponseWithDeserializedMetadata, error::Error> {
305 let non_error_response = match self {
306 Self::Error(e) => return Err(e),
307 Self::Ready => NonErrorResponseWithDeserializedMetadata::Ready,
308 Self::Result(res) => NonErrorResponseWithDeserializedMetadata::Result(res),
309 Self::Authenticate(auth) => {
310 NonErrorResponseWithDeserializedMetadata::Authenticate(auth)
311 }
312 Self::AuthSuccess(auth_succ) => {
313 NonErrorResponseWithDeserializedMetadata::AuthSuccess(auth_succ)
314 }
315 Self::AuthChallenge(auth_chal) => {
316 NonErrorResponseWithDeserializedMetadata::AuthChallenge(auth_chal)
317 }
318 Self::Supported(sup) => NonErrorResponseWithDeserializedMetadata::Supported(sup),
319 Self::Event(eve) => NonErrorResponseWithDeserializedMetadata::Event(eve),
320 };
321
322 Ok(non_error_response)
323 }
324}
325
326#[derive(Debug)]
328#[non_exhaustive]
329pub enum ResponseWithDeserializedMetadataV2 {
330 Error(Error),
332 Ready,
335 Result(result::ResultWithDeserializedMetadata),
337 Authenticate(authenticate::Authenticate),
339 AuthSuccess(authenticate::AuthSuccess),
341 AuthChallenge(authenticate::AuthChallenge),
343 Supported(Supported),
345 Event(event::EventV2),
347}
348
349impl ResponseWithDeserializedMetadataV2 {
350 pub fn to_response_kind(&self) -> CqlResponseKind {
352 match self {
353 Self::Error(_) => CqlResponseKind::Error,
354 Self::Ready => CqlResponseKind::Ready,
355 Self::Result(_) => CqlResponseKind::Result,
356 Self::Authenticate(_) => CqlResponseKind::Authenticate,
357 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
358 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
359 Self::Supported(_) => CqlResponseKind::Supported,
360 Self::Event(_) => CqlResponseKind::Event,
361 }
362 }
363
364 pub fn into_non_error_response(
366 self,
367 ) -> Result<NonErrorResponseWithDeserializedMetadataV2, error::Error> {
368 let non_error_response = match self {
369 Self::Error(e) => return Err(e),
370 Self::Ready => NonErrorResponseWithDeserializedMetadataV2::Ready,
371 Self::Result(res) => NonErrorResponseWithDeserializedMetadataV2::Result(res),
372 Self::Authenticate(auth) => {
373 NonErrorResponseWithDeserializedMetadataV2::Authenticate(auth)
374 }
375 Self::AuthSuccess(auth_succ) => {
376 NonErrorResponseWithDeserializedMetadataV2::AuthSuccess(auth_succ)
377 }
378 Self::AuthChallenge(auth_chal) => {
379 NonErrorResponseWithDeserializedMetadataV2::AuthChallenge(auth_chal)
380 }
381 Self::Supported(sup) => NonErrorResponseWithDeserializedMetadataV2::Supported(sup),
382 Self::Event(eve) => NonErrorResponseWithDeserializedMetadataV2::Event(eve),
383 };
384
385 Ok(non_error_response)
386 }
387}
388
389#[derive(Debug)]
393pub enum NonErrorResponse {
394 Ready,
396 Result(result::Result),
398 Authenticate(authenticate::Authenticate),
400 AuthSuccess(authenticate::AuthSuccess),
402 AuthChallenge(authenticate::AuthChallenge),
404 Supported(Supported),
406 Event(event::Event),
408}
409
410impl NonErrorResponse {
411 pub fn to_response_kind(&self) -> CqlResponseKind {
413 match self {
414 NonErrorResponse::Ready => CqlResponseKind::Ready,
415 NonErrorResponse::Result(_) => CqlResponseKind::Result,
416 NonErrorResponse::Authenticate(_) => CqlResponseKind::Authenticate,
417 NonErrorResponse::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
418 NonErrorResponse::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
419 NonErrorResponse::Supported(_) => CqlResponseKind::Supported,
420 NonErrorResponse::Event(_) => CqlResponseKind::Event,
421 }
422 }
423}
424
425#[derive(Debug)]
431pub enum NonErrorResponseWithDeserializedMetadata {
432 Ready,
434 Result(result::ResultWithDeserializedMetadata),
436 Authenticate(authenticate::Authenticate),
438 AuthSuccess(authenticate::AuthSuccess),
440 AuthChallenge(authenticate::AuthChallenge),
442 Supported(Supported),
444 Event(event::Event),
446}
447
448impl NonErrorResponseWithDeserializedMetadata {
449 pub fn to_response_kind(&self) -> CqlResponseKind {
451 match self {
452 Self::Ready => CqlResponseKind::Ready,
453 Self::Result(_) => CqlResponseKind::Result,
454 Self::Authenticate(_) => CqlResponseKind::Authenticate,
455 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
456 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
457 Self::Supported(_) => CqlResponseKind::Supported,
458 Self::Event(_) => CqlResponseKind::Event,
459 }
460 }
461}
462
463#[derive(Debug)]
469#[non_exhaustive]
470pub enum NonErrorResponseWithDeserializedMetadataV2 {
471 Ready,
473 Result(result::ResultWithDeserializedMetadata),
475 Authenticate(authenticate::Authenticate),
477 AuthSuccess(authenticate::AuthSuccess),
479 AuthChallenge(authenticate::AuthChallenge),
481 Supported(Supported),
483 Event(event::EventV2),
485}
486
487impl NonErrorResponseWithDeserializedMetadataV2 {
488 pub fn to_response_kind(&self) -> CqlResponseKind {
490 match self {
491 Self::Ready => CqlResponseKind::Ready,
492 Self::Result(_) => CqlResponseKind::Result,
493 Self::Authenticate(_) => CqlResponseKind::Authenticate,
494 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
495 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
496 Self::Supported(_) => CqlResponseKind::Supported,
497 Self::Event(_) => CqlResponseKind::Event,
498 }
499 }
500}