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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::ops::{Deref, Index, RangeFrom};

/// Input to parsers should either be a `&str` or a slice (i.e. `&[T]`)
///
/// If you have either of those you can convert it into an `Input` by using `.into()` or
/// `Into::from()`
///
/// `Input` implements `Deref<Target = T>`, and so inherits all of `T`s methods.
///
/// `Input` implements `PartialEq<Rhs=T>`, so you can compare an instance of `Input<T>` with a `T`
#[derive(Debug, Clone)]
pub struct Input<T> {
    input: T,
    length: usize,
    position: usize,
}

impl<'i> From<&'i str> for Input<&'i str> {
    fn from(value: &'i str) -> Self {
        Input {
            input: value,
            length: value.len(),
            position: 0,
        }
    }
}

impl<'i, T> From<&'i [T]> for Input<&'i [T]> {
    fn from(value: &'i [T]) -> Self {
        Input {
            input: value,
            length: value.len(),
            position: 0,
        }
    }
}

impl PartialEq<&str> for Input<&str> {
    fn eq(&self, other: &&str) -> bool {
        &self.input[self.position..] == *other
    }
}

impl<T> PartialEq<&[T]> for Input<&[T]>
where
    [T]: Eq,
{
    fn eq(&self, other: &&[T]) -> bool {
        &self.input[self.position..] == *other
    }
}

impl<T> Deref for Input<T>
where
    T: Deref,
    <T as Deref>::Target: Index<RangeFrom<usize>>,
{
    type Target = <<T as Deref>::Target as Index<RangeFrom<usize>>>::Output;

    fn deref(&self) -> &Self::Target {
        // This is where the magic of the `Input` struct comes from. It holds the entire parse
        // input, but to individual parsers, will only appear to hold the part of the input they
        // care about.
        &self.input[self.position..]
    }
}

impl<T> Input<T>
where
    T: Clone,
{
    /// Returns a new instance of `Input` with `count` elements removed from the start
    pub fn pop(&self, count: usize) -> Input<T> {
        let offset = if self.position + count <= self.length {
            count
        } else {
            0
        };

        Input {
            input: self.input.clone(),
            length: self.length,
            position: self.position + offset,
        }
    }
}

impl<T> Input<T> {
    /// Returns the current position
    pub fn get_position(&self) -> usize {
        self.position
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_equality() {
        let str = "A";
        let input = Input::from(str);
        assert_eq!(input, str);
        assert_eq!(input, "A");

        let slice = &[0, 2, 4][..];
        let input = Input::from(slice);
        assert_eq!(input, slice);
        assert_eq!(input, &[0, 2, 4]);
    }

    #[test]
    fn test_deref_magic() {
        let str = "ABC";

        let input = Input::from(str);

        assert_eq!(input, "ABC");

        let input = input.pop(1);
        assert_eq!(input, "BC");
        assert_eq!(&input[0..], "BC");

        let input = input.pop(1);
        assert_eq!(input, "C");
        assert_eq!(&input[0..], "C");

        let input = input.pop(1);
        assert_eq!(input, "");
        assert_eq!(&input[0..], "");

        let input = input.pop(10).pop(1).pop(3);
        assert_eq!(input, "");
        assert_eq!(&input[0..], "");
    }
}