add mentionable to unexpected_tail

This commit is contained in:
AF 2023-06-16 06:48:44 +00:00
parent 5f0927c94a
commit 8ecc4678f7
14 changed files with 68 additions and 33 deletions

View File

@ -76,7 +76,7 @@ pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone {
addresses: &mut Addresses,
) -> ParseResult<'a, Ctx, Self>;
/// Called by finite stream parsers if there's any data left.
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError;
fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError;
}
pub type Mtbl<'a, Ctx, F> = <F as Factory<'a, Ctx>>::Mtbl;

View File

@ -51,7 +51,7 @@ fn _parse_slice<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>(
if tail.is_empty() {
Ok(mentionable)
} else {
Err(factory.unexpected_tail(tail))
Err(factory.unexpected_tail(mentionable, tail))
}
}

View File

@ -21,17 +21,17 @@ pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable {
/// Static equivalent of [`Factory::deserialize`].
fn a_deserialize(deserializer: &mut dyn Deserializer) -> Result<Self, Self::AParseError>;
/// Static equivalent of [`Factory::unexpected_tail`].
fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError;
fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError;
}
fn _parse_slice<A: Atomic>(slice: &[u8]) -> Result<A, A::AParseError> {
let mut deserializer = SliceDeserializer::from(slice);
let mentionable = A::a_deserialize(&mut deserializer)?;
let atomic = A::a_deserialize(&mut deserializer)?;
let tail = deserializer.read_all();
if tail.is_empty() {
Ok(mentionable)
Ok(atomic)
} else {
Err(A::a_unexpected_tail(tail))
Err(A::a_unexpected_tail(atomic, tail))
}
}

View File

@ -81,8 +81,8 @@ impl<'a, Ctx: Context<'a>, A: Atomic> Factory<'a, Ctx> for AtomicFactory<A> {
Ok(A::a_deserialize(deserializer)?.into())
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
A::a_unexpected_tail(tail)
fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
A::a_unexpected_tail(mentionable.atomic, tail)
}
}

View File

@ -40,7 +40,7 @@ impl Atomic for u64 {
Ok(u64::from_le_bytes(deserializer.read_n_const::<8>()?))
}
fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError {
fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError {
IntParseError::ExtraData(tail.len())
}
}

View File

@ -52,7 +52,7 @@ impl Atomic for bool {
}
}
fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError {
fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError {
BooleanParseError::ExtraData(tail.len())
}
}

View File

@ -35,7 +35,7 @@ impl Atomic for Plain {
Ok(Plain::from_slice(deserializer.read_all()))
}
fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError {
fn a_unexpected_tail(self, tail: &[u8]) -> Self::AParseError {
panic!(
"Plain must use read_all, therefore there must not be any extra tail (received {} bytes).",
tail.len()

View File

@ -149,8 +149,8 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for AvlNodeFact
Ok(AvlNode { l, r, key })
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
AvlError::Key(self.0.unexpected_tail(tail))
fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
AvlError::Key(self.0.unexpected_tail(mentionable.key, tail))
}
}
@ -177,7 +177,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for AvlTreeFact
Ok(AvlTree { node, height })
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
u64::a_unexpected_tail(tail).into()
fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
u64::a_unexpected_tail(mentionable.height, tail).into()
}
}

View File

@ -22,6 +22,10 @@ impl<A: Serializable, B: Serializable> StaticPairSerializable for Pair<A, B> {
fn elements(&self) -> (&Self::SA, &Self::SB) {
(&self.a, &self.b)
}
fn into_elements(self) -> (Self::SA, Self::SB) {
(self.a, self.b)
}
}
#[derive(Debug)]

View File

@ -102,8 +102,11 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa
Ok(StackNode { rest, element })
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
StackParseError::Element(self.element_factory.unexpected_tail(tail))
fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
StackParseError::Element(
self.element_factory
.unexpected_tail(mentionable.element, tail),
)
}
}

View File

@ -18,6 +18,7 @@ pub trait StaticPairSerializable {
/// Borrow both elements.
fn elements(&self) -> (&Self::SA, &Self::SB);
fn into_elements(self) -> (Self::SA, Self::SB);
}
/// Trait to be implemented on object data.
@ -150,9 +151,10 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx>
})
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
let (_, fb) = SP::factories(&self.factory_data);
SP::from_error_b(&self.factory_data, fb.unexpected_tail(tail))
let (_, b) = mentionable.pair.into_elements();
SP::from_error_b(&self.factory_data, fb.unexpected_tail(b, tail))
}
}

View File

@ -70,7 +70,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for NullableFac
})
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
fn unexpected_tail(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len())
}
}

View File

@ -77,7 +77,7 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for PointFactor
Ok(addresses.next_point(deserializer, resolver, self.factory.clone())?)
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
fn unexpected_tail(&self, _mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
PointParseError::WrongLength(HASH_SIZE + tail.len())
}
}

View File

@ -1,6 +1,6 @@
//! Previously part of [`crate::rcore`].
use super::{wrapped_origin::*, *};
use super::{cast::CastError, wrapped_origin::*, *};
type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer);
@ -30,7 +30,7 @@ type TdeBox<'a, Ctx> = Box<dyn Tde<'a, Ctx>>;
trait Tut<'a, Ctx: Context<'a>>: 'a + Send + Sync {
fn clone_box(&self) -> TutBox<'a, Ctx>;
fn ut(&self, tail: &[u8]) -> TypelessError<'a>;
fn ut(&self, mentionable: TypelessMentionable<'a, Ctx>, tail: &[u8]) -> TypelessError<'a>;
}
type TutBox<'a, Ctx> = Box<dyn Tut<'a, Ctx>>;
@ -103,12 +103,15 @@ impl<'a, Ctx: Context<'a>> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> {
}
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
self.t_unexpected_tail.ut(tail)
fn unexpected_tail(&self, mentionable: Self::Mtbl, tail: &[u8]) -> Self::ParseError {
self.t_unexpected_tail.ut(mentionable, tail)
}
}
impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> {
impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx>
where
Ctx::LookupError: From<CastError<'a>>,
{
pub fn from_typed<A: Mentionable<'a, Ctx>>(mentionable: Rc<A>) -> Self {
let factory = TypelessFactory::from_typed(mentionable.factory());
let topology = mentionable.topology();
@ -122,7 +125,10 @@ impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> {
}
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F
where
Ctx::LookupError: From<CastError<'a>>,
{
fn clone_box(&self) -> TdeBox<'a, Ctx> {
Box::new(self.clone())
}
@ -143,17 +149,29 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F {
}
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F {
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F
where
Ctx::LookupError: From<CastError<'a>>,
{
fn clone_box(&self) -> TutBox<'a, Ctx> {
Box::new(self.clone())
}
fn ut(&self, tail: &[u8]) -> TypelessError<'a> {
TypelessError::from_typed(self.unexpected_tail(tail))
fn ut(&self, mentionable: TypelessMentionable<'a, Ctx>, tail: &[u8]) -> TypelessError<'a> {
TypelessError::from_typed(self.unexpected_tail(
match mentionable.cast(self.clone()) {
Ok(m) => m,
Err(e) => return TypelessError::from_typed(e),
},
tail,
))
}
}
impl<'a, Ctx: Context<'a>> TypelessFactory<'a, Ctx> {
impl<'a, Ctx: Context<'a>> TypelessFactory<'a, Ctx>
where
Ctx::LookupError: From<CastError<'a>>,
{
pub fn from_typed<F: Factory<'a, Ctx>>(factory: F) -> Self {
TypelessFactory {
t_deserialize: Box::new(factory.clone()),
@ -168,7 +186,10 @@ impl<'a> TypelessError<'a> {
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A>
where
Ctx::LookupError: From<CastError<'a>>,
{
/// Typeless version of the point.
pub fn typeless(&self) -> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> {
Point {
@ -189,7 +210,10 @@ pub trait MentionableExt<'a, Ctx: Context<'a>>: Mentionable<'a, Ctx> {
fn points_vec(&self) -> Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>;
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A {
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A
where
Ctx::LookupError: From<CastError<'a>>,
{
fn points_typeless(&self, points: &mut Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) {
self.points_typed(points)
}
@ -203,6 +227,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for
impl<'a, Ctx: Context<'a>> TakesPoints<'a, Ctx>
for Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>
where
Ctx::LookupError: From<CastError<'a>>,
{
fn take<A: Mentionable<'a, Ctx>>(&mut self, point: &Point<'a, Ctx, A>) {
self.push(point.typeless());