diff --git a/src/sequence/mod.rs b/src/sequence/mod.rs index 478e236b2..a0eafb97b 100644 --- a/src/sequence/mod.rs +++ b/src/sequence/mod.rs @@ -224,6 +224,12 @@ pub trait Tuple { fn parse(&self, input: I) -> IResult; } +impl, F: Fn(Input) -> IResult > Tuple for (F,) { + fn parse(&self, input: Input) -> IResult { + 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),*); @@ -290,3 +296,18 @@ pub fn tuple, List: Tuple>(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)))); + } +}