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

impl<'a, Ctx: Context<'a>, F: FactoryModeParse<'a, Ctx>> FactoryParse<'a, Ctx> for F {
    fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Self> {
        self.mdeserialize(inctx).map(Self::seal)
    }

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

/// [`FactoryParse`] equivalent of [`ModeResult`].
pub type ModeResultM<'a, F, I> = ModeResultP<F, Mtbl<'a, F>, ParseError<'a, F>, I>;

/// [`FactoryParse`] equivalent of [`ExtensionResult`].
pub type ExtensionResultM<'a, F> = ExtensionResultP<F, Mtbl<'a, F>, ParseError<'a, F>>;

/// [`FactoryParse`] equivalent of [`ExtensionSource`].
pub type ExtensionSourceM<'a, F> = ExtensionSourceP<F, Mtbl<'a, F>>;

/// A more generic version of [`FactoryParse`].
pub trait FactoryModeParse<'a, Ctx: Context<'a>>: FactoryBase<'a> + ParseMode {
    /// A more generic version of [`FactoryParse::deserialize`].
    fn mdeserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> ModeResultM<'a, Self, I>;

    /// A more generic version of [`FactoryParse::extend`].
    fn mextend(
        &self,
        mentionable: ExtensionSourceM<'a, Self>,
        tail: &[u8],
    ) -> ExtensionResultM<'a, Self>;
}

/// External implementation of [`FactoryModeParse`].
pub trait FactoryModeProxy<'a, Ctx: Context<'a>> {
    /// Associated [`FactoryModeParse`].
    type F: FactoryBase<'a> + ParseMode;

    /// External implementation of [`FactoryModeParse::mdeserialize`].
    fn pmdeserialize<I: InCtx<'a, Ctx>>(f: &Self::F, inctx: I) -> ModeResultM<'a, Self::F, I>;

    /// External implementation of [`FactoryModeParse::mextend`].
    fn pmextend(
        f: &Self::F,
        mentionable: ExtensionSourceM<'a, Self::F>,
        tail: &[u8],
    ) -> ExtensionResultM<'a, Self::F>;
}

impl<'a, Ctx: Context<'a>, F: FactoryBase<'a> + WithParseMode> FactoryModeParse<'a, Ctx> for F
where
    F::WithMode: FactoryModeProxy<'a, Ctx, F = F>,
{
    fn mdeserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> ModeResultM<'a, Self, I> {
        <F::WithMode as FactoryModeProxy<'a, Ctx>>::pmdeserialize(self, inctx)
    }

    fn mextend(
        &self,
        mentionable: ExtensionSourceM<'a, Self>,
        tail: &[u8],
    ) -> ExtensionResultM<'a, Self> {
        <F::WithMode as FactoryModeProxy<'a, Ctx>>::pmextend(self, mentionable, tail)
    }
}