scylla-cql 2.0.0

CQL data types and primitives, for interacting with ScyllaDB.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! CQL responses sent by the server.

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;

/// Opcode of a response, used to identify the response type in a CQL frame.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ResponseOpcode {
    /// See [CqlResponseKind::Error].
    Error = 0x00,
    /// See [CqlResponseKind::Ready].
    Ready = 0x02,
    /// See [CqlResponseKind::Authenticate].
    Authenticate = 0x03,
    /// See [CqlResponseKind::Supported].
    Supported = 0x06,
    /// See [CqlResponseKind::Result].
    Result = 0x08,
    /// See [CqlResponseKind::Event].
    Event = 0x0C,
    /// See [CqlResponseKind::AuthChallenge].
    AuthChallenge = 0x0E,
    /// See [CqlResponseKind::AuthSuccess].
    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)),
        }
    }
}

/// A CQL response that has been received from the server.
#[derive(Debug)]
pub enum Response {
    /// ERROR response, returned by the server when an error occurs.
    Error(Error),
    /// READY response, indicating that the server is ready to process requests,
    /// typically after a connection is established.
    Ready,
    /// RESULT response, containing the result of a statement execution.
    Result(result::Result),
    /// AUTHENTICATE response, indicating that the server requires authentication.
    Authenticate(authenticate::Authenticate),
    /// AUTH_SUCCESS response, indicating that the authentication was successful.
    AuthSuccess(authenticate::AuthSuccess),
    /// AUTH_CHALLENGE response, indicating that the server requires further authentication.
    AuthChallenge(authenticate::AuthChallenge),
    /// SUPPORTED response, containing the features supported by the server.
    Supported(Supported),
    /// EVENT response, containing an event that occurred on the server.
    Event(event::Event),
}

/// A CQL response that has been received from the server.
#[derive(Debug)]
#[non_exhaustive]
pub enum ResponseV2 {
    /// ERROR response, returned by the server when an error occurs.
    Error(Error),
    /// READY response, indicating that the server is ready to process requests,
    /// typically after a connection is established.
    Ready,
    /// RESULT response, containing the result of a statement execution.
    Result(result::Result),
    /// AUTHENTICATE response, indicating that the server requires authentication.
    Authenticate(authenticate::Authenticate),
    /// AUTH_SUCCESS response, indicating that the authentication was successful.
    AuthSuccess(authenticate::AuthSuccess),
    /// AUTH_CHALLENGE response, indicating that the server requires further authentication.
    AuthChallenge(authenticate::AuthChallenge),
    /// SUPPORTED response, containing the features supported by the server.
    Supported(Supported),
    /// EVENT response, containing an event that occurred on the server.
    Event(event::EventV2),
}

impl Response {
    /// Returns the kind of this 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,
        }
    }

    /// Deserialize a response from the given bytes.
    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)
    }

    /// Converts this response into a `NonErrorResponse`, returning an error if it is an `Error` response.
    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 {
    /// Returns the kind of this response.
    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,
        }
    }

    /// Deserialize a response from the given bytes.
    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)
    }
}

/// A CQL response that has been received from the server.
#[derive(Debug)]
pub enum ResponseWithDeserializedMetadata {
    /// ERROR response, returned by the server when an error occurs.
    Error(Error),
    /// READY response, indicating that the server is ready to process requests,
    /// typically after a connection is established.
    Ready,
    /// RESULT response, containing the result of a statement execution.
    Result(result::ResultWithDeserializedMetadata),
    /// AUTHENTICATE response, indicating that the server requires authentication.
    Authenticate(authenticate::Authenticate),
    /// AUTH_SUCCESS response, indicating that the authentication was successful.
    AuthSuccess(authenticate::AuthSuccess),
    /// AUTH_CHALLENGE response, indicating that the server requires further authentication.
    AuthChallenge(authenticate::AuthChallenge),
    /// SUPPORTED response, containing the features supported by the server.
    Supported(Supported),
    /// EVENT response, containing an event that occurred on the server.
    Event(event::Event),
}

impl ResponseWithDeserializedMetadata {
    /// Returns the kind of this response.
    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,
        }
    }

    /// Converts this response into a `NonErrorResponse`, returning an error if it is an `Error` response.
    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)
    }
}

/// A CQL response that has been received from the server.
#[derive(Debug)]
#[non_exhaustive]
pub enum ResponseWithDeserializedMetadataV2 {
    /// ERROR response, returned by the server when an error occurs.
    Error(Error),
    /// READY response, indicating that the server is ready to process requests,
    /// typically after a connection is established.
    Ready,
    /// RESULT response, containing the result of a statement execution.
    Result(result::ResultWithDeserializedMetadata),
    /// AUTHENTICATE response, indicating that the server requires authentication.
    Authenticate(authenticate::Authenticate),
    /// AUTH_SUCCESS response, indicating that the authentication was successful.
    AuthSuccess(authenticate::AuthSuccess),
    /// AUTH_CHALLENGE response, indicating that the server requires further authentication.
    AuthChallenge(authenticate::AuthChallenge),
    /// SUPPORTED response, containing the features supported by the server.
    Supported(Supported),
    /// EVENT response, containing an event that occurred on the server.
    Event(event::EventV2),
}

impl ResponseWithDeserializedMetadataV2 {
    /// Returns the kind of this response.
    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,
        }
    }

    /// Converts this response into a `NonErrorResponseV2`, returning an error if it is an `Error` response.
    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)
    }
}

/// A CQL response that has been received from the server, excluding error responses.
/// This is used to handle responses that are not errors, allowing for easier processing
/// of valid responses without need to handle error case any later.
#[derive(Debug)]
pub enum NonErrorResponse {
    /// See [`Response::Ready`].
    Ready,
    /// See [`Response::Result`].
    Result(result::Result),
    /// See [`Response::Authenticate`].
    Authenticate(authenticate::Authenticate),
    /// See [`Response::AuthSuccess`].
    AuthSuccess(authenticate::AuthSuccess),
    /// See [`Response::AuthChallenge`].
    AuthChallenge(authenticate::AuthChallenge),
    /// See [`Response::Supported`].
    Supported(Supported),
    /// See [`Response::Event`].
    Event(event::Event),
}

impl NonErrorResponse {
    /// Returns the kind of this non-error response.
    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,
        }
    }
}

/// A CQL response that has been received from the server, excluding error responses.
/// This is used to handle responses that are not errors, allowing for easier processing
/// of valid responses without need to handle error case any later.
/// The difference from [NonErrorResponse] is that Result::Rows variant holds [result::DeserializedMetadataAndRawRows]
/// instead of [result::RawMetadataAndRawRows].
#[derive(Debug)]
pub enum NonErrorResponseWithDeserializedMetadata {
    /// See [`Response::Ready`].
    Ready,
    /// See [`Response::Result`].
    Result(result::ResultWithDeserializedMetadata),
    /// See [`Response::Authenticate`].
    Authenticate(authenticate::Authenticate),
    /// See [`Response::AuthSuccess`].
    AuthSuccess(authenticate::AuthSuccess),
    /// See [`Response::AuthChallenge`].
    AuthChallenge(authenticate::AuthChallenge),
    /// See [`Response::Supported`].
    Supported(Supported),
    /// See [`Response::Event`].
    Event(event::Event),
}

impl NonErrorResponseWithDeserializedMetadata {
    /// Returns the kind of this non-error response.
    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,
        }
    }
}

/// A CQL response that has been received from the server, excluding error responses.
/// This is used to handle responses that are not errors, allowing for easier processing
/// of valid responses without need to handle error case any later.
/// The difference from [NonErrorResponse] is that Result::Rows variant holds [result::DeserializedMetadataAndRawRows]
/// instead of [result::RawMetadataAndRawRows].
#[derive(Debug)]
#[non_exhaustive]
pub enum NonErrorResponseWithDeserializedMetadataV2 {
    /// See [`Response::Ready`].
    Ready,
    /// See [`Response::Result`].
    Result(result::ResultWithDeserializedMetadata),
    /// See [`Response::Authenticate`].
    Authenticate(authenticate::Authenticate),
    /// See [`Response::AuthSuccess`].
    AuthSuccess(authenticate::AuthSuccess),
    /// See [`Response::AuthChallenge`].
    AuthChallenge(authenticate::AuthChallenge),
    /// See [`Response::Supported`].
    Supported(Supported),
    /// See [`Response::Event`].
    Event(event::EventV2),
}

impl NonErrorResponseWithDeserializedMetadataV2 {
    /// Returns the kind of this non-error response.
    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,
        }
    }
}