Skip to content

Commit

Permalink
support single element tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Sep 25, 2019
1 parent c326e07 commit 9d49820
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ pub trait Tuple<I,O,E> {
fn parse(&self, input: I) -> IResult<I,O,E>;
}

impl<Input, Output, Error: ParseError<Input>, F: Fn(Input) -> IResult<Input, Output, Error> > Tuple<Input, (Output,), Error> for (F,) {
fn parse(&self, input: Input) -> IResult<Input,(Output,),Error> {
self.0(input).map(|(i,o)| (i, (o,)))
}
}

macro_rules! tuple_trait(
($name1:ident $ty1:ident, $name2: ident $ty2:ident, $($name:ident $ty:ident),*) => (
tuple_trait!(__impl $name1 $ty1, $name2 $ty2; $($name $ty),*);
Expand Down Expand Up @@ -290,3 +296,18 @@ pub fn tuple<I: Clone, O, E: ParseError<I>, List: Tuple<I,O,E>>(l: List) -> imp
l.parse(i)
}
}

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

#[test]
fn single_element_tuples() {
use crate::character::complete::{alpha1, digit1};
use crate::{Err, error::ErrorKind};

let parser = tuple((alpha1,));
assert_eq!(parser("abc123def"), Ok(("123def", ("abc",))));
assert_eq!(parser("123def"), Err(Err::Error(("123def", ErrorKind::Alpha))));
}
}

0 comments on commit 9d49820

Please sign in to comment.