1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
pub mod branch;
pub mod primitive;
pub mod missing;
pub mod error;
pub mod reader_writer;
pub mod prelude;
#[cfg(test)]
mod tests;
use crate::prelude::*;
const PREAMBLE: [u8; 7] = [84, 82, 69, 69, 66, 85, 70];
pub fn write<T: Writable>(value: &T) -> Vec<u8>
{
let mut writer = T::Writer::new();
writer.write(value);
let mut bytes = Vec::new();
bytes.extend_from_slice(&PREAMBLE);
writer.flush(&BranchId { name: "", parent: 0 }, &mut bytes);
bytes
}
pub fn read<T: Readable>(bytes: &[u8]) -> Result<T, Error> {
let mut offset = 0;
assert_eq!(&PREAMBLE, &bytes[offset..offset+PREAMBLE.len()], "Not valid file");
offset += PREAMBLE.len();
let mut sticks = Vec::new();
while offset < bytes.len() {
sticks.push(Stick::read(bytes, &mut offset));
}
let branch = BranchId { name: "", parent: 0 };
let mut reader = T::Reader::new(&sticks, &branch);
Ok(reader.read())
}