radn-rs/src/rcore/resolver_origin.rs
2023-05-26 08:52:58 +00:00

55 lines
1.5 KiB
Rust

use super::*;
impl<'a, Ctx: Context<'a>, 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: Context<'a>, F: Factory<'a, Ctx>> {
r_factory: F,
r_address: Address,
r_resolver: Rc<dyn Resolver<'a, Ctx>>,
}
fn _resolve_origin<'a, Ctx: Context<'a>, 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| {
let (src, resolver) = resolved.map_err(ResolutionError::Lookup)?;
let mentionable = origin
.r_factory
.parse_slice(&src, resolver)
.map_err(ResolutionError::Parse)?;
Ok(Rc::new(mentionable))
},
resolution,
)
}
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Origin<'a, Ctx> for ResolverOrigin<'a, Ctx, F> {
type Mtbl = F::Mtbl;
fn factory(&self) -> OFctr<'a, Ctx, Self> {
self.r_factory.clone()
}
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl> {
_resolve_origin(self)
}
}