net_reporter_api/api/network_graph/
graph_edge.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
14
15const DATA_TYPE: &str = "graph_edge";
16
17#[derive(Debug, PartialEq, Eq, Clone)]
18pub struct GraphEdgeDTO {
19 src_id: String,
20 dst_id: String,
21 value: i64,
22}
23impl API for GraphEdgeDTO { }
24
25impl GraphEdgeDTO {
26 pub fn new(src_id: &str, dst_id: &str, value: i64) -> Self {
27 GraphEdgeDTO {
28 src_id: src_id.into(),
29 dst_id: dst_id.into(),
30 value,
31 }
32 }
33
34 pub fn get_src_id(&self) -> &str {
35 &self.src_id
36 }
37
38 pub fn get_dst_id(&self) -> &str {
39 &self.dst_id
40 }
41
42 pub fn get_value(&self) -> i64 {
43 self.value
44 }
45}
46
47impl Encoder for GraphEdgeDTO {
48 fn encode(&self) -> Vec<u8> {
49 let buffer: Vec<u8> = Vec::new();
50
51 let binary_writer_builder = ion_rs::BinaryWriterBuilder::new();
52 let mut writer = binary_writer_builder.build(buffer.clone()).unwrap();
53
54 writer.step_in(IonType::Struct).expect("Error while creating an ion struct");
55
56 writer.set_field_name("src_id");
57 writer.write_string(&self.src_id).unwrap();
58
59 writer.set_field_name("dst_id");
60 writer.write_string(&self.dst_id).unwrap();
61
62 writer.set_field_name("value");
63 writer.write_i64(self.value).unwrap();
64
65 writer.step_out().unwrap();
66 writer.flush().unwrap();
67
68 writer.output().as_slice().into()
69 }
70}
71
72impl Decoder for GraphEdgeDTO {
73 fn decode(data: &[u8]) -> Self {
74
75 let mut binary_user_reader = ReaderBuilder::new().build(data).unwrap();
76 binary_user_reader.next().unwrap();
77 binary_user_reader.step_in().unwrap();
78
79 binary_user_reader.next().unwrap();
80 let binding = binary_user_reader.read_string().unwrap();
81 let src_id = binding.text();
82
83 binary_user_reader.next().unwrap();
84 let binding = binary_user_reader.read_string().unwrap();
85 let dst_id = binding.text();
86
87 binary_user_reader.next().unwrap();
88 let value = binary_user_reader.read_i64().unwrap();
89
90 binary_user_reader.step_out().unwrap();
91
92 GraphEdgeDTO::new(
93 src_id,
94 dst_id,
95 value,
96 )
97 }
98}
99
100impl Typed for GraphEdgeDTO {
101 fn get_data_type() -> &'static str {
102 DATA_TYPE
103 }
104 fn get_type(&self) -> &str {
105 Self::get_data_type()
106 }
107}
108
109
110#[cfg(test)]
111mod tests {
112 use ion_rs::IonType;
113 use ion_rs::IonReader;
114 use ion_rs::ReaderBuilder;
115 use ion_rs::StreamItem;
116
117 use net_core_api::core::encoder_api::Encoder;
118 use net_core_api::core::decoder_api::Decoder;
119 use net_core_api::core::typed_api::Typed;
120
121 use crate::api::network_graph::graph_edge::GraphEdgeDTO;
122
123
124 #[test]
125 fn reader_correctly_read_encoded_graph_edge() {
126 const SRC_ID: &str = "0.0.0.0:0000";
127 const DST_ID: &str = "0.0.0.0:5656";
128 const VALUE: i64 = 123;
129 let graph_edge: GraphEdgeDTO = GraphEdgeDTO::new(SRC_ID, DST_ID, VALUE);
130 let mut binary_user_reader = ReaderBuilder::new().build(graph_edge.encode()).unwrap();
131
132 assert_eq!(StreamItem::Value(IonType::Struct), binary_user_reader.next().unwrap());
133 binary_user_reader.step_in().unwrap();
134
135 assert_eq!(StreamItem::Value(IonType::String), binary_user_reader.next().unwrap());
136 assert_eq!("src_id", binary_user_reader.field_name().unwrap());
137 assert_eq!(SRC_ID, binary_user_reader.read_string().unwrap().text());
138
139 assert_eq!(StreamItem::Value(IonType::String), binary_user_reader.next().unwrap());
140 assert_eq!("dst_id", binary_user_reader.field_name().unwrap());
141 assert_eq!(DST_ID, binary_user_reader.read_string().unwrap().text());
142
143 assert_eq!(StreamItem::Value(IonType::Int), binary_user_reader.next().unwrap());
144 assert_eq!("value", binary_user_reader.field_name().unwrap());
145 assert_eq!(VALUE, binary_user_reader.read_i64().unwrap());
146
147 binary_user_reader.step_out().unwrap();
148 }
149
150 #[test]
151 fn endec_graph_edge() {
152 const SRC_ID: &str = "0.0.0.0:0000";
153 const DST_ID: &str = "0.0.0.0:5656";
154 const VALUE: i64 = 123;
155 let graph_edge: GraphEdgeDTO = GraphEdgeDTO::new(SRC_ID, DST_ID, VALUE);
156 assert_eq!(graph_edge, GraphEdgeDTO::decode(&graph_edge.encode()));
157 }
158
159 #[test]
160 fn test_getting_data_types() {
161 const SRC_ID: &str = "0.0.0.0:0000";
162 const DST_ID: &str = "0.0.0.0:5656";
163 const VALUE: i64 = 123;
164 let graph_edge: GraphEdgeDTO = GraphEdgeDTO::new(SRC_ID, DST_ID, VALUE);
165 assert_eq!(graph_edge.get_type(), GraphEdgeDTO::get_data_type());
166 assert_eq!(graph_edge.get_type(), super::DATA_TYPE);
167 }
168}