typeward 0.5.3

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

/// Parses `A` followed by `B` and retains both outputs.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct And<A, B> {
    /// Output parsed by `A`.
    pub left: A,
    /// Output parsed by `B`.
    pub right: B,
}

impl<A, B> And<A, B> {
    /// Creates a pair from two parsed values.
    pub fn new(left: A, right: B) -> Self {
        Self { left, right }
    }
    /// Returns the first parsed value.
    pub fn left(&self) -> &A {
        &self.left
    }
    /// Returns the second parsed value.
    pub fn right(&self) -> &B {
        &self.right
    }
    /// Returns a mutable reference to the first parsed value.
    pub fn left_mut(&mut self) -> &mut A {
        &mut self.left
    }
    /// Returns a mutable reference to the second parsed value.
    pub fn right_mut(&mut self) -> &mut B {
        &mut self.right
    }
    /// Splits the pair into its two parsed values.
    pub fn into_parts(self) -> (A, B) {
        (self.left, self.right)
    }
    /// Adds a third value to the pair, returning a nested `And` structure.
    pub fn and<C>(self, c: C) -> And<A, And<B, C>> {
        And {
            left: self.left,
            right: And {
                left: self.right,
                right: c,
            },
        }
    }
}

impl<A, B, C> And<A, And<B, C>> {
    /// Flattens a nested pair into a three-element tuple.
    pub fn flatten(self) -> (A, B, C) {
        (self.left, self.right.left, self.right.right)
    }
}

/// Unpacks a nested [`And`] value into a tuple.
#[deprecated(
    since = "0.5.0",
    note = "Use the `Unpack` trait instead, which provides a more flexible and ergonomic way to unpack nested `And` values."
)]
#[allow(deprecated)]
#[macro_export]
macro_rules! unpack_and {
    ($val:expr, ($single:ty $(,)?)) => {
        ($val,)
    };
    ($val:expr, ($head:ty, $($tail:ty),+ $(,)?)) => {
        unpack_and!(@collect $val; ; $head, $($tail),+)
    };
    (@collect $val:expr; $($out:expr,)* ; $head:ty, $($tail:ty),+) => {{
        let $crate::combinators::and::And { left, right } = $val;
        unpack_and!(@collect right; $($out,)* left, ; $($tail),+)
    }};
    (@collect $val:expr; $($out:expr,)* ; $last:ty) => {
        ($($out,)* $val)
    };
}

macro_rules! unpack_tuple_and {
    ($($t:ty),+) => {
        pastey::paste! {
          impl <
              $($t),+ ,
              $([< T $t  >]),*
          > $crate::parse::Unpack<($([< T $t >]),+)>
          for $crate::and!($($t),+)
          where
              $($t: $crate::parse::Unpack<[< T $t >]>,)+
          {
              #[inline]
              #[allow(deprecated)]
              fn unpack(self) -> ($([< T $t >]),+) {
                  let ($([< $t:lower >],)*) = unpack_and!(self, ($($t),+));
                  ($([< $t:lower >].unpack(),)+)
              }
          }
        }
    };
}

for_all_tuples!(unpack_tuple_and);

impl<'a, I, A, B> Parse<'a, I> for And<A, B>
where
    I: crate::input::Input<'a>,
    A: Parse<'a, I>,
    B: Parse<'a, I>,
{
    #[inline]
    fn parse_with_context(
        input: I,
        context: &mut crate::parse::ParseOffsetContext,
    ) -> ParseResult<(Self, I)> {
        let (left, remaining) = A::parse_with_context(input, context)?;
        let (right, remaining) = B::parse_with_context(remaining, context)?;
        Ok((And { left, right }, remaining))
    }
}

#[macro_export]
/// Builds the nested [`And`] type for a sequence of parser types.
macro_rules! and {
    ($a:ty, $b:ty) => {
        $crate::combinators::and::And<$a, $b>
    };
    ($a:ty, $($rest:ty),+ $(,)?) => {
        $crate::combinators::and::And<$a, $crate::and!($($rest),+)>
    };
}

#[macro_export]
/// Builds a nested [`And`] value from parser outputs.
macro_rules! new_and {
    ($a:expr, $b:expr) => {
        $crate::combinators::and::And::new($a, $b)
    };
    ($a:expr, $($rest:expr),+ $(,)?) => {
        $crate::combinators::and::And::new($a, $crate::new_and!($($rest),+))
    };
}

#[cfg(test)]
mod tests {
    use crate::parse::Unpack;

    #[test]
    fn unpack_and_two_values() {
        let value = crate::new_and!(1_u8, 2_u16);
        let tuple: (u8, u16) = value.unpack();
        assert_eq!(tuple, (1, 2));
    }

    #[test]
    fn unpack_and_three_values() {
        let value = crate::new_and!(1_u8, 2_u16, 3_u32);
        let tuple: (u8, u16, u32) = value.unpack();
        assert_eq!(tuple, (1, 2, 3));
    }

    #[test]
    fn unpack_and_four_values() {
        let value = crate::new_and!(1_u8, 2_u16, 3_u32, 4_u64);
        let tuple: (u8, u16, u32, u64) = value.unpack();
        assert_eq!(tuple, (1, 2, 3, 4));
    }
}