typeward 0.5.3

A type-driven parser combinator library for Rust.
Documentation
use crate::{error::ParseResult, parse::Parse};

/// A parser that matches `P` without consuming any input.
///
/// Useful for lookahead or peeking at upcoming input without advancing the parser state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Peek<P> {
    value: P,
}

impl<'a, I, P> Parse<'a, I> for Peek<P>
where
    I: crate::input::Input<'a>,
    P: Parse<'a, I>,
{
    #[inline]
    fn parse_with_context(
        input: I,
        context: &mut crate::parse::ParseOffsetContext,
    ) -> ParseResult<(Self, I)> {
        let (value, _) = P::parse_with_context(input, context)?;
        Ok((Peek { value }, input))
    }
}

impl<P, U> crate::parse::Unpack<U> for Peek<P>
where
    P: crate::parse::Unpack<U>,
{
    #[inline]
    fn unpack(self) -> U {
        self.value.unpack()
    }
}

#[cfg(test)]
mod tests {
    use crate::parse::Unpack;
    use crate::{combinators::and::And, primitives::filtered::AlphaString};

    use super::*;

    #[test]
    fn test_peek() {
        let input = "42";
        let (result, rest) = Peek::<i64>::parse(input).unwrap();
        assert_eq!(result.value, 42);
        assert_eq!(rest, "42");
    }

    #[test]
    fn test_peek_and() {
        let input = "42abc";
        let (result, rest) = And::<i64, Peek<AlphaString>>::parse(input).unwrap();
        assert_eq!(result.left, 42);
        assert_eq!(result.right.value, "abc");
        assert_eq!(rest, "abc");
    }

    #[test]
    fn test_peek_unpack() {
        let input = "42";
        let peek = Peek::<i64>::parse(input).unwrap().0;
        let result = peek.unpack();
        assert_eq!(result, 42);
    }
}