Factory: ModeFactory

This commit is contained in:
AF 2023-07-28 21:06:59 +00:00
parent 5a7ef36d89
commit f3eaccfa77
9 changed files with 74 additions and 43 deletions

View File

@ -86,7 +86,7 @@ pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
/// 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>>: FactoryBase<'a, Ctx> { pub trait Factory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> + ModeFactory {
/// 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.

View File

@ -73,12 +73,16 @@ impl<'a, Ctx: Context<'a>, A: Atomic> FactoryBase<'a, Ctx> for AtomicFactory<A>
type ParseError = A::AParseError; type ParseError = A::AParseError;
} }
impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory<A> { impl<A: Atomic> ModeFactory for AtomicFactory<A> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { type Mode = RegularFactoryMode;
}
impl<'a, Ctx: Context<'a>, A: Atomic> RegularFactory<'a, Ctx> for AtomicFactory<A> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
Ok(A::a_deserialize(inctx)?.into()) Ok(A::a_deserialize(inctx)?.into())
} }
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
Ok(A::a_extend(mentionable.atomic, tail)?.into()) Ok(A::a_extend(mentionable.atomic, tail)?.into())
} }
} }

View File

@ -93,8 +93,12 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Sta
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> { impl<F> ModeFactory for StackNodeFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { type Mode = RegularFactoryMode;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> RegularFactory<'a, Ctx> for StackNodeFactory<F> {
fn rdeserialize(&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
.element_factory .element_factory
@ -103,7 +107,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa
Ok(StackNode { rest, element }) Ok(StackNode { rest, element })
} }
fn extend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
mentionable.element = self mentionable.element = self
.element_factory .element_factory
.extend(mentionable.element, tail) .extend(mentionable.element, tail)

View File

@ -137,8 +137,12 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Nod
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> { impl<F> ModeFactory for NodeFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { type Mode = RegularFactoryMode;
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> RegularFactory<'a, Ctx> for NodeFactory<F> {
fn rdeserialize(&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)?;
let (r, inctx) = tree_factory.ideserialize(inctx)?; let (r, inctx) = tree_factory.ideserialize(inctx)?;
@ -146,7 +150,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NodeFactory
Ok(Node { l, r, key }) Ok(Node { l, r, key })
} }
fn extend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
mentionable.key = self mentionable.key = self
.0 .0
.extend(mentionable.key, tail) .extend(mentionable.key, tail)
@ -161,12 +165,16 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Tre
type ParseError = TreeParseError<F::ParseError>; type ParseError = TreeParseError<F::ParseError>;
} }
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> Factory<'a, Ctx> for TreeFactory<F> { impl<F> ModeFactory for TreeFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { type Mode = RegularFactoryMode;
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for TreeFactory<F> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal() self.ideserialize(inctx).seal()
} }
fn extend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
mentionable.height = u64::a_extend(mentionable.height, tail)?; mentionable.height = u64::a_extend(mentionable.height, tail)?;
mentionable.validate_height()?; mentionable.validate_height()?;
Ok(mentionable) Ok(mentionable)

View File

@ -6,7 +6,6 @@ use crate::rcore::*;
use super::{ use super::{
atomic::{atomic_object::*, *}, atomic::{atomic_object::*, *},
point::*,
*, *,
}; };
@ -232,17 +231,3 @@ impl<'a, Ctx: Context<'a>, F: AlwaysConstSize + InlineableFactory<'a, Ctx>>
{ {
const SIZE: usize = Self::_SIZE; const SIZE: usize = Self::_SIZE;
} }
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> InlineableFactory<'a, Ctx> for PointFactory<F> {
fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len())
}
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> {
inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))
}
}
impl<F> AlwaysConstSize for PointFactory<F> {
const _SIZE: usize = HASH_SIZE;
}

View File

@ -132,10 +132,14 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FactoryBase<'a, Ctx>
type ParseError = SP::ParseError; type ParseError = SP::ParseError;
} }
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx> impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> ModeFactory for StaticPairFactory<'a, Ctx, SP> {
type Mode = RegularFactoryMode;
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> RegularFactory<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP> for StaticPairFactory<'a, Ctx, SP>
{ {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn rdeserialize(&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
.ideserialize(inctx) .ideserialize(inctx)
@ -148,7 +152,7 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx>
}) })
} }
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
let (_, fb) = SP::factories(&self.factory_data); let (_, fb) = SP::factories(&self.factory_data);
let (a, b) = mentionable.pair.into_elements(); let (a, b) = mentionable.pair.into_elements();
match fb.extend(b, tail) { match fb.extend(b, tail) {

View File

@ -56,12 +56,16 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Nul
type ParseError = PointParseError; type ParseError = PointParseError;
} }
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> Factory<'a, Ctx> for NullableFactory<F> { impl<F> ModeFactory for NullableFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { type Mode = RegularFactoryMode;
}
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for NullableFactory<F> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal() self.ideserialize(inctx).seal()
} }
fn extend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
Err(self.extension_error(tail)) Err(self.extension_error(tail))
} }
} }

View File

@ -3,6 +3,7 @@
use std::{error::Error, fmt::Display}; use std::{error::Error, fmt::Display};
use crate::rcore::*; use crate::rcore::*;
use crate::rstd::inlining::*;
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for Point<'a, Ctx, A> { impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Serializable for Point<'a, Ctx, A> {
fn serialize(&self, serializer: &mut dyn Serializer) { fn serialize(&self, serializer: &mut dyn Serializer) {
@ -74,13 +75,30 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for Poi
type ParseError = PointParseError; type ParseError = PointParseError;
} }
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> Factory<'a, Ctx> for PointFactory<F> { impl<F> ModeFactory for PointFactory<F> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { type Mode = RegularFactoryMode;
let (point, _) = inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))?; }
Ok(point)
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> RegularFactory<'a, Ctx> for PointFactory<F> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.ideserialize(inctx).seal()
} }
fn extend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
Err(PointParseError::WrongLength(HASH_SIZE + tail.len())) Err(self.extension_error(tail))
} }
} }
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> InlineableFactory<'a, Ctx> for PointFactory<F> {
fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len())
}
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> {
inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice))
}
}
impl<F> AlwaysConstSize for PointFactory<F> {
const _SIZE: usize = HASH_SIZE;
}

View File

@ -93,12 +93,16 @@ impl<'a, Ctx: Context<'a>> FactoryBase<'a, Ctx> for TypelessFactory<'a, Ctx> {
type ParseError = TypelessError<'a>; type ParseError = TypelessError<'a>;
} }
impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> { impl<'a, Ctx: Context<'a>> ModeFactory for TypelessFactory<'a, Ctx> {
fn deserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { type Mode = RegularFactoryMode;
}
impl<'a, Ctx: Context<'a>> RegularFactory<'a, Ctx> for TypelessFactory<'a, Ctx> {
fn rdeserialize(&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)
} }
fn extend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
self.t_extend.xt(mentionable, tail) self.t_extend.xt(mentionable, tail)
} }
} }