1#![allow(missing_docs)]
3
4use bstr::BStr;
5use uuid::Uuid;
6use zerocopy::{Immutable, IntoBytes};
7
8pub struct Encoder<'a> {
10 pub buf: &'a mut Vec<u8>,
11}
12
13impl<'a> Encoder<'a> {
14 pub fn new(buf: &'a mut Vec<u8>) -> Self {
15 Self { buf }
16 }
17
18 pub fn len(&self) -> usize {
19 self.buf.len()
20 }
21
22 pub fn is_empty(&self) -> bool {
23 self.buf.is_empty()
24 }
25
26 pub fn u8(&mut self, x: u8) {
27 self.buf.push(x);
28 }
29
30 pub fn bytes(&mut self, b: &[u8]) {
31 self.buf.extend_from_slice(b);
32 }
33
34 pub fn u16(&mut self, x: u16) {
35 self.bytes(&x.to_le_bytes());
36 }
37
38 pub fn u32(&mut self, x: u32) {
39 self.bytes(&x.to_le_bytes());
40 }
41
42 pub fn t<T: IntoBytes + Immutable>(&mut self, x: &T) {
43 self.buf.extend_from_slice(x.as_bytes());
44 }
45
46 pub fn strz(&mut self, s: &BStr) {
47 self.buf.extend_from_slice(s);
48 self.buf.push(0);
49 }
50
51 pub fn uuid(&mut self, u: &Uuid) {
52 self.bytes(&u.to_bytes_le())
53 }
54}