From be427e4e87771dc4845b16261fb27ec3020e1802 Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 1 Aug 2023 20:50:23 +0000 Subject: [PATCH] `OriginMap` --- src/rcore.rs | 2 +- src/rcore/origin.rs | 22 ++++++++++++++++++++++ src/rcore/point.rs | 4 ++-- src/rstd/cast.rs | 27 +++++++++++---------------- src/rstd/wrapped_origin.rs | 2 +- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/rcore.rs b/src/rcore.rs index 0a17b18..d88c8bb 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -35,7 +35,7 @@ pub use self::inlining::{CInliningFactory, IParseResult, InliningFactory}; pub use self::modes::{ ExtensionResultM, ExtensionSourceM, FactoryModeParse, FactoryModeProxy, ModeResultM, }; -pub use self::origin::{OFctr, Origin}; +pub use self::origin::{OFctr, Origin, OriginMap}; pub use self::point::Point; pub use self::points::PointsVisitor; pub use self::regular::{CRegularFactory, RegularFactory}; diff --git a/src/rcore/origin.rs b/src/rcore/origin.rs index 947340b..ff594a6 100644 --- a/src/rcore/origin.rs +++ b/src/rcore/origin.rs @@ -16,3 +16,25 @@ pub trait Origin<'a, Ctx: Context<'a>>: 'a { /// Type of the [`Factory`] associated with the [`Origin`]. pub type OFctr<'a, Ctx, O> = Fctr<'a, Ctx, >::Mtbl>; + +/// [`OriginMap::resolve_map`]. +pub trait OriginMap<'a, Ctx: Context<'a>>: Origin<'a, Ctx> { + fn ref_resolve(self: &Rc) -> Resolution<'a, Ctx, Self::Mtbl> + where + OFctr<'a, Ctx, Self>: FactoryParse<'a, Ctx>, + { + self.clone().resolve() + } + + fn resolve_map( + self: &Rc, + f: impl 'a + FnOnce(ResolutionResult<'a, Ctx, Self::Mtbl>) -> T, + ) -> Wrapped<'a, Ctx, T> + where + OFctr<'a, Ctx, Self>: FactoryParse<'a, Ctx>, + { + Ctx::fmap(self.ref_resolve(), f) + } +} + +impl<'a, Ctx: Context<'a>, R: ?Sized + Origin<'a, Ctx>> OriginMap<'a, Ctx> for R {} diff --git a/src/rcore/point.rs b/src/rcore/point.rs index 6349a6d..fa51f11 100644 --- a/src/rcore/point.rs +++ b/src/rcore/point.rs @@ -32,7 +32,7 @@ where { /// See [`Origin::resolve`]. pub fn resolve(&self) -> Resolution<'a, Ctx, A> { - self.origin.clone().resolve() + self.origin.ref_resolve() } /// Resolve the object, then map the [ResolutionResult]. @@ -40,6 +40,6 @@ where &self, f: impl 'a + FnOnce(ResolutionResult<'a, Ctx, A>) -> B, ) -> Wrapped<'a, Ctx, B> { - Ctx::fmap(self.resolve(), f) + self.origin.resolve_map(f) } } diff --git a/src/rstd/cast.rs b/src/rstd/cast.rs index ac2abf2..8cfdba4 100644 --- a/src/rstd/cast.rs +++ b/src/rstd/cast.rs @@ -126,7 +126,7 @@ impl<'a, Ctx: CastCtx<'a>> CastResolver<'a, Ctx> { } } -fn cast_resolved<'a, Ctx: CastCtx<'a>>( +fn map_resolved<'a, Ctx: CastCtx<'a>>( resolved: ResolutionResult<'a, Ctx, TypelessMentionable<'a, Ctx>>, ) -> HashResolutionResult<'a, Ctx> { match resolved { @@ -147,7 +147,7 @@ impl<'a, Ctx: CastCtx<'a>> Resolver<'a, Ctx> for CastResolver<'a, Ctx> { Ok(point) => point, Err(cast_error) => return cast_error.pure::(), }; - point.resolve_map(cast_resolved) + point.resolve_map(map_resolved) } } @@ -173,21 +173,16 @@ fn cast_resolve<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>>( typeless_origin: Rc>>, factory: A::Fctr, ) -> Resolution<'a, Ctx, A> { - Ctx::fmap( - typeless_origin.clone().resolve(), - move |resolved| match resolved { - Ok(typeless_mentionable) => match typeless_mentionable.cast(factory) { - Ok(mentionable) => Ok(Rc::new(mentionable)), - Err(parse_error) => Err(ResolutionError::Parse(parse_error)), - }, - Err(error) => Err(ResolutionError::Lookup(match error { - ResolutionError::Lookup(lookup_error) => lookup_error, - ResolutionError::Parse(parse_error) => { - Ctx::from_cast(CastError::Typeless(parse_error)) - } - })), + typeless_origin.resolve_map(|resolved| match resolved { + Ok(typeless_mentionable) => match typeless_mentionable.cast(factory) { + Ok(mentionable) => Ok(Rc::new(mentionable)), + Err(parse_error) => Err(ResolutionError::Parse(parse_error)), }, - ) + Err(error) => Err(ResolutionError::Lookup(match error { + ResolutionError::Lookup(lookup_error) => lookup_error, + ResolutionError::Parse(parse_error) => Ctx::from_cast(CastError::Typeless(parse_error)), + })), + }) } fn cast_origin<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>>( diff --git a/src/rstd/wrapped_origin.rs b/src/rstd/wrapped_origin.rs index 2641695..71f1e99 100644 --- a/src/rstd/wrapped_origin.rs +++ b/src/rstd/wrapped_origin.rs @@ -37,7 +37,7 @@ pub trait MappableOrigin<'a, Ctx: Context<'a>>: Origin<'a, Ctx> { let origin = origin_r.clone(); let map_ok = map_ok.clone(); let map_err = map_err.clone(); - map_resolve(move || origin.clone().resolve(), map_ok, map_err) + map_resolve(move || origin.ref_resolve(), map_ok, map_err) }), w_resolve_bytes: Box::new(move || origin_rb.clone().resolve_bytes()), };