1use super::*;
5use ::either::Either;
6
7impl<'src, L, R, I, O, E> Parser<'src, I, O, E> for Either<L, R>
8where
9 I: Input<'src>,
10 E: ParserExtra<'src, I>,
11 L: Parser<'src, I, O, E>,
12 R: Parser<'src, I, O, E>,
13{
14 fn go<M: crate::private::Mode>(
15 &self,
16 inp: &mut crate::input::InputRef<'src, '_, I, E>,
17 ) -> crate::private::PResult<M, O>
18 where
19 Self: Sized,
20 {
21 match self {
22 Either::Left(l) => L::go::<M>(l, inp),
23 Either::Right(r) => R::go::<M>(r, inp),
24 }
25 }
26
27 go_extra!(O);
28}
29
30#[cfg(test)]
31mod tests {
32 use crate::{
33 prelude::{any, just},
34 IterParser, Parser,
35 };
36 use either::Either;
37
38 fn parser<'src>() -> impl Parser<'src, &'src str, Vec<u64>> {
39 any()
40 .filter(|c: &char| c.is_ascii_digit())
41 .repeated()
42 .at_least(1)
43 .at_most(3)
44 .to_slice()
45 .map(|b: &str| b.parse::<u64>().unwrap())
46 .padded()
47 .separated_by(just(',').padded())
48 .allow_trailing()
49 .collect()
50 .delimited_by(just('['), just(']'))
51 }
52
53 #[test]
54 fn either() {
55 let parsers = [Either::Left(parser()), Either::Right(parser())];
56 for parser in parsers {
57 assert_eq!(
58 parser.parse("[122 , 23,43, 4, ]").into_result(),
59 Ok(vec![122, 23, 43, 4]),
60 );
61 assert_eq!(
62 parser.parse("[0, 3, 6, 900,120]").into_result(),
63 Ok(vec![0, 3, 6, 900, 120]),
64 );
65 assert_eq!(
66 parser.parse("[200,400,50 ,0,0, ]").into_result(),
67 Ok(vec![200, 400, 50, 0, 0]),
68 );
69
70 assert!(parser.parse("[1234,123,12,1]").has_errors());
71 assert!(parser.parse("[,0, 1, 456]").has_errors());
72 assert!(parser.parse("[3, 4, 5, 67 89,]").has_errors());
73 }
74 }
75}