scylla_cql/frame/request/
prepare.rs

1//! CQL protocol-level representation of a `PREPARE` request.
2
3use std::num::TryFromIntError;
4
5use thiserror::Error;
6
7use crate::frame::frame_errors::CqlRequestSerializationError;
8
9use crate::{
10    frame::request::{RequestOpcode, SerializableRequest},
11    frame::types,
12};
13
14/// CQL protocol-level representation of an `PREPARE` request,
15/// used to prepare a single statement for further execution.
16pub struct Prepare<'a> {
17    /// CQL statement string to prepare.
18    pub query: &'a str,
19}
20
21impl SerializableRequest for Prepare<'_> {
22    const OPCODE: RequestOpcode = RequestOpcode::Prepare;
23
24    fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), CqlRequestSerializationError> {
25        types::write_long_string(self.query, buf)
26            .map_err(PrepareSerializationError::StatementStringSerialization)?;
27        Ok(())
28    }
29}
30
31/// An error type returned when serialization of PREPARE request fails.
32#[non_exhaustive]
33#[derive(Error, Debug, Clone)]
34pub enum PrepareSerializationError {
35    /// Failed to serialize the CQL statement string.
36    #[error("Failed to serialize statement contents: {0}")]
37    StatementStringSerialization(TryFromIntError),
38}