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
use crate::ByteVisitor;

pub trait VisitPrefixEncode {
    fn visit_pe<BV: ?Sized + ByteVisitor>(&self, visitor: &mut PrefixEncodeVisitor<BV>);

    fn visit_bv<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        let mut prefix_visitor = PrefixEncodeVisitor::new(visitor);
        self.visit_pe(&mut prefix_visitor);
    }
}

pub struct PrefixEncodeVisitor<'a, BV>
where
    BV: ?Sized + ByteVisitor,
{
    buffer: [u8; 10],
    inner: &'a mut BV,
}

impl<'a, BV> PrefixEncodeVisitor<'a, BV>
where
    BV: ?Sized + ByteVisitor,
{
    pub fn new(inner: &'a mut BV) -> Self {
        Self {
            buffer: [0u8; 10],
            inner,
        }
    }

    pub fn visit_unsigned(&mut self, i: u64) {
        let len = leb128::write::unsigned(&mut self.buffer.as_mut_slice(), i).unwrap();
        self.inner.visit_bytes(&self.buffer[..len]);
    }

    pub fn visit_str_raw(&mut self, s: &str) {
        self.inner.visit_bytes(s.as_bytes());
    }

    pub fn visit_str(&mut self, s: &str) {
        self.visit_unsigned(s.len() as u64);
        self.visit_str_raw(s);
    }
}