nessie_parse/
combine_fail.rs

1use crate::State;
2
3pub trait CombineFail<'a, Rhs, Out> {
4    fn combine_fail(self, self_state: State<'a>, other: Rhs, other_state: State<'a>) -> Out;
5}
6
7pub trait CombineManyFail<'text, Out>: Sized {
8    fn combine_many_fail(fails: Vec<(Self, State<'text>)>) -> Out;
9}
10
11// Let's have some useful implementations!
12
13// Implementations for combining to ().
14
15impl<F1, F2> CombineFail<'_, F2, ()> for F1 {
16    fn combine_fail(self, _: State, _: F2, _: State) {}
17}
18
19impl<F> CombineManyFail<'_, ()> for F {
20    fn combine_many_fail(_: Vec<(Self, State)>) {}
21}
22
23// Now for just saving everything
24
25impl<'a, F1, F2> CombineFail<'a, F2, (F1, State<'a>, F2, State<'a>)> for F1 {
26    fn combine_fail(
27        self,
28        s1: State<'a>,
29        other: F2,
30        s2: State<'a>,
31    ) -> (F1, State<'a>, F2, State<'a>) {
32        (self, s1, other, s2)
33    }
34}
35
36impl<'a, F> CombineManyFail<'a, Vec<(F, State<'a>)>> for F {
37    fn combine_many_fail(fails: Vec<(Self, State)>) -> Vec<(Self, State)> {
38        fails
39    }
40}
41
42// Now for saving the failure, but not the parsing state.
43
44impl<F1, F2> CombineFail<'_, F2, (F1, F2)> for F1 {
45    fn combine_fail(self, _: State, other: F2, _: State) -> (F1, F2) {
46        (self, other)
47    }
48}
49
50impl<F> CombineManyFail<'_, Vec<F>> for F {
51    fn combine_many_fail(fails: Vec<(Self, State)>) -> Vec<Self> {
52        fails.into_iter().map(|(f, _)| f).collect()
53    }
54}