1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pub mod walkers;

use crate::{
    effect::{Effect, Yield},
    protocol::Visitor,
};

/// A type that can be walked.
pub trait Walk<'ctx>: Sized {
    /// The walker for the type.
    type Walker: Walker<'ctx> + From<Self>;
}

pub fn into_walker<'ctx, T: Walk<'ctx>>(value: T) -> T::Walker {
    <T as Walk<'ctx>>::Walker::from(value)
}

/// 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> {
    type Effect: Effect<'ctx, Result<Self::Output, Self::Error>>;

    type Error;

    /// An arbitrary type the walker is left with after walking.
    ///
    /// Its recommended that this is `Self` if the walker is repeatable.
    type Output;

    /// Walk the value.
    ///
    /// The walker should send data to the `visitor` as it walks the value.
    #[must_use]
    fn walk<'a>(
        self,
        visitor: Visitor<'a, 'ctx, Self::Effect>,
    ) -> Yield<'a, 'ctx, Result<Self::Output, Self::Error>, Self::Effect>
    where
        Self: 'a,
        'ctx: 'a;
}