shrimple_parser/input.rs
1/// This trait represents input that can be parsed by a [Parser] and/or matched by a [Pattern]
2///
3/// Its [`Default`] impl of the type should return a value that represents empty input, akin to an
4/// empty string, `""`
5///
6/// [Parser]: crate::Parser
7/// [Pattern]: crate::pattern::Pattern
8pub trait Input:
9 Sized + Clone + core::fmt::Debug + Default + core::ops::Deref<Target = str>
10{
11 /// A generalisation of [`str::split_at`]
12 #[must_use]
13 fn split_at(self, mid: usize) -> (Self, Self);
14
15 /// Equivalent to `self.split_at(mid).0`, but can be overridden to provide a more optimal
16 /// implementation
17 #[must_use]
18 fn before(self, index: usize) -> Self {
19 self.split_at(index).0
20 }
21
22 /// Equivalent to `self.split_at(mid).1`, but can be overriden to provide a more optimal
23 /// implementation
24 #[must_use]
25 fn after(self, index: usize) -> Self {
26 self.split_at(index).1
27 }
28}
29
30impl Input for &str {
31 fn split_at(self, mid: usize) -> (Self, Self) {
32 str::split_at(self, mid)
33 }
34
35 fn before(self, index: usize) -> Self {
36 &self[..index]
37 }
38
39 fn after(self, index: usize) -> Self {
40 &self[index..]
41 }
42}