1pub mod authenticate;
4pub mod custom_type_parser;
5pub mod error;
6pub mod event;
7pub mod result;
8pub mod supported;
9
10use std::sync::Arc;
11
12pub use error::Error;
13pub use supported::Supported;
14
15use crate::frame::TryFromPrimitiveError;
16use crate::frame::frame_errors::ResultMetadataAndRowsCountParseError;
17use crate::frame::protocol_features::ProtocolFeatures;
18use crate::frame::response::result::ResultMetadata;
19
20use super::frame_errors::CqlResponseParseError;
21
22#[derive(Debug, Copy, Clone)]
26#[non_exhaustive]
27pub enum CqlResponseKind {
28 Error,
30
31 Ready,
36
37 Authenticate,
50
51 Supported,
54
55 Result,
63
64 Event,
75
76 AuthChallenge,
80
81 AuthSuccess,
83}
84
85impl std::fmt::Display for CqlResponseKind {
86 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87 let kind_str = match self {
88 CqlResponseKind::Error => "ERROR",
89 CqlResponseKind::Ready => "READY",
90 CqlResponseKind::Authenticate => "AUTHENTICATE",
91 CqlResponseKind::Supported => "SUPPORTED",
92 CqlResponseKind::Result => "RESULT",
93 CqlResponseKind::Event => "EVENT",
94 CqlResponseKind::AuthChallenge => "AUTH_CHALLENGE",
95 CqlResponseKind::AuthSuccess => "AUTH_SUCCESS",
96 };
97
98 f.write_str(kind_str)
99 }
100}
101
102#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
104#[repr(u8)]
105pub enum ResponseOpcode {
106 Error = 0x00,
108 Ready = 0x02,
110 Authenticate = 0x03,
112 Supported = 0x06,
114 Result = 0x08,
116 Event = 0x0C,
118 AuthChallenge = 0x0E,
120 AuthSuccess = 0x10,
122}
123
124impl TryFrom<u8> for ResponseOpcode {
125 type Error = TryFromPrimitiveError<u8>;
126
127 fn try_from(value: u8) -> Result<Self, TryFromPrimitiveError<u8>> {
128 match value {
129 0x00 => Ok(Self::Error),
130 0x02 => Ok(Self::Ready),
131 0x03 => Ok(Self::Authenticate),
132 0x06 => Ok(Self::Supported),
133 0x08 => Ok(Self::Result),
134 0x0C => Ok(Self::Event),
135 0x0E => Ok(Self::AuthChallenge),
136 0x10 => Ok(Self::AuthSuccess),
137 _ => Err(TryFromPrimitiveError {
138 enum_name: "ResponseOpcode",
139 primitive: value,
140 }),
141 }
142 }
143}
144
145#[derive(Debug)]
147pub enum Response {
148 Error(Error),
150 Ready,
153 Result(result::Result),
155 Authenticate(authenticate::Authenticate),
157 AuthSuccess(authenticate::AuthSuccess),
159 AuthChallenge(authenticate::AuthChallenge),
161 Supported(Supported),
163 Event(event::Event),
165}
166
167#[derive(Debug)]
169#[non_exhaustive]
170pub enum ResponseV2 {
171 Error(Error),
173 Ready,
176 Result(result::Result),
178 Authenticate(authenticate::Authenticate),
180 AuthSuccess(authenticate::AuthSuccess),
182 AuthChallenge(authenticate::AuthChallenge),
184 Supported(Supported),
186 Event(event::EventV2),
188}
189
190impl Response {
191 pub fn to_response_kind(&self) -> CqlResponseKind {
193 match self {
194 Response::Error(_) => CqlResponseKind::Error,
195 Response::Ready => CqlResponseKind::Ready,
196 Response::Result(_) => CqlResponseKind::Result,
197 Response::Authenticate(_) => CqlResponseKind::Authenticate,
198 Response::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
199 Response::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
200 Response::Supported(_) => CqlResponseKind::Supported,
201 Response::Event(_) => CqlResponseKind::Event,
202 }
203 }
204
205 pub fn deserialize(
207 features: &ProtocolFeatures,
208 opcode: ResponseOpcode,
209 buf_bytes: bytes::Bytes,
210 cached_metadata: Option<&Arc<ResultMetadata<'static>>>,
211 ) -> Result<Response, CqlResponseParseError> {
212 let buf = &mut &*buf_bytes;
213 let response = match opcode {
214 ResponseOpcode::Error => Response::Error(Error::deserialize(features, buf)?),
215 ResponseOpcode::Ready => Response::Ready,
216 ResponseOpcode::Authenticate => {
217 Response::Authenticate(authenticate::Authenticate::deserialize(buf)?)
218 }
219 ResponseOpcode::Supported => Response::Supported(Supported::deserialize(buf)?),
220 ResponseOpcode::Result => Response::Result(result::deserialize_with_features(
221 buf_bytes,
222 cached_metadata,
223 features,
224 )?),
225 ResponseOpcode::Event => Response::Event(event::Event::deserialize(buf)?),
226 ResponseOpcode::AuthChallenge => {
227 Response::AuthChallenge(authenticate::AuthChallenge::deserialize(buf)?)
228 }
229 ResponseOpcode::AuthSuccess => {
230 Response::AuthSuccess(authenticate::AuthSuccess::deserialize(buf)?)
231 }
232 };
233
234 Ok(response)
235 }
236
237 pub fn deserialize_metadata(
238 self,
239 ) -> Result<ResponseWithDeserializedMetadata, ResultMetadataAndRowsCountParseError> {
240 let result = match self {
241 Self::Error(e) => ResponseWithDeserializedMetadata::Error(e),
242 Self::Ready => ResponseWithDeserializedMetadata::Ready,
243 Self::Result(res) => {
244 ResponseWithDeserializedMetadata::Result(res.deserialize_metadata()?)
245 }
246 Self::Authenticate(auth) => ResponseWithDeserializedMetadata::Authenticate(auth),
247 Self::AuthSuccess(auth_succ) => {
248 ResponseWithDeserializedMetadata::AuthSuccess(auth_succ)
249 }
250 Self::AuthChallenge(auth_chal) => {
251 ResponseWithDeserializedMetadata::AuthChallenge(auth_chal)
252 }
253 Self::Supported(sup) => ResponseWithDeserializedMetadata::Supported(sup),
254 Self::Event(eve) => ResponseWithDeserializedMetadata::Event(eve),
255 };
256 Ok(result)
257 }
258
259 pub fn into_non_error_response(self) -> Result<NonErrorResponse, error::Error> {
261 let non_error_response = match self {
262 Response::Error(e) => return Err(e),
263 Response::Ready => NonErrorResponse::Ready,
264 Response::Result(res) => NonErrorResponse::Result(res),
265 Response::Authenticate(auth) => NonErrorResponse::Authenticate(auth),
266 Response::AuthSuccess(auth_succ) => NonErrorResponse::AuthSuccess(auth_succ),
267 Response::AuthChallenge(auth_chal) => NonErrorResponse::AuthChallenge(auth_chal),
268 Response::Supported(sup) => NonErrorResponse::Supported(sup),
269 Response::Event(eve) => NonErrorResponse::Event(eve),
270 };
271
272 Ok(non_error_response)
273 }
274}
275
276impl ResponseV2 {
277 pub fn to_response_kind(&self) -> CqlResponseKind {
279 match self {
280 Self::Error(_) => CqlResponseKind::Error,
281 Self::Ready => CqlResponseKind::Ready,
282 Self::Result(_) => CqlResponseKind::Result,
283 Self::Authenticate(_) => CqlResponseKind::Authenticate,
284 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
285 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
286 Self::Supported(_) => CqlResponseKind::Supported,
287 Self::Event(_) => CqlResponseKind::Event,
288 }
289 }
290
291 pub fn deserialize(
293 features: &ProtocolFeatures,
294 opcode: ResponseOpcode,
295 buf_bytes: bytes::Bytes,
296 cached_metadata: Option<&Arc<ResultMetadata<'static>>>,
297 ) -> Result<Self, CqlResponseParseError> {
298 let buf = &mut &*buf_bytes;
299 let response = match opcode {
300 ResponseOpcode::Error => Self::Error(Error::deserialize(features, buf)?),
301 ResponseOpcode::Ready => Self::Ready,
302 ResponseOpcode::Authenticate => {
303 Self::Authenticate(authenticate::Authenticate::deserialize(buf)?)
304 }
305 ResponseOpcode::Supported => Self::Supported(Supported::deserialize(buf)?),
306 ResponseOpcode::Result => Self::Result(result::deserialize_with_features(
307 buf_bytes,
308 cached_metadata,
309 features,
310 )?),
311 ResponseOpcode::Event => Self::Event(event::EventV2::deserialize(buf)?),
312 ResponseOpcode::AuthChallenge => {
313 Self::AuthChallenge(authenticate::AuthChallenge::deserialize(buf)?)
314 }
315 ResponseOpcode::AuthSuccess => {
316 Self::AuthSuccess(authenticate::AuthSuccess::deserialize(buf)?)
317 }
318 };
319
320 Ok(response)
321 }
322
323 pub fn deserialize_metadata(
324 self,
325 ) -> Result<ResponseWithDeserializedMetadataV2, ResultMetadataAndRowsCountParseError> {
326 let result = match self {
327 Self::Error(e) => ResponseWithDeserializedMetadataV2::Error(e),
328 Self::Ready => ResponseWithDeserializedMetadataV2::Ready,
329 Self::Result(res) => {
330 ResponseWithDeserializedMetadataV2::Result(res.deserialize_metadata()?)
331 }
332 Self::Authenticate(auth) => ResponseWithDeserializedMetadataV2::Authenticate(auth),
333 Self::AuthSuccess(auth_succ) => {
334 ResponseWithDeserializedMetadataV2::AuthSuccess(auth_succ)
335 }
336 Self::AuthChallenge(auth_chal) => {
337 ResponseWithDeserializedMetadataV2::AuthChallenge(auth_chal)
338 }
339 Self::Supported(sup) => ResponseWithDeserializedMetadataV2::Supported(sup),
340 Self::Event(eve) => ResponseWithDeserializedMetadataV2::Event(eve),
341 };
342 Ok(result)
343 }
344}
345
346#[derive(Debug)]
348pub enum ResponseWithDeserializedMetadata {
349 Error(Error),
351 Ready,
354 Result(result::ResultWithDeserializedMetadata),
356 Authenticate(authenticate::Authenticate),
358 AuthSuccess(authenticate::AuthSuccess),
360 AuthChallenge(authenticate::AuthChallenge),
362 Supported(Supported),
364 Event(event::Event),
366}
367
368impl ResponseWithDeserializedMetadata {
369 pub fn to_response_kind(&self) -> CqlResponseKind {
371 match self {
372 Self::Error(_) => CqlResponseKind::Error,
373 Self::Ready => CqlResponseKind::Ready,
374 Self::Result(_) => CqlResponseKind::Result,
375 Self::Authenticate(_) => CqlResponseKind::Authenticate,
376 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
377 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
378 Self::Supported(_) => CqlResponseKind::Supported,
379 Self::Event(_) => CqlResponseKind::Event,
380 }
381 }
382
383 pub fn into_non_error_response(
385 self,
386 ) -> Result<NonErrorResponseWithDeserializedMetadata, error::Error> {
387 let non_error_response = match self {
388 Self::Error(e) => return Err(e),
389 Self::Ready => NonErrorResponseWithDeserializedMetadata::Ready,
390 Self::Result(res) => NonErrorResponseWithDeserializedMetadata::Result(res),
391 Self::Authenticate(auth) => {
392 NonErrorResponseWithDeserializedMetadata::Authenticate(auth)
393 }
394 Self::AuthSuccess(auth_succ) => {
395 NonErrorResponseWithDeserializedMetadata::AuthSuccess(auth_succ)
396 }
397 Self::AuthChallenge(auth_chal) => {
398 NonErrorResponseWithDeserializedMetadata::AuthChallenge(auth_chal)
399 }
400 Self::Supported(sup) => NonErrorResponseWithDeserializedMetadata::Supported(sup),
401 Self::Event(eve) => NonErrorResponseWithDeserializedMetadata::Event(eve),
402 };
403
404 Ok(non_error_response)
405 }
406}
407
408#[derive(Debug)]
410#[non_exhaustive]
411pub enum ResponseWithDeserializedMetadataV2 {
412 Error(Error),
414 Ready,
417 Result(result::ResultWithDeserializedMetadata),
419 Authenticate(authenticate::Authenticate),
421 AuthSuccess(authenticate::AuthSuccess),
423 AuthChallenge(authenticate::AuthChallenge),
425 Supported(Supported),
427 Event(event::EventV2),
429}
430
431impl ResponseWithDeserializedMetadataV2 {
432 pub fn to_response_kind(&self) -> CqlResponseKind {
434 match self {
435 Self::Error(_) => CqlResponseKind::Error,
436 Self::Ready => CqlResponseKind::Ready,
437 Self::Result(_) => CqlResponseKind::Result,
438 Self::Authenticate(_) => CqlResponseKind::Authenticate,
439 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
440 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
441 Self::Supported(_) => CqlResponseKind::Supported,
442 Self::Event(_) => CqlResponseKind::Event,
443 }
444 }
445
446 pub fn into_non_error_response(
448 self,
449 ) -> Result<NonErrorResponseWithDeserializedMetadataV2, error::Error> {
450 let non_error_response = match self {
451 Self::Error(e) => return Err(e),
452 Self::Ready => NonErrorResponseWithDeserializedMetadataV2::Ready,
453 Self::Result(res) => NonErrorResponseWithDeserializedMetadataV2::Result(res),
454 Self::Authenticate(auth) => {
455 NonErrorResponseWithDeserializedMetadataV2::Authenticate(auth)
456 }
457 Self::AuthSuccess(auth_succ) => {
458 NonErrorResponseWithDeserializedMetadataV2::AuthSuccess(auth_succ)
459 }
460 Self::AuthChallenge(auth_chal) => {
461 NonErrorResponseWithDeserializedMetadataV2::AuthChallenge(auth_chal)
462 }
463 Self::Supported(sup) => NonErrorResponseWithDeserializedMetadataV2::Supported(sup),
464 Self::Event(eve) => NonErrorResponseWithDeserializedMetadataV2::Event(eve),
465 };
466
467 Ok(non_error_response)
468 }
469}
470
471#[derive(Debug)]
475pub enum NonErrorResponse {
476 Ready,
478 Result(result::Result),
480 Authenticate(authenticate::Authenticate),
482 AuthSuccess(authenticate::AuthSuccess),
484 AuthChallenge(authenticate::AuthChallenge),
486 Supported(Supported),
488 Event(event::Event),
490}
491
492impl NonErrorResponse {
493 pub fn to_response_kind(&self) -> CqlResponseKind {
495 match self {
496 NonErrorResponse::Ready => CqlResponseKind::Ready,
497 NonErrorResponse::Result(_) => CqlResponseKind::Result,
498 NonErrorResponse::Authenticate(_) => CqlResponseKind::Authenticate,
499 NonErrorResponse::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
500 NonErrorResponse::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
501 NonErrorResponse::Supported(_) => CqlResponseKind::Supported,
502 NonErrorResponse::Event(_) => CqlResponseKind::Event,
503 }
504 }
505}
506
507#[derive(Debug)]
513pub enum NonErrorResponseWithDeserializedMetadata {
514 Ready,
516 Result(result::ResultWithDeserializedMetadata),
518 Authenticate(authenticate::Authenticate),
520 AuthSuccess(authenticate::AuthSuccess),
522 AuthChallenge(authenticate::AuthChallenge),
524 Supported(Supported),
526 Event(event::Event),
528}
529
530impl NonErrorResponseWithDeserializedMetadata {
531 pub fn to_response_kind(&self) -> CqlResponseKind {
533 match self {
534 Self::Ready => CqlResponseKind::Ready,
535 Self::Result(_) => CqlResponseKind::Result,
536 Self::Authenticate(_) => CqlResponseKind::Authenticate,
537 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
538 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
539 Self::Supported(_) => CqlResponseKind::Supported,
540 Self::Event(_) => CqlResponseKind::Event,
541 }
542 }
543}
544
545#[derive(Debug)]
551#[non_exhaustive]
552pub enum NonErrorResponseWithDeserializedMetadataV2 {
553 Ready,
555 Result(result::ResultWithDeserializedMetadata),
557 Authenticate(authenticate::Authenticate),
559 AuthSuccess(authenticate::AuthSuccess),
561 AuthChallenge(authenticate::AuthChallenge),
563 Supported(Supported),
565 Event(event::EventV2),
567}
568
569impl NonErrorResponseWithDeserializedMetadataV2 {
570 pub fn to_response_kind(&self) -> CqlResponseKind {
572 match self {
573 Self::Ready => CqlResponseKind::Ready,
574 Self::Result(_) => CqlResponseKind::Result,
575 Self::Authenticate(_) => CqlResponseKind::Authenticate,
576 Self::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
577 Self::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
578 Self::Supported(_) => CqlResponseKind::Supported,
579 Self::Event(_) => CqlResponseKind::Event,
580 }
581 }
582}