FactoryBase

This commit is contained in:
AF 2023-07-28 19:40:30 +00:00
parent c01963809e
commit f1b4e7b88e
11 changed files with 42 additions and 22 deletions

View File

@ -67,19 +67,22 @@ pub trait Mentionable<'a, Ctx: Context<'a>>: 'a + Serializable {
pub type Fctr<'a, Ctx, A> = <A as Mentionable<'a, Ctx>>::Fctr; pub type Fctr<'a, Ctx, A> = <A as Mentionable<'a, Ctx>>::Fctr;
/// Shorthand for the type of vaalues returned by [`Factory::deserialize`]. /// Shorthand for the type of vaalues returned by [`Factory::deserialize`].
pub type ParseResult<'a, Ctx, F> = Result<Mtbl<'a, Ctx, F>, <F as Factory<'a, Ctx>>::ParseError>; 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`]).
pub type ParseResultA<'a, Ctx, A> = Result<A, ParseErrorA<'a, Ctx, A>>; pub type ParseResultA<'a, Ctx, A> = Result<A, ParseErrorA<'a, Ctx, A>>;
/// Trait representing deserialisation rules for [Mentionable]s. /// [Factory] base.
/// Crucial for [`crate::rstd::typeless`]. pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
/// Type of the associated objects. /// Type of the associated objects.
type Mtbl: Mentionable<'a, Ctx, Fctr = Self>; type Mtbl: Mentionable<'a, Ctx, Fctr = Self>;
/// Type of an error that [`Factory::deserialize`] can fail with. /// Type of an error that [`Factory::deserialize`] can fail with.
type ParseError: 'a + Error; type ParseError: 'a + Error;
}
/// Trait representing deserialisation rules for [Mentionable]s.
/// Crucial for [`crate::rstd::typeless`].
pub trait Factory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> {
/// See [`Deserializer`], [`Resolver`]. /// See [`Deserializer`], [`Resolver`].
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>; fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
/// Called by finite stream parsers if there's any data left. /// Called by finite stream parsers if there's any data left.
@ -87,12 +90,12 @@ pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
} }
/// [`Mentionable`] associated with the [`Factory`]. Mostly useful for `type` definitions. /// [`Mentionable`] associated with the [`Factory`]. Mostly useful for `type` definitions.
pub type Mtbl<'a, Ctx, F> = <F as Factory<'a, Ctx>>::Mtbl; pub type Mtbl<'a, Ctx, F> = <F as FactoryBase<'a, Ctx>>::Mtbl;
/// [`Factory::ParseError`]. Mostly useful for `type` definitions. /// [`FactoryBase::ParseError`]. Mostly useful for `type` definitions.
pub type ParseError<'a, Ctx, F> = <F as Factory<'a, Ctx>>::ParseError; pub type ParseError<'a, Ctx, F> = <F as FactoryBase<'a, Ctx>>::ParseError;
/// [`Factory::ParseError`] associated with the [`Mentionable`]. /// [`FactoryBase::ParseError`] associated with the [`Mentionable`].
/// Mostly useful for `type` definitions. /// Mostly useful for `type` definitions.
pub type ParseErrorA<'a, Ctx, A> = ParseError<'a, Ctx, Fctr<'a, Ctx, A>>; pub type ParseErrorA<'a, Ctx, A> = ParseError<'a, Ctx, Fctr<'a, Ctx, A>>;

View File

@ -19,7 +19,7 @@ pub type AParseResult<A> = Result<A, <A as Atomic>::AParseError>;
/// while limiting [`Mentionable::points_typed`] (and corresponding [`Mentionable::topology`]) /// while limiting [`Mentionable::points_typed`] (and corresponding [`Mentionable::topology`])
/// to an empty sequence. /// to an empty sequence.
pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable { pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable {
/// Equivalent of [`Factory::ParseError`]. /// Equivalent of [`FactoryBase::ParseError`].
type AParseError: Error; type AParseError: Error;
/// Static equivalent of [`Factory::deserialize`]. /// Static equivalent of [`Factory::deserialize`].
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self>; fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self>;

View File

@ -67,11 +67,13 @@ impl<A: Atomic> Clone for AtomicFactory<A> {
} }
} }
impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory<A> { impl<'a, Ctx: Context<'a>, A: Atomic> FactoryBase<'a, Ctx> for AtomicFactory<A> {
type Mtbl = AtomicObject<A>; type Mtbl = AtomicObject<A>;
type ParseError = A::AParseError; type ParseError = A::AParseError;
}
impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory<A> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
Ok(A::a_deserialize(inctx)?.into()) Ok(A::a_deserialize(inctx)?.into())
} }

View File

@ -15,7 +15,7 @@ struct CastResolver<'a, Ctx: Context<'a>> {
} }
/// Used to represent errors that might arise during resolution/parsion. /// Used to represent errors that might arise during resolution/parsion.
/// Is expected to be classified as [`Context::LookupError`] rather than [`Factory::ParseError`]. /// Is expected to be classified as [`Context::LookupError`] rather than [`FactoryBase::ParseError`].
/// [`CastError::AddressIndexOutOfBounds`] and [`CastError::AddressPointMismatch`] /// [`CastError::AddressIndexOutOfBounds`] and [`CastError::AddressPointMismatch`]
/// variants generally shouldn't happen. /// variants generally shouldn't happen.
#[derive(Debug)] #[derive(Debug)]

View File

@ -87,11 +87,13 @@ impl<F> StackNodeFactory<F> {
} }
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFactory<F> { impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for StackNodeFactory<F> {
type Mtbl = StackNode<'a, Ctx, F::Mtbl>; type Mtbl = StackNode<'a, Ctx, F::Mtbl>;
type ParseError = StackParseError<ParseError<'a, Ctx, F>>; type ParseError = StackParseError<ParseError<'a, Ctx, F>>;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (rest, inctx) = self.parse_point(inctx)?; let (rest, inctx) = self.parse_point(inctx)?;
let element = self let element = self

View File

@ -131,11 +131,13 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Tre
} }
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory<F> { impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for NodeFactory<F> {
type Mtbl = Node<'a, Ctx, F::Mtbl>; type Mtbl = Node<'a, Ctx, F::Mtbl>;
type ParseError = TreeParseError<F::ParseError>; type ParseError = TreeParseError<F::ParseError>;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let tree_factory = TreeFactory(NullableFactory::new(self.clone())); let tree_factory = TreeFactory(NullableFactory::new(self.clone()));
let (l, inctx) = tree_factory.ideserialize(inctx)?; let (l, inctx) = tree_factory.ideserialize(inctx)?;
@ -153,11 +155,13 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory
} }
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for TreeFactory<F> { impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for TreeFactory<F> {
type Mtbl = Tree<'a, Ctx, F::Mtbl>; type Mtbl = Tree<'a, Ctx, F::Mtbl>;
type ParseError = TreeParseError<F::ParseError>; type ParseError = TreeParseError<F::ParseError>;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for TreeFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal() self.ideserialize(inctx).seal()
} }

View File

@ -10,8 +10,7 @@ use super::{
*, *,
}; };
pub type IParseResult<'a, Ctx, F, I> = pub type IParseResult<'a, Ctx, F, I> = Result<(Mtbl<'a, Ctx, F>, I), ParseError<'a, Ctx, F>>;
Result<(Mtbl<'a, Ctx, F>, I), <F as Factory<'a, Ctx>>::ParseError>;
/// This factory should return an error on EOF. /// This factory should return an error on EOF.
pub trait InlineableFactory<'a, Ctx: Context<'a>>: Factory<'a, Ctx> { pub trait InlineableFactory<'a, Ctx: Context<'a>>: Factory<'a, Ctx> {
@ -126,7 +125,7 @@ impl Display for SizeError {
impl Error for SizeError {} impl Error for SizeError {}
/// Wrapper for [`SizeError`]/[`Factory::ParseError`]. /// Wrapper for [`SizeError`]/[`FactoryBase::ParseError`].
#[derive(Debug)] #[derive(Debug)]
pub enum CheckedParseError<P: Error> { pub enum CheckedParseError<P: Error> {
Parse(P), Parse(P),

View File

@ -41,7 +41,7 @@ pub trait StaticPair<'a, Ctx: Context<'a>>:
type FA: Factory<'a, Ctx, Mtbl = Self::A> + InlineableFactory<'a, Ctx>; type FA: Factory<'a, Ctx, Mtbl = Self::A> + InlineableFactory<'a, Ctx>;
/// Second element's factory. /// Second element's factory.
type FB: Factory<'a, Ctx, Mtbl = Self::B>; type FB: Factory<'a, Ctx, Mtbl = Self::B>;
/// See [Factory::ParseError]. /// See [`FactoryBase::ParseError`].
type ParseError: 'a + Error; type ParseError: 'a + Error;
/// Borrow both elements' factories. /// Borrow both elements' factories.
@ -124,13 +124,17 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Clone for StaticPairFactory<
} }
} }
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx> impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FactoryBase<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP> for StaticPairFactory<'a, Ctx, SP>
{ {
type Mtbl = StaticPairObject<SP>; type Mtbl = StaticPairObject<SP>;
type ParseError = SP::ParseError; type ParseError = SP::ParseError;
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP>
{
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (fa, fb) = SP::factories(&self.factory_data); let (fa, fb) = SP::factories(&self.factory_data);
let (a, inctx) = fa let (a, inctx) = fa

View File

@ -50,11 +50,13 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Nul
} }
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NullableFactory<F> { impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for NullableFactory<F> {
type Mtbl = Nullable<'a, Ctx, F::Mtbl>; type Mtbl = Nullable<'a, Ctx, F::Mtbl>;
type ParseError = PointParseError; type ParseError = PointParseError;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NullableFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal() self.ideserialize(inctx).seal()
} }

View File

@ -68,11 +68,13 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AsRef<[u8]> for Point<'a, Ct
} }
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for PointFactory<F> { impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> FactoryBase<'a, Ctx> for PointFactory<F> {
type Mtbl = Point<'a, Ctx, F::Mtbl>; type Mtbl = Point<'a, Ctx, F::Mtbl>;
type ParseError = PointParseError; type ParseError = PointParseError;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for PointFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (point, _) = inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))?; let (point, _) = inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))?;
Ok(point) Ok(point)

View File

@ -87,11 +87,13 @@ impl<'a> Display for TypelessError<'a> {
impl<'a> Error for TypelessError<'a> {} impl<'a> Error for TypelessError<'a> {}
impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> { impl<'a, Ctx: Context<'a>> FactoryBase<'a, Ctx> for TypelessFactory<'a, Ctx> {
type Mtbl = TypelessMentionable<'a, Ctx>; type Mtbl = TypelessMentionable<'a, Ctx>;
type ParseError = TypelessError<'a>; type ParseError = TypelessError<'a>;
}
impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.t_deserialize.de(inctx.demote()).map_err(TypelessError) self.t_deserialize.de(inctx.demote()).map_err(TypelessError)
} }