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
use super::*;

/// For auto-deriving [`RegularFactory`] from concrete implementations.
pub trait CRegularFactory<'a, Ctx: Context<'a>>:
    FactoryBase<'a> + ImplMode<Mode = RegularMode>
{
    /// Concrete implementation of [`RegularFactory::rdeserialize`].
    fn crdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Self>;

    /// Concrete implementation of [`RegularFactory::rextend`].
    fn crextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Self>;
}

/// Mostly same as [`FactoryModeParse`] but requires [`Mode`] to be [`RegularMode`].
pub trait RegularFactory<'a, Ctx: Context<'a>>:
    FactoryBase<'a> + ParseMode<Mode = RegularMode>
{
    /// Same as [`FactoryModeParse::mdeserialize`].
    fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Self>;

    /// Same as [`FactoryModeParse::mextend`].
    fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Self>;
}

impl<'a, Ctx: Context<'a>, F: FactoryModeParse<'a, Ctx> + ParseMode<Mode = RegularMode>>
    RegularFactory<'a, Ctx> for F
{
    fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Self> {
        self.mdeserialize(inctx)
    }

    fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Self> {
        self.mextend(mentionable, tail)
    }
}

impl<'a, Ctx: Context<'a>, F: CRegularFactory<'a, Ctx>> FactoryModeProxy<'a, Ctx>
    for WithMode<F, RegularMode>
{
    type F = F;

    fn pmdeserialize<I: InCtx<'a, Ctx>>(f: &Self::F, inctx: I) -> ModeResultM<'a, F, I> {
        f.crdeserialize(inctx)
    }

    fn pmextend(f: &F, mentionable: Mtbl<'a, F>, tail: &[u8]) -> ExtensionResultM<'a, F> {
        f.crextend(mentionable, tail)
    }
}