ParseFactory
This commit is contained in:
parent
f7290cdcfc
commit
af813bf51f
18
src/rcore.rs
18
src/rcore.rs
@ -87,7 +87,7 @@ where
|
|||||||
/// [`Factory`] associated with the [`Mentionable`]. Mostly useful for `type` definitions.
|
/// [`Factory`] associated with the [`Mentionable`]. Mostly useful for `type` definitions.
|
||||||
pub type Fctr<'a, Ctx, A> = <A as MentionableBase<'a, Ctx>>::Fctr;
|
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>>;
|
pub type ParseResult<'a, Ctx, F> = Result<Mtbl<'a, Ctx, F>, ParseError<'a, Ctx, F>>;
|
||||||
|
|
||||||
/// [`ParseResult`] associated with a [`Mentionable`] (instead of a [`Factory`]).
|
/// [`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 {
|
pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
|
||||||
/// Type of the associated objects.
|
/// Type of the associated objects.
|
||||||
type Mtbl: MentionableBase<'a, Ctx, Fctr = Self>;
|
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;
|
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.
|
/// Trait representing deserialisation rules for [Mentionable]s.
|
||||||
/// Crucial for [`crate::rstd::typeless`].
|
/// Crucial for [`crate::rstd::typeless`].
|
||||||
pub trait Factory<'a, Ctx: Context<'a>>:
|
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>;
|
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.
|
/// [`Mentionable`] associated with the [`Factory`]. Mostly useful for `type` definitions.
|
||||||
|
@ -27,13 +27,11 @@ pub trait FactoryProxy<'a, Ctx: Context<'a>> {
|
|||||||
) -> ParseResult<'a, Ctx, Self::F>;
|
) -> 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
|
where
|
||||||
F::WithMode: FactoryProxy<'a, Ctx, F = Self>,
|
F::WithMode: FactoryProxy<'a, Ctx, F = Self>,
|
||||||
F::Mtbl: MentionableTop<'a, Ctx>,
|
F::Mtbl: MentionableTop<'a, Ctx>,
|
||||||
{
|
{
|
||||||
type _Mtbl = Self::Mtbl;
|
|
||||||
|
|
||||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
|
||||||
<F::WithMode as FactoryProxy<'a, Ctx>>::pdeserialize(self, inctx)
|
<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 struct RegularMode;
|
||||||
|
|
||||||
pub trait RegularFactory<'a, Ctx: Context<'a>>:
|
pub trait RegularFactory<'a, Ctx: Context<'a>>:
|
||||||
|
@ -5,7 +5,7 @@ use super::*;
|
|||||||
pub enum ResolutionError<L, P> {
|
pub enum ResolutionError<L, P> {
|
||||||
/// Usually comes from [`Resolver::resolve`].
|
/// Usually comes from [`Resolver::resolve`].
|
||||||
Lookup(L),
|
Lookup(L),
|
||||||
/// Usually comes from [`Factory::deserialize`].
|
/// Usually comes from [`ParseFactory::deserialize`].
|
||||||
Parse(P),
|
Parse(P),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ impl Serializer for Vec<u8> {
|
|||||||
|
|
||||||
/// Trait representing a readable stream used for parsing.
|
/// Trait representing a readable stream used for parsing.
|
||||||
///
|
///
|
||||||
/// See [`Serializer`], [`Factory::deserialize`].
|
/// See [`Serializer`], [`ParseFactory::deserialize`].
|
||||||
pub trait Deserializer {
|
pub trait Deserializer {
|
||||||
/// Read at most `n` bytes.
|
/// Read at most `n` bytes.
|
||||||
fn read_n(&mut self, n: usize) -> &[u8];
|
fn read_n(&mut self, n: usize) -> &[u8];
|
||||||
|
@ -29,9 +29,9 @@ pub trait AtomicBase: 'static + Send + Sync + Send + Clone + Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Atomic: AtomicBase + ParseMode {
|
pub trait Atomic: AtomicBase + ParseMode {
|
||||||
/// Static equivalent of [`Factory::deserialize`].
|
/// Static equivalent of [`ParseFactory::deserialize`].
|
||||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
|
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>;
|
fn a_extend(self, tail: &[u8]) -> AParseResult<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user