1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::{fmt::Debug, marker::PhantomData};

use super::Neu;

///
/// Return true if the value matches both `L` and `R`.
///
/// # Example
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> color_eyre::Result<()> {
/// #     color_eyre::install()?;
///     let large_than = |c: &char| *c > '7';
///     let digit = neu::digit(10).and(large_than).repeat::<1, 3>();
///     let mut ctx = CharsCtx::new("899");
///
///     assert_eq!(ctx.try_mat(&digit)?, Span::new(0, 3));
///
///     let digit = re!((neu::digit(10).and(large_than)){1,3});
///     let mut ctx = CharsCtx::new("99c");
///
///     assert_eq!(ctx.try_mat(&digit)?, Span::new(0, 2));
///
///     Ok(())
/// # }
/// ```
#[derive(Default, Copy)]
pub struct And<L, R, T>
where
    L: Neu<T>,
    R: Neu<T>,
{
    left: L,
    right: R,
    marker: PhantomData<T>,
}

impl<L, R, T> Debug for And<L, R, T>
where
    L: Neu<T> + Debug,
    R: Neu<T> + Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("And")
            .field("left", &self.left)
            .field("right", &self.right)
            .finish()
    }
}

impl<L, R, T> Clone for And<L, R, T>
where
    L: Neu<T> + Clone,
    R: Neu<T> + Clone,
{
    fn clone(&self) -> Self {
        Self {
            left: self.left.clone(),
            right: self.right.clone(),
            marker: self.marker,
        }
    }
}

impl<L, R, T> And<L, R, T>
where
    L: Neu<T>,
    R: Neu<T>,
{
    pub fn new(left: L, right: R) -> Self {
        Self {
            left,
            right,
            marker: PhantomData,
        }
    }

    pub fn left(&self) -> &L {
        &self.left
    }

    pub fn right(&self) -> &R {
        &self.right
    }

    pub fn left_mut(&mut self) -> &mut L {
        &mut self.left
    }

    pub fn right_mut(&mut self) -> &mut R {
        &mut self.right
    }

    pub fn set_left(&mut self, left: L) -> &mut Self {
        self.left = left;
        self
    }

    pub fn set_right(&mut self, right: R) -> &mut Self {
        self.right = right;
        self
    }
}

impl<L, R, T> Neu<T> for And<L, R, T>
where
    L: Neu<T>,
    R: Neu<T>,
{
    #[inline(always)]
    fn is_match(&self, other: &T) -> bool {
        let ret = self.left.is_match(other) && self.right.is_match(other);

        crate::trace_log!("neu logical `and` -> {ret}");
        ret
    }
}

///
/// Return true if the value matches both `L` and `R`.
///
/// # Example
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> color_eyre::Result<()> {
/// #     color_eyre::install()?;
///     const BEG: u8 = 'a' as u8 - 1;
///     const END: u8 = 'z' as u8 + 1;
///
///     let char = neu::and(|a: &u8| a > &BEG, |a: &u8| a < &END);
///     let str = char.repeat_range(6..);
///     let mut ctx = BytesCtx::new(br#"abcedfgABCEE"#);
///
///     assert_eq!(ctx.try_mat(&str)?, Span::new(0, 7));
///     Ok(())
/// # }
/// ```
pub fn and<T, L: Neu<T>, R: Neu<T>>(left: L, right: R) -> And<L, R, T> {
    And::new(left, right)
}