Compare commits
No commits in common. "c20df8108b7ac9ae2feb0a4c540e2aa4e92aaad0" and "f0170910f6e335b346800b170a52eb94425f340c" have entirely different histories.
c20df8108b
...
f0170910f6
@ -21,11 +21,12 @@ pub type ResolutionResult<'a, Ctx, A> = Result<Rc<A>, ResolutionFailure<'a, Ctx,
|
|||||||
/// Wrapped result returned by [`Origin`].
|
/// Wrapped result returned by [`Origin`].
|
||||||
pub type Resolution<'a, Ctx, A> = Wrapped<'a, Ctx, ResolutionResult<'a, Ctx, A>>;
|
pub type Resolution<'a, Ctx, A> = Wrapped<'a, Ctx, ResolutionResult<'a, Ctx, A>>;
|
||||||
|
|
||||||
pub type HashResolutionResult<'a, Ctx> =
|
|
||||||
Result<(Vec<u8>, Rc<dyn Resolver<'a, Ctx>>), <Ctx as Context>::LookupError<'a>>;
|
|
||||||
|
|
||||||
/// Shorthand for the type of values returned by [`Resolver::resolve`].
|
/// Shorthand for the type of values returned by [`Resolver::resolve`].
|
||||||
pub type HashResolution<'a, Ctx> = Wrapped<'a, Ctx, HashResolutionResult<'a, Ctx>>;
|
pub type HashResolution<'a, Ctx> = Wrapped<
|
||||||
|
'a,
|
||||||
|
Ctx,
|
||||||
|
Result<(Vec<u8>, Rc<dyn Resolver<'a, Ctx>>), <Ctx as Context>::LookupError<'a>>,
|
||||||
|
>;
|
||||||
|
|
||||||
/// Value accepted by [`Resolver::resolve`]. Includes index to make it order-sensitive.
|
/// Value accepted by [`Resolver::resolve`]. Includes index to make it order-sensitive.
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
159
src/std/cast.rs
159
src/std/cast.rs
@ -60,90 +60,44 @@ impl<'a> Display for CastError<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CastError<'a> {
|
|
||||||
fn pure<Ctx: 'a + Context>(self) -> HashResolution<'a, Ctx>
|
|
||||||
where
|
|
||||||
Ctx::LookupError<'a>: From<CastError<'a>>,
|
|
||||||
{
|
|
||||||
Ctx::T::pure(Err(self.into()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, Ctx: 'a + Context> CastResolver<'a, Ctx> {
|
|
||||||
fn rc(points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) -> Rc<dyn Resolver<'a, Ctx>>
|
|
||||||
where
|
|
||||||
Ctx::LookupError<'a>: From<CastError<'a>>,
|
|
||||||
{
|
|
||||||
Rc::new(Self { points })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn _get_point(
|
|
||||||
&self,
|
|
||||||
index: usize,
|
|
||||||
) -> Result<&Point<'a, Ctx, TypelessMentionable<'a, Ctx>>, CastError<'a>> {
|
|
||||||
match self.points.get(index) {
|
|
||||||
Some(point) => Ok(point),
|
|
||||||
None => Err(CastError::AddressIndexOutOfBounds {
|
|
||||||
index,
|
|
||||||
length: self.points.len(),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn _validate_point(
|
|
||||||
&self,
|
|
||||||
point: &Point<'a, Ctx, TypelessMentionable<'a, Ctx>>,
|
|
||||||
address: Address,
|
|
||||||
) -> Result<(), CastError<'a>> {
|
|
||||||
if point.point == address.point {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(CastError::AddressPointMismatch {
|
|
||||||
expected: point.point,
|
|
||||||
received: address.point,
|
|
||||||
index: address.index,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_point(
|
|
||||||
&self,
|
|
||||||
address: Address,
|
|
||||||
) -> Result<&Point<'a, Ctx, TypelessMentionable<'a, Ctx>>, CastError<'a>> {
|
|
||||||
let point = self._get_point(address.index)?;
|
|
||||||
self._validate_point(point, address)?;
|
|
||||||
Ok(point)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cast_resolved<'a, Ctx: 'a + Context>(
|
|
||||||
resolved: ResolutionResult<'a, Ctx, TypelessMentionable<'a, Ctx>>,
|
|
||||||
) -> HashResolutionResult<'a, Ctx>
|
|
||||||
where
|
|
||||||
Ctx::LookupError<'a>: From<CastError<'a>>,
|
|
||||||
{
|
|
||||||
match resolved {
|
|
||||||
Ok(mentionable) => Ok((
|
|
||||||
mentionable.bytes(),
|
|
||||||
CastResolver::rc(mentionable.points_vec()),
|
|
||||||
)),
|
|
||||||
Err(error) => Err(match error {
|
|
||||||
ResolutionError::Lookup(lookup_error) => lookup_error,
|
|
||||||
ResolutionError::Parse(parse_error) => CastError::Typeless(parse_error).into(),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, Ctx: 'a + Context> Resolver<'a, Ctx> for CastResolver<'a, Ctx>
|
impl<'a, Ctx: 'a + Context> Resolver<'a, Ctx> for CastResolver<'a, Ctx>
|
||||||
where
|
where
|
||||||
Ctx::LookupError<'a>: From<CastError<'a>>,
|
Ctx::LookupError<'a>: From<CastError<'a>>,
|
||||||
{
|
{
|
||||||
fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx> {
|
fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx> {
|
||||||
let point = match self.get_point(address) {
|
let point = match self.points.get(address.index) {
|
||||||
Ok(point) => point,
|
Some(point) => point,
|
||||||
Err(cast_error) => return cast_error.pure::<Ctx>(),
|
None => {
|
||||||
|
return Ctx::T::pure(Err(CastError::AddressIndexOutOfBounds {
|
||||||
|
index: address.index,
|
||||||
|
length: self.points.len(),
|
||||||
|
}
|
||||||
|
.into()));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Ctx::T::fmap(cast_resolved, point.resolve())
|
if point.point != address.point {
|
||||||
|
return Ctx::T::pure(Err(CastError::AddressPointMismatch {
|
||||||
|
expected: point.point,
|
||||||
|
received: address.point,
|
||||||
|
index: address.index,
|
||||||
|
}
|
||||||
|
.into()));
|
||||||
|
}
|
||||||
|
Ctx::T::fmap(
|
||||||
|
|resolved| match resolved {
|
||||||
|
Ok(mentionable) => {
|
||||||
|
let resolver: Rc<dyn Resolver<'a, Ctx>> = Rc::new(CastResolver {
|
||||||
|
points: mentionable.points_vec(),
|
||||||
|
});
|
||||||
|
Ok((mentionable.bytes(), resolver))
|
||||||
|
}
|
||||||
|
Err(error) => Err(match error {
|
||||||
|
ResolutionError::Lookup(lookup_error) => lookup_error,
|
||||||
|
ResolutionError::Parse(parse_error) => CastError::Typeless(parse_error).into(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
point.resolve(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +116,9 @@ where
|
|||||||
) -> CastResult<'a, Ctx, A> {
|
) -> CastResult<'a, Ctx, A> {
|
||||||
factory.parse_slice(
|
factory.parse_slice(
|
||||||
&self.bytes(),
|
&self.bytes(),
|
||||||
map_resolver(CastResolver::rc(self.points_vec())),
|
map_resolver(Rc::new(CastResolver {
|
||||||
|
points: self.points_vec(),
|
||||||
|
})),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,13 +128,15 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cast_resolve<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>(
|
impl<'a, Ctx: Context> Point<'a, Ctx, TypelessMentionable<'a, Ctx>>
|
||||||
typeless_origin: Rc<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>,
|
|
||||||
factory: A::Fctr,
|
|
||||||
) -> Resolution<'a, Ctx, A>
|
|
||||||
where
|
where
|
||||||
Ctx::LookupError<'a>: From<CastError<'a>>,
|
Ctx::LookupError<'a>: From<CastError<'a>>,
|
||||||
{
|
{
|
||||||
|
/// See [`TypelessMentionable::cast`]
|
||||||
|
pub fn cast<A: Mentionable<'a, Ctx>>(&self, factory: A::Fctr) -> Point<'a, Ctx, A> {
|
||||||
|
let typeless_origin = self.origin.clone();
|
||||||
|
let origin: Rc<dyn Origin<Ctx, Mtbl = A>> = wrapped_origin(factory.clone(), move || {
|
||||||
|
let factory = factory.clone();
|
||||||
Ctx::T::fmap(
|
Ctx::T::fmap(
|
||||||
move |resolved| match resolved {
|
move |resolved| match resolved {
|
||||||
Ok(typeless_mentionable) => match typeless_mentionable.cast(factory) {
|
Ok(typeless_mentionable) => match typeless_mentionable.cast(factory) {
|
||||||
@ -187,42 +145,17 @@ where
|
|||||||
},
|
},
|
||||||
Err(error) => Err(ResolutionError::Lookup(match error {
|
Err(error) => Err(ResolutionError::Lookup(match error {
|
||||||
ResolutionError::Lookup(lookup_error) => lookup_error,
|
ResolutionError::Lookup(lookup_error) => lookup_error,
|
||||||
ResolutionError::Parse(parse_error) => CastError::Typeless(parse_error).into(),
|
ResolutionError::Parse(parse_error) => {
|
||||||
|
CastError::Typeless(parse_error).into()
|
||||||
|
}
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
typeless_origin.clone().resolve(),
|
typeless_origin.clone().resolve(),
|
||||||
)
|
)
|
||||||
}
|
});
|
||||||
|
|
||||||
fn cast_origin<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>(
|
|
||||||
typeless_origin: Rc<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>,
|
|
||||||
factory: A::Fctr,
|
|
||||||
) -> Rc<dyn Origin<'a, Ctx, Mtbl = A>>
|
|
||||||
where
|
|
||||||
Ctx::LookupError<'a>: From<CastError<'a>>,
|
|
||||||
{
|
|
||||||
wrapped_origin(factory.clone(), move || {
|
|
||||||
cast_resolve(typeless_origin.clone(), factory.clone())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, Ctx: 'a + Context> Point<'a, Ctx, TypelessMentionable<'a, Ctx>>
|
|
||||||
where
|
|
||||||
Ctx::LookupError<'a>: From<CastError<'a>>,
|
|
||||||
{
|
|
||||||
fn cast_origin<A: Mentionable<'a, Ctx>>(
|
|
||||||
&self,
|
|
||||||
factory: A::Fctr,
|
|
||||||
) -> Rc<dyn Origin<'a, Ctx, Mtbl = A>> {
|
|
||||||
let typeless_origin = self.origin.clone();
|
|
||||||
cast_origin(typeless_origin, factory)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// See [`TypelessMentionable::cast`]
|
|
||||||
pub fn cast<A: Mentionable<'a, Ctx>>(&self, factory: A::Fctr) -> Point<'a, Ctx, A> {
|
|
||||||
Point {
|
Point {
|
||||||
point: self.point,
|
point: self.point,
|
||||||
origin: self.cast_origin(factory),
|
origin,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user