1+ //! Abstractions of the CQL wire protocol:
2+ //! - request and response frames' representation and ser/de;
3+ //! - frame header and body;
4+ //! - serialization and deserialization of low-level CQL protocol types;
5+ //! - protocol features negotiation;
6+ //! - compression, tracing, custom payload support;
7+ //! - consistency levels;
8+ //! - errors that can occur during the above operations.
9+ //!
10+
111pub mod frame_errors;
212pub mod protocol_features;
313pub mod request;
@@ -25,13 +35,21 @@ const HEADER_SIZE: usize = 9;
2535
2636pub mod flag {
2737 //! Frame flags
38+
39+ /// The frame contains a compressed body.
2840 pub const COMPRESSION : u8 = 0x01 ;
41+
42+ /// The frame contains tracing ID.
2943 pub const TRACING : u8 = 0x02 ;
44+
45+ /// The frame contains a custom payload.
3046 pub const CUSTOM_PAYLOAD : u8 = 0x04 ;
47+
48+ /// The frame contains warnings.
3149 pub const WARNING : u8 = 0x08 ;
3250}
3351
34- /// All of the Authenticators supported by Scylla
52+ /// All of the Authenticators supported by ScyllaDB
3553#[ derive( Debug , PartialEq , Eq , Clone ) ]
3654// Check triggers because all variants end with "Authenticator".
3755// TODO(2.0): Remove the "Authenticator" postfix from variants.
@@ -54,6 +72,7 @@ pub enum Compression {
5472}
5573
5674impl Compression {
75+ /// Returns the string representation of the compression algorithm.
5776 pub fn as_str ( & self ) -> & ' static str {
5877 match self {
5978 Compression :: Lz4 => "lz4" ,
@@ -89,11 +108,21 @@ impl Display for Compression {
89108 }
90109}
91110
111+ /// A serialized CQL request frame, nearly ready to be sent over the wire.
112+ ///
113+ /// The only difference from a real frame is that it does not contain the stream number yet.
114+ /// The stream number is set by the `set_stream` method before sending.
92115pub struct SerializedRequest {
93116 data : Vec < u8 > ,
94117}
95118
96119impl SerializedRequest {
120+ /// Creates a new serialized request frame from a request object.
121+ ///
122+ /// # Parameters
123+ /// - `req`: The request object to serialize. Must implement `SerializableRequest`.
124+ /// - `compression`: An optional compression algorithm to use for the request body.
125+ /// - `tracing`: A boolean indicating whether to request tracing information in the response.
97126 pub fn make < R : SerializableRequest > (
98127 req : & R ,
99128 compression : Option < Compression > ,
@@ -125,20 +154,32 @@ impl SerializedRequest {
125154 Ok ( Self { data } )
126155 }
127156
157+ /// Sets the stream number for this request frame.
158+ /// Intended to be called before sending the request,
159+ /// once a stream ID has been assigned.
128160 pub fn set_stream ( & mut self , stream : i16 ) {
129161 self . data [ 2 ..4 ] . copy_from_slice ( & stream. to_be_bytes ( ) ) ;
130162 }
131163
164+ /// Returns the serialized frame data, including the header and body.
132165 pub fn get_data ( & self ) -> & [ u8 ] {
133166 & self . data [ ..]
134167 }
135168}
136169
137- // Parts of the frame header which are not determined by the request/response type.
170+ /// Parts of the frame header which are not determined by the request/response type.
138171#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
139172pub struct FrameParams {
173+ /// The version of the frame protocol. Currently, only version 4 is supported.
174+ /// The most significant bit (0x80) is treated specially:
175+ /// it indicates whether the frame is from the client or server.
140176 pub version : u8 ,
177+
178+ /// Flags for the frame, indicating features like compression, tracing, etc.
141179 pub flags : u8 ,
180+
181+ /// The stream ID for this frame, which allows matching requests and responses
182+ /// in a multiplexed connection.
142183 pub stream : i16 ,
143184}
144185
@@ -152,6 +193,8 @@ impl Default for FrameParams {
152193 }
153194}
154195
196+ /// Reads a response frame from the provided reader (usually, a socket).
197+ /// Then parses and validates the frame header and extracts the body.
155198pub async fn read_response_frame (
156199 reader : & mut ( impl AsyncRead + Unpin ) ,
157200) -> Result < ( FrameParams , ResponseOpcode , Bytes ) , FrameHeaderParseError > {
@@ -203,13 +246,29 @@ pub async fn read_response_frame(
203246 Ok ( ( frame_params, opcode, raw_body. into_inner ( ) . into ( ) ) )
204247}
205248
249+ /// Represents the already parsed response body extensions,
250+ /// including trace ID, warnings, and custom payload,
251+ /// and the remaining body raw data.
206252pub struct ResponseBodyWithExtensions {
253+ /// The trace ID if tracing was requested in the request.
254+ ///
255+ /// This can be used to issue a follow-up request to the server
256+ /// to get detailed tracing information about the request.
207257 pub trace_id : Option < Uuid > ,
258+
259+ /// Warnings returned by the server, if any.
208260 pub warnings : Vec < String > ,
209- pub body : Bytes ,
261+
262+ /// Custom payload (see [the CQL protocol description of the feature](https://github.com/apache/cassandra/blob/a39f3b066f010d465a1be1038d5e06f1e31b0391/doc/native_protocol_v4.spec#L276))
263+ /// returned by the server, if any.
210264 pub custom_payload : Option < HashMap < String , Bytes > > ,
265+
266+ /// The remaining body data after parsing the extensions.
267+ pub body : Bytes ,
211268}
212269
270+ /// Decompresses the response body if compression is enabled,
271+ /// and parses any extensions like trace ID, warnings, and custom payload.
213272pub fn parse_response_body_extensions (
214273 flags : u8 ,
215274 compression : Option < Compression > ,
@@ -260,11 +319,13 @@ pub fn parse_response_body_extensions(
260319 Ok ( ResponseBodyWithExtensions {
261320 trace_id,
262321 warnings,
263- body,
264322 custom_payload,
323+ body,
265324 } )
266325}
267326
327+ /// Compresses the request body using the specified compression algorithm,
328+ /// appending the compressed data to the provided output buffer.
268329pub fn compress_append (
269330 uncomp_body : & [ u8 ] ,
270331 compression : Compression ,
@@ -291,6 +352,8 @@ pub fn compress_append(
291352 }
292353}
293354
355+ /// Deompresses the response body using the specified compression algorithm
356+ /// and returns the decompressed data as an owned buffer.
294357pub fn decompress (
295358 mut comp_body : & [ u8 ] ,
296359 compression : Compression ,
0 commit comments