sonet_rs/buffer/
write.rs

1use std::any::Any;
2use crate::buffer::read::SonetReadBuf;
3
4/// Writable Buf
5pub struct SonetWriteBuf {
6
7    /// Data of written buffers
8    data: Vec<u8>,
9
10    /// Current position
11    position: i32
12}
13
14/// Writable Buf Implementation
15impl SonetWriteBuf {
16
17    /// New SonetWriteBuf
18    pub fn new() -> Self {
19        Self { data: vec![], position: 0 }
20    }
21
22    /// Convert to SonetReadBuf
23    pub fn readable(&mut self) -> SonetReadBuf {
24        SonetReadBuf::new(self.data.to_vec())
25    }
26
27    /// Write byte or u8
28    pub fn write_byte(&mut self, data: u8) {
29        self.write_raw(data.to_be_bytes().as_slice());
30    }
31
32    /// Write short or u16
33    pub fn write_short(&mut self, data: u16) {
34        self.write_raw(data.to_be_bytes().as_slice());
35    }
36
37    /// Write int or u32
38    pub fn write_int(&mut self, data: u32) {
39        self.write_raw(data.to_be_bytes().as_slice());
40    }
41
42    /// Write long or u64
43    pub fn write_long(&mut self, data: u64) {
44        self.write_raw(data.to_be_bytes().as_slice());
45    }
46
47    /// Write 0 if false, 1 if true
48    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    /// Write byte_array or vec<u8>
57    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    /// Write short_array or vec<u16>
65    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    /// Write int_array or vec<u32>
73    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    /// Write long_array or vec<u64>
81    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    /// Write float or f32
89    pub fn write_float(&mut self, data: f32) {
90        self.write_raw(data.to_be_bytes().as_slice());
91
92    }
93
94    /// Write double or f64
95    pub fn write_double(&mut self, data: f64) {
96        self.write_raw(data.to_be_bytes().as_slice());
97    }
98
99    /// Write float_array or vec<f32>
100    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    /// Write double_array or vec<f64>
108    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    /// Write char
116    pub fn write_char(&mut self, data: char) {
117        self.write_byte(data as u8);
118    }
119
120    /// Write String
121    pub fn write_string(&mut self, data: &String) {
122        self.write_byte_array(&data.as_bytes().to_vec());
123    }
124
125    /// Write with given type and data list
126    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    /// Write to the buffer, automatically extending the buffer if required
154    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}