#![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>>;
}