pratt_gen_core/
source.rs

1#[derive(Debug, Clone, Copy)]
2pub struct Source<'a> {
3    pub split: usize,
4    pub inner: &'a str,
5}
6
7impl<'a> Source<'a> {
8    pub fn proceed(&self, by: usize) -> Self {
9        Source { split: self.split + by, inner: self.inner }
10    }
11    pub fn new(inner: &'a str) -> Self {
12        Self { inner, split: 0 }
13    }
14}
15
16impl<'a, T: std::ops::RangeBounds<usize>> std::ops::Index<T> for Source<'a> {
17    type Output = str;
18    fn index(&self, index: T) -> &Self::Output {
19        use std::ops::Bound::*;
20        let start = match index.start_bound() {
21            Excluded(x) => self.split + x + 1,
22            Included(x) => self.split + x,
23            Unbounded => self.split,
24        };
25        let end = match index.end_bound() {
26            Excluded(x) => self.split + x,
27            Included(x) => self.split + x + 1,
28            Unbounded => self.inner.len(),
29        }.min(self.inner.len());
30        &self.inner[start..end]
31    }
32}