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
use crate::{
    apply::Apply,
    ruled::Ruled,
};

#[derive(Copy, Clone, Debug)]
pub struct Not<A>(pub A);

impl<I, A> Apply<I> for Not<A>
    where
        A: Apply<I>,
        I: Copy,
{
    type Err = A::Res;
    type Res = A::Err;

    fn apply(self, input: I) -> Ruled<I, Self::Res, Self::Err> {
        match self.0.apply(input) {
            Ruled::Ok(r, _) => Ruled::Err(r),
            Ruled::Err(e) => Ruled::Ok(e, input),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        apply::apply,
        rule::rule,
        expected::Expected,
    };

    #[test]
    fn not() {
        let r = !rule('a');
        assert_eq!(apply(r, "a"), Ruled::Err("a"));
        assert_eq!(apply(r, "b"), Ruled::Ok(Expected::Char('a'), "b"));
    }
}