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
use crate::*;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Either<A, B> {
    A(A),
    B(B),
}

pub struct Or<Iter: Iterator + Clone, A: Parser<Iter>, B: Parser<Iter>>
where
    Iter: Iterator + Clone,
    A: Parser<Iter>,
    B: Parser<Iter>,
{
    a: A,
    b: B,
    _i: PhantomData<Iter>,
}

impl<Iter, A, B> Or<Iter, A, B>
where
    Iter: Iterator + Clone,
    A: Parser<Iter>,
    B: Parser<Iter>,
{
    pub(crate) fn new(a: A, b: B) -> Self {
        Self {
            a,
            b,
            _i: Default::default(),
        }
    }
}

impl<Iter, A, B> Parser<Iter> for Or<Iter, A, B>
where
    Iter: Iterator + Clone,
    A: Parser<Iter>,
    B: Parser<Iter>,
{
    type Output = Either<A::Output, B::Output>;
    fn parse(&self, i: Iter) -> Option<(Iter, Self::Output)> {
        match (self.a.parse(i.clone()), self.b.parse(i)) {
            (Some((i, a)), None) => Some((i, Either::A(a))),
            (None, Some((i, b))) => Some((i, Either::B(b))),
            (Some((ia, a)), Some((ib, b))) => {
                if ia.size_hint().1 <= ib.size_hint().1 {
                    Some((ia, Either::A(a)))
                } else {
                    Some((ib, Either::B(b)))
                }
            }
            (None, None) => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;

    #[test]
    fn equal_length_both() {
        assert_eq!(('a'.or('a')).parse_str("abc"), Some(("bc", A('a'))))
    }

    #[test]
    fn equal_length_a() {
        assert_eq!(('a'.or('b')).parse_str("abc"), Some(("bc", A('a'))))
    }

    #[test]
    fn equal_length_b() {
        assert_eq!(('b'.or('a')).parse_str("abc"), Some(("bc", B('a'))))
    }

    #[test]
    fn fail() {
        assert_eq!(('b'.or('c')).parse_str("abc"), None)
    }

    #[test]
    fn use_longest_if_both() {
        assert_eq!(
            ("hell".or("hello")).parse_str("hello world"),
            Some((" world", B("hello")))
        )
    }
}