1pub mod buffer;
2pub mod encoding;
3pub mod metadata;
4pub mod reader;
5pub mod writer;
6
7pub use buffer::types;
8pub use buffer::Buffer;
9pub use metadata::FieldMetadata;
10pub use reader::BufferMutReader;
11pub use reader::BufferReader;
12pub use writer::BufferWriter;
13
14#[cfg(test)]
15mod tests {
16 use crate::{types, Buffer};
17
18 #[test]
19 pub fn buffer_test() {
20 let data_types = vec![
21 types::BOOL,
22 types::I8,
23 types::U8,
24 types::I16,
25 types::U16,
26 types::I32,
27 types::U32,
28 types::I64,
29 types::U64,
30 types::F32,
31 types::F64,
32 types::BINARY,
33 types::STRING,
34 types::STRING,
35 types::I32,
36 ];
37
38 let strs = vec![
39 "aaaa-bbbb-cccc-dddd",
40 "cccc-bbbb-aaaa",
41 "aaaa-bbbb-cccc-dddd-eeee",
42 "dddd-cccc-bbbb-aaaa",
43 "bbbb",
44 ];
45 for i in 0..strs.len() {
46 let mut buffer = Buffer::new();
47
48 let uuid1 = strs[i];
49 let uuid2 = uuid::Uuid::new_v4().to_string();
50 let uuid2 = uuid2.as_str();
51
52 let mut writer = buffer.as_writer(&data_types);
53
54 writer.set_bool(i % 2 == 0).unwrap();
55
56 writer.set_i8((10 + i) as i8).unwrap();
57 writer.set_u8((11 + i) as u8).unwrap();
58
59 writer.set_i16((12 + i) as i16).unwrap();
60 writer.set_u16((13 + i) as u16).unwrap();
61
62 writer.set_i32((14 + i) as i32).unwrap();
63 writer.set_u32((15 + i) as u32).unwrap();
64
65 writer.set_i64((16 + i) as i64).unwrap();
66 writer.set_u64((17 + i) as u64).unwrap();
67
68 writer.set_f32(18.001 + i as f32).unwrap();
69 writer.set_f64(19.002 + i as f64).unwrap();
70
71 writer.set_binary(uuid1.as_bytes()).unwrap();
72 writer.set_str(uuid2).unwrap();
73 writer.set_str("").unwrap();
74
75 writer.set_i32((5 + i) as i32).unwrap();
76
77 println!("{:?}", buffer.buf.as_ref());
78
79 let reader = buffer.as_reader(&data_types);
80
81 assert_eq!(reader.get_bool(0).unwrap(), i % 2 == 0);
82
83 assert_eq!(reader.get_i8(1).unwrap(), (10 + i) as i8);
84 assert_eq!(reader.get_u8(2).unwrap(), (11 + i) as u8);
85
86 assert_eq!(reader.get_i16(3).unwrap(), (12 + i) as i16);
87 assert_eq!(reader.get_u16(4).unwrap(), (13 + i) as u16);
88
89 assert_eq!(reader.get_i32(5).unwrap(), (14 + i) as i32);
90 assert_eq!(reader.get_u32(6).unwrap(), (15 + i) as u32);
91
92 assert_eq!(reader.get_i64(7).unwrap(), (16 + i) as i64);
93 assert_eq!(reader.get_u64(8).unwrap(), (17 + i) as u64);
94
95 assert_eq!(reader.get_f32(9).unwrap(), 18.001 + i as f32);
96 assert_eq!(reader.get_f64(10).unwrap(), 19.002 + i as f64);
97
98 assert_eq!(reader.get_binary(11).unwrap(), uuid1.as_bytes());
99 assert_eq!(reader.get_str(12).unwrap(), uuid2.to_string());
100 assert_eq!(reader.get_str(13).unwrap(), "".to_string());
101
102 assert_eq!(reader.get_i32(14).unwrap(), (5 + i) as i32);
103
104 }
106 }
107
108 #[test]
109 pub fn buf_extend_test() {
110 let mut buffer0 = Buffer::new();
111 let mut buffer1 = Buffer::new();
112
113 let data_types0 = [
114 types::I8,
115 types::I16,
116 types::I32,
117 types::I64,
118 types::F32,
119 types::STRING,
120 types::I32,
121 ];
122 let data_types1 = [
123 types::U8,
124 types::U16,
125 types::U32,
126 types::U64,
127 types::F64,
128 types::STRING,
129 types::U32,
130 ];
131
132 let uuid1 = "b871544b-c044-495c-98ee-f5aa34660527".to_string();
133 let uuid2 = "6af8fda0-e9fa-498b-829a-bb3c7b87554b".to_string();
134
135 {
136 let mut writer = buffer0.as_writer(&data_types0);
137
138 writer.set_i8((10) as i8).unwrap();
139 writer.set_i16((12) as i16).unwrap();
140 writer.set_i32((14) as i32).unwrap();
141 writer.set_i64((16) as i64).unwrap();
142 writer.set_f32(18.001 as f32).unwrap();
143 writer.set_str(uuid1.as_str()).unwrap();
144 writer.set_i32((5) as i32).unwrap();
145 }
146
147 {
148 let mut writer = buffer1.as_writer(&data_types1);
149
150 writer.set_u8((11) as u8).unwrap();
151 writer.set_u16((13) as u16).unwrap();
152 writer.set_u32((15) as u32).unwrap();
153 writer.set_u64((17) as u64).unwrap();
154 writer.set_f64(19.002 as f64).unwrap();
155 writer.set_str(uuid2.as_str()).unwrap();
156 writer.set_u32((5) as u32).unwrap();
157 }
158
159 println!("{:?}", buffer0.buf.as_ref());
160 println!("{:?}", buffer1.buf.as_ref());
161
162 buffer0.extend(&buffer1).unwrap();
163 println!("{:?}", buffer0.buf.as_ref());
164
165 let mut data_type_merge = data_types0.to_vec();
166 data_type_merge.extend_from_slice(&data_types1);
167 let reader = buffer0.as_reader(data_type_merge.as_slice());
168
169 assert_eq!(reader.get_i8(0).unwrap(), (10) as i8);
170 assert_eq!(reader.get_i16(1).unwrap(), (12) as i16);
171 assert_eq!(reader.get_i32(2).unwrap(), (14) as i32);
172 assert_eq!(reader.get_i64(3).unwrap(), (16) as i64);
173 assert_eq!(reader.get_f32(4).unwrap(), 18.001 as f32);
174 assert_eq!(reader.get_str(5).unwrap(), uuid1);
175 assert_eq!(reader.get_i32(6).unwrap(), (5) as i32);
176
177 assert_eq!(reader.get_u8(7).unwrap(), (11) as u8);
178 assert_eq!(reader.get_u16(8).unwrap(), (13) as u16);
179 assert_eq!(reader.get_u32(9).unwrap(), (15) as u32);
180 assert_eq!(reader.get_u64(10).unwrap(), (17) as u64);
181 assert_eq!(reader.get_f64(11).unwrap(), 19.002 as f64);
182 assert_eq!(reader.get_str(12).unwrap(), uuid2);
183 assert_eq!(reader.get_u32(13).unwrap(), (5) as u32);
184 }
185}