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
use derive_more::Constructor;
use std::io::Cursor;
use crate::frame::*;
use crate::query::QueryParams;
use crate::types::*;
#[derive(Debug, Constructor, Eq, PartialEq)]
pub struct BodyReqExecute<'a> {
id: &'a CBytesShort,
query_parameters: &'a QueryParams,
}
impl<'a> Serialize for BodyReqExecute<'a> {
fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>) {
self.id.serialize(cursor);
self.query_parameters.serialize(cursor);
}
#[inline]
fn serialize_to_vec(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(self.id.serialized_len());
let _ = self.serialize(&mut Cursor::new(&mut buf));
buf
}
}
#[derive(Debug, Constructor, Clone, Eq, PartialEq, Default)]
pub struct BodyReqExecuteOwned {
id: CBytesShort,
query_parameters: QueryParams,
}
impl FromCursor for BodyReqExecuteOwned {
fn from_cursor(cursor: &mut Cursor<&[u8]>) -> error::Result<Self> {
let id = CBytesShort::from_cursor(cursor)?;
let query_parameters = QueryParams::from_cursor(cursor)?;
Ok(BodyReqExecuteOwned::new(id, query_parameters))
}
}
impl Serialize for BodyReqExecuteOwned {
fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>) {
BodyReqExecute::new(&self.id, &self.query_parameters).serialize(cursor)
}
}
impl Frame {
pub fn new_req_execute(
id: &CBytesShort,
query_parameters: &QueryParams,
flags: Flags,
version: Version,
) -> Frame {
let direction = Direction::Request;
let opcode = Opcode::Execute;
let body = BodyReqExecute::new(id, query_parameters);
Frame::new(
version,
direction,
flags,
opcode,
0,
body.serialize_to_vec(),
None,
vec![],
)
}
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use crate::consistency::Consistency;
use crate::frame::frame_execute::BodyReqExecuteOwned;
use crate::frame::FromCursor;
use crate::types::CBytesShort;
#[test]
fn should_deserialize_body() {
let data = [0, 1, 2, 0, 0, 0];
let mut cursor = Cursor::new(data.as_slice());
let body = BodyReqExecuteOwned::from_cursor(&mut cursor).unwrap();
assert_eq!(body.id, CBytesShort::new(vec![2]));
assert_eq!(body.query_parameters.consistency, Consistency::Any);
}
}