pub mod walkers;
use crate::{
effect::{Effect, Future},
protocol::Visitor,
Flow,
};
/// A type that can be walked.
pub trait Walk<'ctx, M, E: Effect>: WalkerTypes + Sized {
/// The walker for the type.
type Walker: Walker<'ctx, E, Error = Self::Error, Output = Self::Output>;
#[must_use]
fn into_walker(self) -> Self::Walker;
}
pub trait WalkerTypes {
type Error: Send;
/// An arbitrary type the walker is left with after walking.
///
/// Its recommended that this is `Self` if the walker is repeatable.
type Output: Send;
}
/// Walker for a type.
///
/// The `'ctx` lifetime is some lifetime that is longer than `Self`.
/// Data from the value may borrow using `'ctx`.
///
/// The way to use a walker is as follows.
/// - Call [From::from()] with a value to be walked to make a walker.
/// - Call [Self::walk()] to walk the value. Data will be sent to the provided
/// visitor.
pub trait Walker<'ctx, E: Effect>: WalkerTypes + Send {
/// Walk the value.
///
/// The walker should send data to the `visitor` as it walks the value.
fn walk<'a>(
self,
visitor: Visitor<'a, 'ctx>,
) -> Future<'a, Result<Self::Output, Self::Error>, E>
where
Self: 'a;
}
pub trait WalkerObjSafe<'ctx, E: Effect>: Send {
fn walk<'a>(&'a mut self, visitor: Visitor<'a, 'ctx>) -> Future<'a, Flow, E>
where
Self: 'a;
}
pub type DynWalker<'a, 'ctx, E> = &'a mut (dyn WalkerObjSafe<'ctx, E> + Send + 'a);
enum DynWalkerState<W: WalkerTypes> {
Walking,
Pending(W),
Done(W::Output),
Err(W::Error),
}
pub enum DynWalkerError<W: WalkerTypes> {
NeverWalked(W),
/// This can only happen if a panic happens furing the walk and is then caught before calling
/// finish..
WalkNeverFinished,
Walker(W::Error),
WasWalked(W::Output),
}
pub struct DynWalkerAdapter<W: WalkerTypes> {
state: DynWalkerState<W>,
}
impl<W: WalkerTypes> DynWalkerAdapter<W> {
pub fn new(walker: W) -> Self {
Self {
state: DynWalkerState::Pending(walker),
}
}
pub fn finish(self) -> Result<W::Output, DynWalkerError<W>> {
match self.state {
DynWalkerState::Walking => Err(DynWalkerError::WalkNeverFinished),
DynWalkerState::Pending(walker) => Err(DynWalkerError::NeverWalked(walker)),
DynWalkerState::Done(value) => Ok(value),
DynWalkerState::Err(err) => Err(DynWalkerError::Walker(err)),
}
}
pub fn into_innter(self) -> Result<W, DynWalkerError<W>> {
match self.state {
DynWalkerState::Walking => Err(DynWalkerError::WalkNeverFinished),
DynWalkerState::Pending(walker) => Ok(walker),
DynWalkerState::Done(value) => Err(DynWalkerError::WasWalked(value)),
DynWalkerState::Err(err) => Err(DynWalkerError::Walker(err)),
}
}
}
impl<'ctx, W: Walker<'ctx, E>, E: Effect> WalkerObjSafe<'ctx, E> for DynWalkerAdapter<W> {
fn walk<'a>(&'a mut self, visitor: Visitor<'a, 'ctx>) -> Future<'a, Flow, E>
where
Self: 'a,
{
E::wrap(async {
if let DynWalkerState::Pending(walker) =
core::mem::replace(&mut self.state, DynWalkerState::Walking)
{
// Walk the walker.
match walker.walk(visitor).await {
Ok(value) => {
self.state = DynWalkerState::Done(value);
Flow::Done
}
Err(err) => {
self.state = DynWalkerState::Err(err);
// Signal that control flow should stop as soon as possible as we
// are in an error state.
Flow::Break
}
}
} else {
// Can't do anything if the walker has already been walked.
Flow::Done
}
})
}
}