99 lines
2.4 KiB
Rust
99 lines
2.4 KiB
Rust
//! Module responsible for making [Point]s [Mentionable].
|
|
|
|
use std::{error::Error, fmt::Display};
|
|
|
|
use crate::rcore::*;
|
|
use crate::rstd::inlining::*;
|
|
|
|
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Serializable for Point<'a, Ctx, A> {
|
|
fn serialize(&self, serializer: &mut dyn Serializer) {
|
|
serializer.write(&self.point)
|
|
}
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> MentionableBase<'a, Ctx>
|
|
for Point<'a, Ctx, A>
|
|
{
|
|
type Fctr = PointFactory<A::Fctr>;
|
|
|
|
fn factory(&self) -> Self::Fctr {
|
|
PointFactory {
|
|
factory: self.origin.factory(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableTop<'a, Ctx> for Point<'a, Ctx, A> {
|
|
fn topology(&self) -> Hash {
|
|
Ctx::hash(&self.point)
|
|
}
|
|
|
|
fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>) {
|
|
points.visit(self)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct PointFactory<F> {
|
|
factory: F,
|
|
}
|
|
|
|
impl<F: Clone> PointFactory<F> {
|
|
pub fn inner(&self) -> F {
|
|
self.factory.clone()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum PointParseError {
|
|
WrongLength(usize),
|
|
}
|
|
|
|
impl Display for PointParseError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Self::WrongLength(length) => {
|
|
write!(f, "expected {} bytes, received {}", HASH_SIZE, length)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Error for PointParseError {}
|
|
|
|
impl From<&[u8]> for PointParseError {
|
|
fn from(value: &[u8]) -> Self {
|
|
PointParseError::WrongLength(value.len())
|
|
}
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> AsRef<[u8]> for Point<'a, Ctx, A> {
|
|
fn as_ref(&self) -> &[u8] {
|
|
&self.point
|
|
}
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> FactoryBase<'a, Ctx> for PointFactory<F> {
|
|
type Mtbl = Point<'a, Ctx, F::Mtbl>;
|
|
|
|
type ParseError = PointParseError;
|
|
}
|
|
|
|
impl<F> ParseMode for PointFactory<F> {
|
|
type Mode = InliningMode;
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, F: Factory<'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;
|
|
}
|