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
145
146
use crate::ctx::Context;
use crate::ctx::Match;
use crate::ctx::Span;
use crate::err::Error;
use crate::re::def_not;
use crate::re::Ctor;
use crate::re::Extract;
use crate::re::Handler;
use crate::re::Regex;

pub type DynamicCtorHandler<'a, C, O> = Box<dyn Fn(&mut C) -> Result<O, Error> + 'a>;

pub struct DynamicCtor<'a, C, O> {
    inner: DynamicCtorHandler<'a, C, O>,
}

def_not!(DynamicCtor<'a, C, O>);

impl<'a, C, O> DynamicCtor<'a, C, O> {
    pub fn new(inner: DynamicCtorHandler<'a, C, O>) -> Self {
        Self { inner }
    }

    pub fn with_inner(mut self, inner: DynamicCtorHandler<'a, C, O>) -> Self {
        self.inner = inner;
        self
    }

    pub fn inner(&self) -> &DynamicCtorHandler<'a, C, O> {
        &self.inner
    }

    pub fn inner_mut(&mut self) -> &mut DynamicCtorHandler<'a, C, O> {
        &mut self.inner
    }

    pub fn set_inner(&mut self, inner: DynamicCtorHandler<'a, C, O>) -> &mut Self {
        self.inner = inner;
        self
    }
}

impl<'a, C, R> Regex<C> for DynamicCtor<'a, C, R> {
    type Ret = Span;

    fn try_parse(&self, _: &mut C) -> Result<Self::Ret, Error> {
        unreachable!("Dynamic invoke not support `Regex` trait")
    }
}

impl<'a, 'b, C, M, O> Ctor<'a, C, M, O> for DynamicCtor<'b, C, O>
where
    C: Context<'a> + Match<C>,
{
    #[inline(always)]
    fn constrct<H, A>(&self, ctx: &mut C, _: &mut H) -> Result<O, Error>
    where
        H: Handler<A, Out = M, Error = Error>,
        A: Extract<'a, C, Span, Out<'a> = A, Error = Error>,
    {
        (self.inner)(ctx)
    }
}

///
/// For use in recursive parsers,
/// it will construct a type that implements `Ctor<'_, C, M, O>`
/// from a closure(`Fn(&mut C) -> Result<O, Error>`).
///
/// # Example 1
///
/// ```
/// # use neure::{err::Error, prelude::*, re::RecursiveCtor};
/// #
/// # fn main() -> color_eyre::Result<()> {
/// #     color_eyre::install()?;
///     pub fn parser<'a: 'b, 'b>(
///         ctor: RecursiveCtor<'b, BytesCtx<'a>, &'a [u8]>,
///     ) -> impl Fn(&mut BytesCtx<'a>) -> Result<&'a [u8], Error> + 'b {
///         move |ctx| {
///             let re = u8::is_ascii_lowercase.repeat_one();
///             let re = ctor
///                 .clone()
///                 .or(re)
///                 .quote(b"{", b"}")
///                 .or(ctor.clone().or(re).quote(b"[", b"]"));
///
///             ctx.ctor(&re)
///         }
///     }
///     
///     // into_dyn_ctor using by rec_parser
///     let parser = re::ctor::rec_parser(parser);
///
///     assert_eq!(BytesCtx::new(b"[a]").ctor(&parser)?, b"a");
///     assert_eq!(BytesCtx::new(b"{{[[{[{b}]}]]}}").ctor(&parser)?, b"b");
///     assert_eq!(BytesCtx::new(b"[{{{[c]}}}]").ctor(&parser)?, b"c");
///     Ok(())
/// # }
/// ```
///
/// # Example 2
///
/// ```
/// # use neure::{err::Error, prelude::*};
/// #
/// # fn main() -> color_eyre::Result<()> {
/// #     color_eyre::install()?;
///     let num = u8::is_ascii_digit
///         .repeat_one()
///         .map(|v: &[u8]| String::from_utf8(v.to_vec()).map_err(|_| Error::Uid(0)))
///         .map(map::from_str::<usize>());
///     let num = num.clone().sep_once(b",", num);
///     let re = re::ctor::into_dyn_ctor(|ctx: &mut BytesCtx| ctx.ctor(&num));
///
///     assert_eq!(BytesCtx::new(b"3,0").ctor(&re)?, (3, 0));
///     assert_eq!(BytesCtx::new(b"2,1").ctor(&re)?, (2, 1));
///     assert_eq!(BytesCtx::new(b"0,3").ctor(&re)?, (0, 3));
///     Ok(())
/// # }
/// ```
pub fn into_dyn_ctor<'a, 'b, C, O>(
    invoke: impl Fn(&mut C) -> Result<O, Error> + 'b,
) -> DynamicCtor<'b, C, O>
where
    C: Context<'a>,
{
    DynamicCtor::new(Box::new(invoke))
}

pub trait DynamicCtorHelper<'a, 'b, C, O>
where
    C: Context<'a>,
{
    fn into_dyn_ctor(self) -> DynamicCtor<'b, C, O>;
}

impl<'a, 'b, C, O, T> DynamicCtorHelper<'a, 'b, C, O> for T
where
    C: Context<'a>,
    T: Fn(&mut C) -> Result<O, Error> + 'b,
{
    fn into_dyn_ctor(self) -> DynamicCtor<'b, C, O> {
        DynamicCtor::new(Box::new(self))
    }
}