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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::ctx::Context;
use crate::err::Error;
use crate::iter::BytesIndices;
use crate::span::SpanStorer;

#[derive(Debug, Default)]
pub struct BytesCtx<'a> {
    bytes: &'a [u8],
    offset: usize,
}

impl<'a> BytesCtx<'a> {
    pub fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, offset: 0 }
    }

    pub fn with_str(mut self, bytes: &'a [u8]) -> Self {
        self.bytes = bytes;
        self
    }

    pub fn with_offset(mut self, offset: usize) -> Self {
        self.offset = offset;
        self
    }

    pub fn reset_with(&mut self, bytes: &'a [u8]) -> &mut Self {
        self.bytes = bytes;
        self.offset = 0;
        self
    }

    pub fn reset(&mut self) -> &mut Self {
        self.offset = 0;
        self
    }

    pub fn span_storer(&self, capacity: usize) -> SpanStorer {
        SpanStorer::new(capacity)
    }
}

impl<'a> Context for BytesCtx<'a> {
    type Orig = [u8];

    type Item = u8;

    type Iter<'b> = BytesIndices<'b> where Self: 'b;

    fn len(&self) -> usize {
        self.bytes.len()
    }

    fn offset(&self) -> usize {
        self.offset
    }

    fn inc(&mut self, offset: usize) -> &mut Self {
        self.offset += offset;
        self
    }

    fn dec(&mut self, offset: usize) -> &mut Self {
        self.offset -= offset;
        self
    }

    fn orig_at(&self, offset: usize) -> Result<&Self::Orig, Error> {
        self.bytes.get(offset..).ok_or(Error::ReachEnd)
    }

    fn peek_at(&self, offset: usize) -> Result<Self::Iter<'_>, Error> {
        Ok(BytesIndices::new(self.orig_at(offset)?))
    }
}