FactoryBase
This commit is contained in:
parent
c01963809e
commit
f1b4e7b88e
19
src/rcore.rs
19
src/rcore.rs
@ -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;
|
||||
|
||||
/// 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`]).
|
||||
pub type ParseResultA<'a, Ctx, A> = Result<A, ParseErrorA<'a, Ctx, A>>;
|
||||
|
||||
/// Trait representing deserialisation rules for [Mentionable]s.
|
||||
/// Crucial for [`crate::rstd::typeless`].
|
||||
pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
|
||||
/// [Factory] base.
|
||||
pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
|
||||
/// Type of the associated objects.
|
||||
type Mtbl: Mentionable<'a, Ctx, Fctr = Self>;
|
||||
/// Type of an error that [`Factory::deserialize`] can fail with.
|
||||
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`].
|
||||
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
|
||||
/// 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.
|
||||
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.
|
||||
pub type ParseError<'a, Ctx, F> = <F as Factory<'a, Ctx>>::ParseError;
|
||||
/// [`FactoryBase::ParseError`]. Mostly useful for `type` definitions.
|
||||
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.
|
||||
pub type ParseErrorA<'a, Ctx, A> = ParseError<'a, Ctx, Fctr<'a, Ctx, A>>;
|
||||
|
||||
|
@ -19,7 +19,7 @@ pub type AParseResult<A> = Result<A, <A as Atomic>::AParseError>;
|
||||
/// while limiting [`Mentionable::points_typed`] (and corresponding [`Mentionable::topology`])
|
||||
/// to an empty sequence.
|
||||
pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable {
|
||||
/// Equivalent of [`Factory::ParseError`].
|
||||
/// Equivalent of [`FactoryBase::ParseError`].
|
||||
type AParseError: Error;
|
||||
/// Static equivalent of [`Factory::deserialize`].
|
||||
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self>;
|
||||
|
@ -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 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> {
|
||||
Ok(A::a_deserialize(inctx)?.into())
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ struct CastResolver<'a, Ctx: Context<'a>> {
|
||||
}
|
||||
|
||||
/// 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`]
|
||||
/// variants generally shouldn't happen.
|
||||
#[derive(Debug)]
|
||||
|
@ -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 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> {
|
||||
let (rest, inctx) = self.parse_point(inctx)?;
|
||||
let element = self
|
||||
|
@ -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 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> {
|
||||
let tree_factory = TreeFactory(NullableFactory::new(self.clone()));
|
||||
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 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> {
|
||||
self.ideserialize(inctx).seal()
|
||||
}
|
||||
|
@ -10,8 +10,7 @@ use super::{
|
||||
*,
|
||||
};
|
||||
|
||||
pub type IParseResult<'a, Ctx, F, I> =
|
||||
Result<(Mtbl<'a, Ctx, F>, I), <F as Factory<'a, Ctx>>::ParseError>;
|
||||
pub type IParseResult<'a, Ctx, F, I> = Result<(Mtbl<'a, Ctx, F>, I), ParseError<'a, Ctx, F>>;
|
||||
|
||||
/// This factory should return an error on EOF.
|
||||
pub trait InlineableFactory<'a, Ctx: Context<'a>>: Factory<'a, Ctx> {
|
||||
@ -126,7 +125,7 @@ impl Display for SizeError {
|
||||
|
||||
impl Error for SizeError {}
|
||||
|
||||
/// Wrapper for [`SizeError`]/[`Factory::ParseError`].
|
||||
/// Wrapper for [`SizeError`]/[`FactoryBase::ParseError`].
|
||||
#[derive(Debug)]
|
||||
pub enum CheckedParseError<P: Error> {
|
||||
Parse(P),
|
||||
|
@ -41,7 +41,7 @@ pub trait StaticPair<'a, Ctx: Context<'a>>:
|
||||
type FA: Factory<'a, Ctx, Mtbl = Self::A> + InlineableFactory<'a, Ctx>;
|
||||
/// Second element's factory.
|
||||
type FB: Factory<'a, Ctx, Mtbl = Self::B>;
|
||||
/// See [Factory::ParseError].
|
||||
/// See [`FactoryBase::ParseError`].
|
||||
type ParseError: 'a + Error;
|
||||
|
||||
/// 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>
|
||||
{
|
||||
type Mtbl = StaticPairObject<SP>;
|
||||
|
||||
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> {
|
||||
let (fa, fb) = SP::factories(&self.factory_data);
|
||||
let (a, inctx) = fa
|
||||
|
@ -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 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> {
|
||||
self.ideserialize(inctx).seal()
|
||||
}
|
||||
|
@ -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 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> {
|
||||
let (point, _) = inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))?;
|
||||
Ok(point)
|
||||
|
@ -87,11 +87,13 @@ impl<'a> Display 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 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> {
|
||||
self.t_deserialize.de(inctx.demote()).map_err(TypelessError)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user