1use std::any::Any;
2use crate::buffer::read::SonetReadBuf;
3
4pub struct SonetWriteBuf {
6
7 data: Vec<u8>,
9
10 position: i32
12}
13
14impl SonetWriteBuf {
16
17 pub fn new() -> Self {
19 Self { data: vec![], position: 0 }
20 }
21
22 pub fn readable(&mut self) -> SonetReadBuf {
24 SonetReadBuf::new(self.data.to_vec())
25 }
26
27 pub fn write_byte(&mut self, data: u8) {
29 self.write_raw(data.to_be_bytes().as_slice());
30 }
31
32 pub fn write_short(&mut self, data: u16) {
34 self.write_raw(data.to_be_bytes().as_slice());
35 }
36
37 pub fn write_int(&mut self, data: u32) {
39 self.write_raw(data.to_be_bytes().as_slice());
40 }
41
42 pub fn write_long(&mut self, data: u64) {
44 self.write_raw(data.to_be_bytes().as_slice());
45 }
46
47 pub fn write_bool(&mut self, data: bool) {
49 if data {
50 self.write_byte(1_u8.to_owned());
51 } else {
52 self.write_byte(0_u8.to_owned());
53 }
54 }
55
56 pub fn write_byte_array(&mut self, data: &Vec<u8>) {
58 self.write_int(data.len() as u32);
59 for datum in data.to_vec().into_iter() {
60 self.write_byte(datum);
61 }
62 }
63
64 pub fn write_short_array(&mut self, data: &Vec<u16>) {
66 self.write_int(data.len() as u32);
67 for datum in data.to_vec().into_iter() {
68 self.write_short(datum);
69 }
70 }
71
72 pub fn write_int_array(&mut self, data: &Vec<u32>) {
74 self.write_int(data.len() as u32);
75 for datum in data.to_vec().into_iter() {
76 self.write_int(datum);
77 }
78 }
79
80 pub fn write_long_array(&mut self, data: &Vec<u64>) {
82 self.write_int(data.len() as u32);
83 for datum in data.to_vec().into_iter() {
84 self.write_long(datum);
85 }
86 }
87
88 pub fn write_float(&mut self, data: f32) {
90 self.write_raw(data.to_be_bytes().as_slice());
91
92 }
93
94 pub fn write_double(&mut self, data: f64) {
96 self.write_raw(data.to_be_bytes().as_slice());
97 }
98
99 pub fn write_float_array(&mut self, data: &Vec<f32>) {
101 self.write_int(data.len() as u32);
102 for datum in data.to_vec().into_iter() {
103 self.write_float(datum);
104 }
105 }
106
107 pub fn write_double_array(&mut self, data: &Vec<f64>) {
109 self.write_int(data.len() as u32);
110 for datum in data.to_vec().into_iter() {
111 self.write_double(datum);
112 }
113 }
114
115 pub fn write_char(&mut self, data: char) {
117 self.write_byte(data as u8);
118 }
119
120 pub fn write_string(&mut self, data: &String) {
122 self.write_byte_array(&data.as_bytes().to_vec());
123 }
124
125 pub fn parse_types(&mut self, types: Vec<&'static str>, data: Vec<Box<dyn Any>>) {
127 let data = &data;
128 for (index, type_name) in types.into_iter().enumerate() {
129 match type_name {
130 "String" => self.write_string(data.as_slice()[index].downcast_ref::<String>().unwrap()),
131 "char" => self.write_char(*data.as_slice()[index].downcast_ref::<char>().unwrap()),
132 "i8" | "u8" => self.write_byte(*data.as_slice()[index].downcast_ref::<u8>().unwrap()),
133 "i16" | "u16" => self.write_short(*data.as_slice()[index].downcast_ref::<u16>().unwrap()),
134 "i32" | "u32" => self.write_int(*data.as_slice()[index].downcast_ref::<u32>().unwrap()),
135 "i64" | "u64" => self.write_long(*data.as_slice()[index].downcast_ref::<u64>().unwrap()),
136 "f32" => self.write_float(*data.as_slice()[index].downcast_ref::<f32>().unwrap()),
137 "f64" => self.write_double(*data.as_slice()[index].downcast_ref::<f64>().unwrap()),
138 "Vec<u8>" => self.write_byte_array(data.as_slice()[index].downcast_ref::<Vec<u8>>().unwrap()),
139 "Vec<u16>" => self.write_short_array(data.as_slice()[index].downcast_ref::<Vec<u16>>().unwrap()),
140 "Vec<u32>" => self.write_int_array(data.as_slice()[index].downcast_ref::<Vec<u32>>().unwrap()),
141 "Vec<u64>" => self.write_long_array(data.as_slice()[index].downcast_ref::<Vec<u64>>().unwrap()),
142 "Vec<f32>" => self.write_float_array(data.as_slice()[index].downcast_ref::<Vec<f32>>().unwrap()),
143 "Vec<f64>" => self.write_double_array(data.as_slice()[index].downcast_ref::<Vec<f64>>().unwrap()),
144 "Vec<i8>" => self.write_byte_array(&data.as_slice()[index].downcast_ref::<Vec<i8>>().unwrap().to_vec().into_iter().map(|x| x as u8).collect::<Vec<u8>>()),
145 "Vec<i16>" => self.write_short_array(&data.as_slice()[index].downcast_ref::<Vec<i16>>().unwrap().to_vec().into_iter().map(|x| x as u16).collect::<Vec<u16>>()),
146 "Vec<i32>" => self.write_int_array(&data.as_slice()[index].downcast_ref::<Vec<i32>>().unwrap().to_vec().into_iter().map(|x| x as u32).collect::<Vec<u32>>()),
147 "Vec<i64>" => self.write_long_array(&data.as_slice()[index].downcast_ref::<Vec<i64>>().unwrap().to_vec().into_iter().map(|x| x as u64).collect::<Vec<u64>>()),
148 _ => panic!("Unsupported Type, {}", type_name)
149 }
150 }
151 }
152
153 pub fn write_raw(&mut self, data: &[u8]) {
155 for i in 0 .. data.len() {
156 if self.position + (i as i32) >= self.data.len() as i32 {
157 self.data.push(0);
158 }
159 self.data[(self.position) as usize] = data[i];
160 self.position += 1;
161 }
162 }
163}