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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "alloc")]
extern crate alloc;

// pub mod build;
pub mod error;
// pub mod impls;
pub mod protocol;
// pub mod protocols;
// pub mod transform;
// pub mod walk;

// use buildable::Buildable;
use protocol::{AnyHint, Hint, ProtocolId, Visit, AnyVisit};

// pub use buildable::Buildable;
// pub use walkable::{Walkable, WalkableMut, WalkableRef};

/// Status of a walker after walking using a visitor.
///
/// Some walkers can be walked multiple times to extract multiple
/// values.
pub enum WalkStatus {
    /// The walker is done.
    ///
    /// Attemping to call `walk` is likely to result in an error.
    Done,

    /// The walker has more values to walk.
    More,

    /// The walker will repeat values.
    Repeat,

    Error,
}

/// Walker over a value with lifetime `'value`.
pub trait Walker<'ctx> {
    /// Walk the walker over the value.
    ///
    /// This is the main entrypoint for walking a value.
    /// The walker should call [`Visit::visit`] on the provided visitor as it walks.
    ///
    /// The default impl calls [`Visitor::request_hint`] then returns an error if no hint
    /// was given. Self describing formats can replace the default impl to fall back to
    /// their own logic if no hint is given. It is recommended to keep the call to
    /// [`Visitor::request_hint`] before using walker specific logic to pick a protocol.
    fn walk(
        &mut self,
        visitor: &mut dyn Visitor<'ctx>,
    ) -> WalkStatus;
}

pub trait ErrorNeedsHint {
    fn error_needs_hint(&mut self);
}

pub fn walk_with_hints<'ctx, H: WalkerHints<'ctx>>(
    hints: &mut H,
    visitor: &mut dyn Visitor<'ctx>,
) -> WalkStatus
where
    H: ErrorNeedsHint,
{
    // Request that the visitor give us a hint of what protocol to use.
    match visitor.request_hint(hints, true) {
        Some(status) => status,
        None => {
            hints.error_needs_hint();
            WalkStatus::Error
        },
    }
}

/// Hint lookup for a walker.
pub trait WalkerHints<'ctx> {
    /// Query the walker for a given protocol.
    ///
    /// If the walker doesn't support the protocol then a `None` is returned.
    /// Note, a walker can return `None` if it can't handle a hint of the protocol for the given
    /// value.
    fn protocol(
        &mut self,
        id: ProtocolId,
    ) -> Option<AnyHint<'_, 'ctx>>;
}

/// Visitor over a value to be built.
pub trait Visitor<'ctx> {
    /// Request the visitor hint what protocol to use.
    ///
    /// It is not recommended to call this while in a protocol hint as a walker.
    /// Calling this method when already processing a hint can cause a infinite loop.
    ///
    /// The visitor will hint the protocol by calling the [`Hint::hint`] method on the
    /// the walker's returned hint instance for the protocol.
    ///
    /// A return value of `Ok(None)` means no hint was given to the walker.
    fn request_hint(
        &mut self,
        hints: &mut dyn WalkerHints<'ctx>,
        need_hint: bool,
    ) -> Option<WalkStatus> {
        let _ = hints;
        let _ = need_hint;
        None
    }

    /// Query the visitor for a given protocol.
    ///
    /// If the visitor doesn't support the protocol then a `None` is returned.
    fn protocol(
        &mut self,
        id: ProtocolId,
    ) -> Option<AnyVisit<'_, 'ctx>>;
}