use super::*;
#[derive(Debug)]
pub enum ResolutionError<L, P> {
Lookup(L),
Parse(P),
}
impl<L, P> ResolutionError<L, P> {
pub fn map_parse<Px>(self, f: impl FnOnce(P) -> Px) -> ResolutionError<L, Px> {
match self {
Self::Lookup(l) => ResolutionError::Lookup(l),
Self::Parse(p) => ResolutionError::Parse(f(p)),
}
}
}
pub type LookupError<'a, Ctx> = <Ctx as Context<'a>>::LookupError;
pub type ResolutionFailure<'a, Ctx, A> = ResolutionError<LookupError<'a, Ctx>, ParseErrorA<'a, A>>;
pub type ResolutionResult<'a, Ctx, A> = Result<Arc<A>, ResolutionFailure<'a, Ctx, A>>;
pub type Resolution<'a, Ctx, A> = Wrapped<'a, Ctx, ResolutionResult<'a, Ctx, A>>;
pub type HashResolutionResult<'a, Ctx> =
Result<(Vec<u8>, Arc<dyn Resolver<'a, Ctx>>), LookupError<'a, Ctx>>;
pub type HashResolution<'a, Ctx> = Wrapped<'a, Ctx, HashResolutionResult<'a, Ctx>>;
#[derive(Clone, Copy, Debug)]
pub struct Address {
pub point: Hash,
pub index: usize,
}
pub trait Resolver<'a, Ctx: Context<'a>>: 'a + Send + Sync {
fn resolve(self: Arc<Self>, address: Address) -> HashResolution<'a, Ctx>;
}
pub trait ResolverMap<'a, Ctx: Context<'a>>: Resolver<'a, Ctx> {
fn resolve_map<T: 'a + Send>(
self: Arc<Self>,
address: Address,
f: impl 'a + Send + FnOnce(HashResolutionResult<'a, Ctx>) -> T,
) -> Wrapped<'a, Ctx, T> {
Ctx::fmap(self.resolve(address), f)
}
}
impl<'a, Ctx: Context<'a>, R: ?Sized + Resolver<'a, Ctx>> ResolverMap<'a, Ctx> for R {}