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

/// Inlining version of [`ParseResult`]. Preserves the parser.
pub type IParseResult<'a, F, I> = Result<(Mtbl<'a, F>, I), ParseError<'a, F>>;

/// For auto-deriving [`InliningFactory`] from concrete implementations.
pub trait CInliningFactory<'a, Ctx: Context<'a>>:
    FactoryBase<'a> + ImplMode<Mode = InliningMode>
{
    /// Concrete implementation of [`InliningFactory::extension_error`].
    fn cextension_error(&self, tail: &[u8]) -> Self::ParseError;

    /// Concrete implementation of [`InliningFactory::ideserialize`].
    fn cideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Self, I>;
}

/// Factory preserving the parser on success.
pub trait InliningFactory<'a, Ctx: Context<'a>>:
    FactoryBase<'a> + ParseMode<Mode = InliningMode>
{
    /// Always fail on extension,
    /// as parsing of an inlining object should be determined without reaching EOF.
    fn extension_error(&self, tail: &[u8]) -> Self::ParseError;

    /// Inlining version of [`FactoryParse::deserialize`]. Preserves the parser.
    fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Self, I>;
}

impl<'a, Ctx: Context<'a>, F: FactoryModeParse<'a, Ctx> + ParseMode<Mode = InliningMode>>
    InliningFactory<'a, Ctx> for F
{
    fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
        self.mextend((), tail)
    }

    fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Self, I> {
        self.mdeserialize(inctx)
    }
}

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

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

    fn pmextend(
        f: &F,
        _mentionable: ExtensionSourceM<'a, F>,
        tail: &[u8],
    ) -> ExtensionResultM<'a, F> {
        f.cextension_error(tail)
    }
}