ParseFactory

This commit is contained in:
AF 2023-07-29 14:17:01 +00:00
parent f7290cdcfc
commit af813bf51f
5 changed files with 23 additions and 15 deletions

View File

@ -87,7 +87,7 @@ where
/// [`Factory`] associated with the [`Mentionable`]. Mostly useful for `type` definitions.
pub type Fctr<'a, Ctx, A> = <A as MentionableBase<'a, Ctx>>::Fctr;
/// Shorthand for the type of vaalues returned by [`Factory::deserialize`].
/// Shorthand for the type of values returned by [`ParseFactory::deserialize`].
pub type ParseResult<'a, Ctx, F> = Result<Mtbl<'a, Ctx, F>, ParseError<'a, Ctx, F>>;
/// [`ParseResult`] associated with a [`Mentionable`] (instead of a [`Factory`]).
@ -97,21 +97,23 @@ pub type ParseResultA<'a, Ctx, A> = Result<A, ParseErrorA<'a, Ctx, A>>;
pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
/// Type of the associated objects.
type Mtbl: MentionableBase<'a, Ctx, Fctr = Self>;
/// Type of an error that [`Factory::deserialize`] can fail with.
/// Type of an error that [`ParseFactory::deserialize`] can fail with.
type ParseError: 'a + Error;
}
pub trait ParseFactory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> {
/// See [`Deserializer`], [`Resolver`].
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
/// Called by finite stream parsers if there's any data left.
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>;
}
/// Trait representing deserialisation rules for [Mentionable]s.
/// Crucial for [`crate::rstd::typeless`].
pub trait Factory<'a, Ctx: Context<'a>>:
FactoryBase<'a, Ctx, Mtbl = Self::_Mtbl> + ParseMode
ParseFactory<'a, Ctx, Mtbl = Self::_Mtbl> + ParseMode
{
type _Mtbl: MentionableBase<'a, Ctx, Fctr = Self> + MentionableTop<'a, Ctx>;
/// See [`Deserializer`], [`Resolver`].
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
/// Called by finite stream parsers if there's any data left.
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>;
}
/// [`Mentionable`] associated with the [`Factory`]. Mostly useful for `type` definitions.

View File

@ -27,13 +27,11 @@ pub trait FactoryProxy<'a, Ctx: Context<'a>> {
) -> ParseResult<'a, Ctx, Self::F>;
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx> + WithParseMode> Factory<'a, Ctx> for F
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx> + WithParseMode> ParseFactory<'a, Ctx> for F
where
F::WithMode: FactoryProxy<'a, Ctx, F = Self>,
F::Mtbl: MentionableTop<'a, Ctx>,
{
type _Mtbl = Self::Mtbl;
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
<F::WithMode as FactoryProxy<'a, Ctx>>::pdeserialize(self, inctx)
}
@ -43,6 +41,14 @@ where
}
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx> + WithParseMode> Factory<'a, Ctx> for F
where
F::WithMode: FactoryProxy<'a, Ctx, F = Self>,
F::Mtbl: MentionableTop<'a, Ctx>,
{
type _Mtbl = Self::Mtbl;
}
pub struct RegularMode;
pub trait RegularFactory<'a, Ctx: Context<'a>>:

View File

@ -5,7 +5,7 @@ use super::*;
pub enum ResolutionError<L, P> {
/// Usually comes from [`Resolver::resolve`].
Lookup(L),
/// Usually comes from [`Factory::deserialize`].
/// Usually comes from [`ParseFactory::deserialize`].
Parse(P),
}

View File

@ -30,7 +30,7 @@ impl Serializer for Vec<u8> {
/// Trait representing a readable stream used for parsing.
///
/// See [`Serializer`], [`Factory::deserialize`].
/// See [`Serializer`], [`ParseFactory::deserialize`].
pub trait Deserializer {
/// Read at most `n` bytes.
fn read_n(&mut self, n: usize) -> &[u8];

View File

@ -29,9 +29,9 @@ pub trait AtomicBase: 'static + Send + Sync + Send + Clone + Serializable {
}
pub trait Atomic: AtomicBase + ParseMode {
/// Static equivalent of [`Factory::deserialize`].
/// Static equivalent of [`ParseFactory::deserialize`].
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
/// Static equivalent of [`Factory::extend`].
/// Static equivalent of [`ParseFactory::extend`].
fn a_extend(self, tail: &[u8]) -> AParseResult<Self>;
}