gondola_core/io/
serialization.rs1use crate::prelude::*;
5
6pub use crate::io::caraspace::{
7 Frameable
8};
9
10pub trait Serialization {
13
14 const HEAD: u16 = 0xAAAA;
16 const TAIL: u16 = 0x5555;
18 const SIZE: usize = 0;
24
25 fn verify_fixed(stream : &Vec<u8>,
30 pos : &mut usize) -> Result<(), SerializationError> {
31 if !Self::SIZE == 0 {
32 panic!("Self::verify_fixed can be only used for structs with a fixed size! In case you are convinced, that your struct has indeed a fixed size, please implement trait Serialization::SIZE with the serialized size in bytes including 4 bytes for header and footer!");
35 }
36 let head_pos = seek_marker(stream, Self::HEAD, *pos)?;
37 let tail_pos = seek_marker(stream, Self::TAIL, head_pos + Self::SIZE-2)?;
38 if tail_pos + 2 - head_pos != Self::SIZE {
39 error!("Seing {} bytes, but expecting {}", tail_pos + 2 - head_pos, Self::SIZE);
40 *pos = head_pos + 2;
41 return Err(SerializationError::WrongByteSize);
42 }
43 *pos = head_pos + 2;
44 Ok(())
45 }
46
47 fn from_bytestream(bytestream : &Vec<u8>,
56 pos : &mut usize)
57 -> Result<Self, SerializationError>
58 where Self : Sized;
59
60 fn from_bytestream_alt(bytestream : &Vec<u8>,
70 pos : &mut usize)
71 -> Result<Self, SerializationError>
72 where Self : Sized {
73 Self::from_bytestream(bytestream, pos)
74 }
75
76 fn to_bytestream(&self) -> Vec<u8> {
82 error!("No default implementation for trait!");
83 return Vec::<u8>::new();
84 }
85}
86
87pub fn seek_marker<T: AsRef<[u8]>>(stream : &T, marker : u16, start_pos :usize)
101 -> Result<usize, SerializationError> {
102 let bytestream = stream.as_ref();
104 if bytestream.len() == 0 {
105 error!("Stream empty!");
106 return Err(SerializationError::StreamTooShort);
107 }
108 if start_pos > bytestream.len() - 2 {
109 error!("Start position {} beyond stream capacity {}!", start_pos, bytestream.len() -2);
110 return Err(SerializationError::StreamTooShort);
111 }
112 let mut pos = start_pos;
113 let mut two_bytes : [u8;2];
114 two_bytes = [bytestream[pos], bytestream[pos + 1]];
116 if u16::from_le_bytes(two_bytes) == marker {
118 return Ok(pos);
119 }
120 pos += 1;
123 let mut found = false;
124 for n in pos..bytestream.len() - 1 {
126 two_bytes = [bytestream[n], bytestream[n + 1]];
127 if (u16::from_le_bytes(two_bytes)) == marker {
128 pos = n;
129 found = true;
130 break;
131 }
132 }
133 if !found {
134 let delta = bytestream.len() - start_pos;
135 warn!("Can not find {} in bytestream [-{}:{}]!", marker, delta ,bytestream.len());
136 return Err(SerializationError::ValueNotFound);
137 }
138 trace!("Found {marker} at {pos}");
139 Ok(pos)
140}
141
142#[test]
143fn test_seek_marker() {
144 let mut bytestream = vec![1,2,3,0xAA, 0xAA, 5, 7];
146 let mut pos = seek_marker(&bytestream, 0xaaaa, 0).unwrap();
147 assert_eq!(pos, 3);
148
149 bytestream = vec![1,2,3,244, 16, 32, 0xaa, 0xff, 5, 7];
150 pos = seek_marker(&bytestream, 0xffaa, 1).unwrap();
152 assert_eq!(pos, 6);
153
154 bytestream = vec![0xaa,0xaa,3,244, 16, 32, 0xAA, 0xFF, 5, 7];
155 pos = seek_marker(&bytestream, 0xaaaa, 0).unwrap();
156 assert_eq!(pos, 0);
157}
158