InlineableFactory::ideserialize

This commit is contained in:
AF 2023-06-30 19:01:25 +00:00
parent ba33f82ecb
commit 19a55bf626
4 changed files with 62 additions and 11 deletions

View File

@ -167,6 +167,17 @@ impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>> InlineableFactory<'a,
fn extension_error(&self, tail: &[u8]) -> Self::ParseError { fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
StackParseError::Element(self.element_factory.extension_error(tail)) StackParseError::Element(self.element_factory.extension_error(tail))
} }
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> {
let (rest, inctx) = NullableFactory::new(self.clone())
.ideserialize(inctx)
.map_err(StackParseError::Point)?;
let (element, inctx) = self
.element_factory
.ideserialize(inctx)
.map_err(StackParseError::Element)?;
Ok((StackNode { rest, element }, inctx))
}
} }
impl<'a, Ctx: Context<'a>, F: FixedSizeFactory<'a, Ctx>> FixedSizeFactory<'a, Ctx> impl<'a, Ctx: Context<'a>, F: FixedSizeFactory<'a, Ctx>> FixedSizeFactory<'a, Ctx>

View File

@ -41,7 +41,15 @@ pub trait InCtx<'a, Ctx: Context<'a>>: Inlining {
self, self,
factory: A::Fctr, factory: A::Fctr,
err: impl FnOnce(&[u8]) -> E, err: impl FnOnce(&[u8]) -> E,
) -> Result<(Point<'a, Ctx, A>, Self), E>; ) -> Result<(Point<'a, Ctx, A>, Self), E> {
let (point, inctx) = self.icnext_address(err)?;
Ok((
Point::from_address(point, factory, inctx.iresolver()),
inctx,
))
}
fn iresolver(&self) -> Rc<dyn Resolver<'a, Ctx>>;
} }
struct InCtxT<'a: 'c, 'c, Ctx: Context<'a>> { struct InCtxT<'a: 'c, 'c, Ctx: Context<'a>> {
@ -79,16 +87,8 @@ impl<'a: 'c, 'c, Ctx: Context<'a>> InCtx<'a, Ctx> for InCtxT<'a, 'c, Ctx> {
} }
} }
fn icnext_point<'b, A: Mentionable<'a, Ctx>, E>( fn iresolver(&self) -> Rc<dyn Resolver<'a, Ctx>> {
self, self.dectx.resolver()
factory: A::Fctr,
err: impl FnOnce(&[u8]) -> E,
) -> Result<(Point<'a, Ctx, A>, Self), E> {
let (point, inctx) = self.icnext_address(err)?;
Ok((
Point::from_address(point, factory, inctx.dectx.resolver()),
inctx,
))
} }
} }
@ -98,6 +98,8 @@ pub type IParseResult<'a, Ctx, F, I> =
/// 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> {
fn extension_error(&self, tail: &[u8]) -> Self::ParseError; fn extension_error(&self, tail: &[u8]) -> Self::ParseError;
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I>;
} }
/// This factory always reads the same amount of bytes or returns error. /// This factory always reads the same amount of bytes or returns error.
@ -149,6 +151,11 @@ impl<'a, Ctx: Context<'a>, A: InlineableAtomic> InlineableFactory<'a, Ctx> for A
fn extension_error(&self, tail: &[u8]) -> Self::ParseError { fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
A::a_extension_error(tail) A::a_extension_error(tail)
} }
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> {
let (a, inctx) = A::a_ideserialize(inctx)?;
Ok((a.into(), inctx))
}
} }
impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic> FixedSizeFactory<'a, Ctx> for AtomicFactory<A> { impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic> FixedSizeFactory<'a, Ctx> for AtomicFactory<A> {
@ -315,6 +322,10 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> InlineableFactory<'a, Ctx> for P
fn extension_error(&self, tail: &[u8]) -> Self::ParseError { fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len()) 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> { impl<F> AlwaysConstSize for PointFactory<F> {

View File

@ -163,6 +163,22 @@ where
let (_, fb) = SP::factories(&self.factory_data); let (_, fb) = SP::factories(&self.factory_data);
SP::from_error_b(&self.factory_data, fb.extension_error(tail)) SP::from_error_b(&self.factory_data, fb.extension_error(tail))
} }
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> {
let (fa, fb) = SP::factories(&self.factory_data);
let (a, inctx) = fa
.ideserialize(inctx)
.map_err(|e| SP::from_error_a(&self.factory_data, e))?;
let (b, inctx) = fb
.ideserialize(inctx)
.map_err(|e| SP::from_error_b(&self.factory_data, e))?;
Ok((
StaticPairObject {
pair: SP::from_parsed(&self.factory_data, a, b),
},
inctx,
))
}
} }
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FixedSizeFactory<'a, Ctx> impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FixedSizeFactory<'a, Ctx>

View File

@ -115,6 +115,19 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> InlineableFactory<'a, Ctx> for N
fn extension_error(&self, tail: &[u8]) -> Self::ParseError { fn extension_error(&self, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len()) PointParseError::WrongLength(HASH_SIZE + tail.len())
} }
fn ideserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> {
let factory = self.factory.clone();
let (address, inctx) = inctx.icnext_address(|slice| PointParseError::from(slice))?;
Ok((
if address.point == HASH_ZEROS {
Nullable::Null(factory)
} else {
Nullable::NotNull(Point::from_address(address, factory, inctx.iresolver()))
},
inctx,
))
}
} }
impl<F> AlwaysConstSize for NullableFactory<F> { impl<F> AlwaysConstSize for NullableFactory<F> {