cassandra_protocol/frame/
message_register.rs1use crate::error;
2use crate::frame::events::SimpleServerEvent;
3use crate::frame::{Direction, Envelope, Flags, FromCursor, Opcode, Serialize, Version};
4use crate::types::{from_cursor_string_list, serialize_str_list};
5use derive_more::Constructor;
6use itertools::Itertools;
7use std::convert::TryFrom;
8use std::io::Cursor;
9
10#[derive(Debug, Constructor, Default, Ord, PartialOrd, Eq, PartialEq, Hash, Clone)]
12pub struct BodyReqRegister {
13 pub events: Vec<SimpleServerEvent>,
14}
15
16impl Serialize for BodyReqRegister {
17 fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, version: Version) {
18 let events = self.events.iter().map(|event| event.as_str());
19 serialize_str_list(cursor, events, version);
20 }
21}
22
23impl FromCursor for BodyReqRegister {
24 fn from_cursor(cursor: &mut Cursor<&[u8]>, _version: Version) -> error::Result<Self> {
25 let events = from_cursor_string_list(cursor)?;
26 events
27 .iter()
28 .map(|event| SimpleServerEvent::try_from(event.as_str()))
29 .try_collect()
30 .map(BodyReqRegister::new)
31 }
32}
33
34impl Envelope {
35 pub fn new_req_register(events: Vec<SimpleServerEvent>, version: Version) -> Envelope {
37 let direction = Direction::Request;
38 let opcode = Opcode::Register;
39 let register_body = BodyReqRegister::new(events);
40
41 Envelope::new(
42 version,
43 direction,
44 Flags::empty(),
45 opcode,
46 0,
47 register_body.serialize_to_vec(version),
48 None,
49 vec![],
50 )
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use crate::events::SimpleServerEvent;
57 use crate::frame::message_register::BodyReqRegister;
58 use crate::frame::{FromCursor, Version};
59 use std::io::Cursor;
60
61 #[test]
62 fn should_deserialize_body() {
63 let data = [
64 0, 1, 0, 15, 0x54, 0x4f, 0x50, 0x4f, 0x4c, 0x4f, 0x47, 0x59, 0x5f, 0x43, 0x48, 0x41,
65 0x4e, 0x47, 0x45,
66 ];
67 let mut cursor = Cursor::new(data.as_slice());
68
69 let body = BodyReqRegister::from_cursor(&mut cursor, Version::V4).unwrap();
70 assert_eq!(body.events, vec![SimpleServerEvent::TopologyChange]);
71 }
72}