net_deleter_api/api/
buffered_packet.rs1use ion_rs;
2
3use ion_rs::IonReader;
4use ion_rs::IonType;
5use ion_rs::IonWriter;
6
7use ion_rs::ReaderBuilder;
8
9use net_core_api::core::api::API;
10use net_core_api::core::encoder_api::Encoder;
11use net_core_api::core::decoder_api::Decoder;
12use net_core_api::core::typed_api::Typed;
13
14const DATA_TYPE: &str = "delete-buffered-packet-request";
15
16#[derive(Debug, PartialEq, Eq)]
17pub struct DeleteBufferedPacketRequestDTO {
18 id: String,
19}
20impl API for DeleteBufferedPacketRequestDTO { }
21
22impl DeleteBufferedPacketRequestDTO {
23 pub fn new(
24 id: &str
25 ) -> Self {
26 DeleteBufferedPacketRequestDTO {
27 id: id.to_string(),
28 }
29 }
30
31 pub fn get_id(&self) -> &str {
32 &self.id
33 }
34}
35
36impl Encoder for DeleteBufferedPacketRequestDTO {
37 fn encode(&self) -> Vec<u8> {
38 let buffer: Vec<u8> = Vec::new();
39
40 let binary_writer_builder = ion_rs::BinaryWriterBuilder::new();
41 let mut writer = binary_writer_builder.build(buffer.clone()).unwrap();
42
43 writer.step_in(IonType::Struct).expect("Error while creating an ion struct");
44
45 writer.set_field_name("id");
46 writer.write_string(&self.id).unwrap();
47
48 writer.step_out().unwrap();
49 writer.flush().unwrap();
50
51 writer.output().as_slice().into()
52 }
53}
54
55impl Decoder for DeleteBufferedPacketRequestDTO {
56 fn decode(data: &[u8]) -> Self {
57 let mut binary_user_reader = ReaderBuilder::new().build(data).unwrap();
58 binary_user_reader.next().unwrap();
59 binary_user_reader.step_in().unwrap();
60
61 binary_user_reader.next().unwrap();
62 let id = binary_user_reader.read_string().unwrap();
63
64 DeleteBufferedPacketRequestDTO::new(id.text())
65 }
66}
67
68impl Typed for DeleteBufferedPacketRequestDTO {
69 fn get_data_type() -> &'static str {
70 DATA_TYPE
71 }
72
73 fn get_type(&self) -> &str {
74 Self::get_data_type()
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use ion_rs::IonType;
81 use ion_rs::IonReader;
82 use ion_rs::ReaderBuilder;
83 use ion_rs::StreamItem;
84
85 use net_core_api::core::encoder_api::Encoder;
86 use net_core_api::core::decoder_api::Decoder;
87
88 use crate::api::buffered_packet::DeleteBufferedPacketRequestDTO;
89
90 #[test]
91 fn reader_correctly_read_encoded_delete_buffered_packet_request() {
92 const ID: &str = "1";
93 let delete_packet_request = DeleteBufferedPacketRequestDTO::new(ID);
94 let mut binary_user_reader = ReaderBuilder::new().build(delete_packet_request.encode()).unwrap();
95
96 assert_eq!(StreamItem::Value(IonType::Struct), binary_user_reader.next().unwrap());
97 binary_user_reader.step_in().unwrap();
98
99 assert_eq!(StreamItem::Value(IonType::String), binary_user_reader.next().unwrap());
100 assert_eq!("id", binary_user_reader.field_name().unwrap());
101 assert_eq!(ID, binary_user_reader.read_str().unwrap());
102
103 binary_user_reader.step_out().unwrap();
104 }
105
106 #[test]
107 fn endec_delete_buffered_packet_request() {
108 const ID: &str = "1";
109 let delete_packet_request = DeleteBufferedPacketRequestDTO::new(ID);
110
111 assert_eq!(delete_packet_request, DeleteBufferedPacketRequestDTO::decode(&delete_packet_request.encode()));
112 }
113}