1use super::Control;
2use byteorder::{LittleEndian, WriteBytesExt};
3use integer_encoding::VarIntWriter;
4use std::io::{self, Write};
5
6pub const MAGIC: u32 = 0xB1DF;
7pub const VERSION: u32 = 0x1000;
8
9pub struct Writer<W>
10where
11 W: Write,
12{
13 w: W,
14}
15
16impl<W> Writer<W>
17where
18 W: Write,
19{
20 pub fn new(mut w: W) -> Result<Self, io::Error> {
21 w.write_u32::<LittleEndian>(MAGIC)?;
22 w.write_u32::<LittleEndian>(VERSION)?;
23
24 Ok(Self { w })
25 }
26
27 pub fn write(&mut self, c: &Control) -> Result<(), io::Error> {
28 let w = &mut self.w;
29
30 w.write_varint(c.add.len())?;
31 w.write_all(c.add)?;
32
33 w.write_varint(c.copy.len())?;
34 w.write_all(c.copy)?;
35
36 w.write_varint(c.seek)?;
37
38 Ok(())
39 }
40
41 pub fn flush(&mut self) -> Result<(), io::Error> {
42 self.w.flush()
43 }
44
45 pub fn into_inner(self) -> W {
46 self.w
47 }
48}