use crate::{error::ParseResult, parse::Parse};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct And<A, B> {
pub left: A,
pub right: B,
}
impl<A, B> And<A, B> {
pub fn new(left: A, right: B) -> Self {
Self { left, right }
}
pub fn left(&self) -> &A {
&self.left
}
pub fn right(&self) -> &B {
&self.right
}
pub fn left_mut(&mut self) -> &mut A {
&mut self.left
}
pub fn right_mut(&mut self) -> &mut B {
&mut self.right
}
pub fn into_parts(self) -> (A, B) {
(self.left, self.right)
}
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>> {
pub fn flatten(self) -> (A, B, C) {
(self.left, self.right.left, self.right.right)
}
}
#[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]
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]
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));
}
}