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
use std::fmt::Debug;
use std::marker::PhantomData;

use crate::ctx::Context;
use crate::ctx::Match;
use crate::ctx::Ret;
use crate::ctx::Span;
use crate::err::Error;
use crate::re::Regex;

use super::def_not;
use super::trace;
use super::Ctor;
use super::Extract;
use super::Handler;

#[derive(Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NullRegex<R>(PhantomData<R>);

def_not!(NullRegex<R>);

impl<R> Debug for NullRegex<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("NullRegex").field(&self.0).finish()
    }
}

impl<R> Clone for NullRegex<R> {
    fn clone(&self) -> Self {
        Self(self.0)
    }
}

impl<R> NullRegex<R> {
    pub fn new() -> Self {
        Self(PhantomData)
    }
}

impl<'a, C, R> Regex<C> for NullRegex<R>
where
    R: Ret,
    C: Context<'a>,
{
    type Ret = R;

    #[inline(always)]
    fn try_parse(&self, ctx: &mut C) -> Result<Self::Ret, Error> {
        let beg = ctx.offset();
        let ret = Ok(<R as Ret>::from_ctx(ctx, (0, 0)));

        trace!("null", beg => ctx.offset(), ret)
    }
}

impl<'a, C, O> Ctor<'a, C, O, O> for NullRegex<Span>
where
    C: Context<'a> + Match<C>,
{
    #[inline(always)]
    fn constrct<H, A>(&self, ctx: &mut C, handler: &mut H) -> Result<O, Error>
    where
        H: Handler<A, Out = O, Error = Error>,
        A: Extract<'a, C, Span, Out<'a> = A, Error = Error>,
    {
        let beg = ctx.offset();
        let ret = ctx.try_mat(self);

        trace!("null", beg -> ctx.offset(), ret.is_ok());
        handler.invoke(A::extract(ctx, &ret?)?)
    }
}