extract resolver_origin

This commit is contained in:
AF 2023-04-23 00:52:07 +00:00
parent 7951425738
commit 0cbb98ac84
4 changed files with 56 additions and 52 deletions

View File

@ -2,12 +2,15 @@
//! Brings [`Mentionable`]/[`Factory`]/[`Origin`] concepts from the original implementation in Python. //! Brings [`Mentionable`]/[`Factory`]/[`Origin`] concepts from the original implementation in Python.
//! Allows for more generic behaviour via [`Context`], as opposed to original async-only. //! Allows for more generic behaviour via [`Context`], as opposed to original async-only.
mod addresses;
mod hashing; mod hashing;
mod resolution; mod resolution;
mod resolver_origin;
mod serialization; mod serialization;
mod slice_deserializer; mod slice_deserializer;
mod typeless; mod typeless;
pub use addresses::*;
pub use hashing::*; pub use hashing::*;
pub use resolution::*; pub use resolution::*;
pub use serialization::*; pub use serialization::*;

0
src/core/addresses.rs Normal file
View File

View File

@ -41,24 +41,6 @@ pub trait Resolver<'a, Ctx: 'a + Context>: 'a {
fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx>; fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx>;
} }
impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> {
/// Make a [Point] from an [Address].
pub fn from_address(
address: Address,
factory: A::Fctr,
resolver: Rc<dyn Resolver<'a, Ctx>>,
) -> Self {
Point {
point: address.point,
origin: Rc::new(ResolverOrigin {
r_factory: factory,
r_address: address,
r_resolver: resolver,
}),
}
}
}
/// Preferred way to parse [Point]s off of a [Serializer]. /// Preferred way to parse [Point]s off of a [Serializer].
pub struct Addresses { pub struct Addresses {
current: usize, current: usize,
@ -99,40 +81,6 @@ impl Addresses {
} }
} }
struct ResolverOrigin<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> {
r_factory: F,
r_address: Address,
r_resolver: Rc<dyn Resolver<'a, Ctx>>,
}
fn _resolve_origin<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>(
origin: Rc<ResolverOrigin<'a, Ctx, A::Fctr>>,
) -> Resolution<'a, Ctx, A> {
let resolution = origin.r_resolver.clone().resolve(origin.r_address);
Ctx::T::fmap(
move |resolved| match resolved {
Ok((src, resolver)) => match origin.r_factory.parse_slice(&src, resolver) {
Ok(mentionable) => Ok(Rc::new(mentionable)),
Err(parse_error) => Err(ResolutionError::Parse(parse_error)),
},
Err(lookup_error) => Err(ResolutionError::Lookup(lookup_error)),
},
resolution,
)
}
impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> Origin<'a, Ctx> for ResolverOrigin<'a, Ctx, F> {
type Mtbl = F::Mtbl;
fn factory(&self) -> <Self::Mtbl as Mentionable<'a, Ctx>>::Fctr {
self.r_factory.clone()
}
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl> {
_resolve_origin(self)
}
}
fn _parse_slice<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>( fn _parse_slice<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>(
factory: &A::Fctr, factory: &A::Fctr,
slice: &[u8], slice: &[u8],

View File

@ -0,0 +1,53 @@
use super::*;
impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> {
/// Make a [Point] from an [Address].
pub fn from_address(
address: Address,
factory: A::Fctr,
resolver: Rc<dyn Resolver<'a, Ctx>>,
) -> Self {
Point {
point: address.point,
origin: Rc::new(ResolverOrigin {
r_factory: factory,
r_address: address,
r_resolver: resolver,
}),
}
}
}
struct ResolverOrigin<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> {
r_factory: F,
r_address: Address,
r_resolver: Rc<dyn Resolver<'a, Ctx>>,
}
fn _resolve_origin<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>(
origin: Rc<ResolverOrigin<'a, Ctx, A::Fctr>>,
) -> Resolution<'a, Ctx, A> {
let resolution = origin.r_resolver.clone().resolve(origin.r_address);
Ctx::T::fmap(
move |resolved| match resolved {
Ok((src, resolver)) => match origin.r_factory.parse_slice(&src, resolver) {
Ok(mentionable) => Ok(Rc::new(mentionable)),
Err(parse_error) => Err(ResolutionError::Parse(parse_error)),
},
Err(lookup_error) => Err(ResolutionError::Lookup(lookup_error)),
},
resolution,
)
}
impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> Origin<'a, Ctx> for ResolverOrigin<'a, Ctx, F> {
type Mtbl = F::Mtbl;
fn factory(&self) -> <Self::Mtbl as Mentionable<'a, Ctx>>::Fctr {
self.r_factory.clone()
}
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl> {
_resolve_origin(self)
}
}