Skip to main content

shrimple_parser/
input.rs

1/// This trait represents input that can be parsed by a [Parser] and/or matched by a [Pattern].
2///
3/// An [`Input`] must be cheaply cloneable, which is why types like `Box<str>` or `String` don't
4/// implement this trait.
5///
6/// If you want to create an
7/// empty span of the input of a parser, better carve it out of the input to keep the pointer
8/// inside it. Even an empty string can provide info on its location in the source code.
9/// [`Input::start`] & [`Input::end`] methods will help you with that.
10///
11/// [Parser]: crate::Parser
12/// [Pattern]: crate::pattern::Pattern
13pub trait Input:
14    Sized + Clone + core::fmt::Debug + core::ops::Deref<Target = str>
15{
16    /// A generalisation of [`str::split_at`]
17    #[must_use]
18    fn split_at(self, mid: usize) -> (Self, Self);
19
20    /// Equivalent to `self.split_at(mid).0`, but can be overridden to provide a more optimal
21    /// implementation
22    #[must_use]
23    fn before(self, index: usize) -> Self {
24        self.split_at(index).0
25    }
26
27    /// Equivalent to `self.split_at(mid).1`, but can be overriden to provide a more optimal
28    /// implementation
29    #[must_use]
30    fn after(self, index: usize) -> Self {
31        self.split_at(index).1
32    }
33
34    /// Returns an empty string that points to the start of the input
35    fn start(self) -> Self {
36        self.before(0)
37    }
38    
39    /// Returns an empty string that points to the end of the input
40    fn end(self) -> Self {
41        let len = self.len();
42        self.after(len)
43    }
44}
45
46impl Input for &str {
47    fn split_at(self, mid: usize) -> (Self, Self) {
48        str::split_at(self, mid)
49    }
50
51    fn before(self, index: usize) -> Self {
52        &self[..index]
53    }
54
55    fn after(self, index: usize) -> Self {
56        &self[index..]
57    }
58}