pub trait Handler<Ctx, R>: Send + Sync {
// Required method
fn handle<'tree>(&self, input: Input<'tree, Ctx>) -> HandlerResult<R>;
}Expand description
Core abstraction: maps an Input to an optional output value.
Implement this trait to create a custom handler, or use the
free-function constructors (crate::handler_fn, crate::always, …)
and the combinator extension methods provided by crate::HandlerExt.
Lifetimes are on the handle method, not on the trait itself, so a
single handler instance can be reused across nodes from different trees.
§Example
use tree_sitter_utils::{Handler, Input, HandlerResult};
struct MyHandler;
impl<Ctx: Copy> Handler<Ctx, String> for MyHandler {
fn handle<'tree>(&self, input: Input<'tree, Ctx>) -> HandlerResult<String> {
Some(input.node.kind().to_owned())
}
}Required Methods§
Sourcefn handle<'tree>(&self, input: Input<'tree, Ctx>) -> HandlerResult<R>
fn handle<'tree>(&self, input: Input<'tree, Ctx>) -> HandlerResult<R>
Attempt to produce an output value for the given input.
Returns None when this handler does not apply.
Implementors§
impl<Ctx, R> Handler<Ctx, R> for BoxedHandler<Ctx, R>where
Ctx: Copy,
impl<Ctx, R> Handler<Ctx, R> for DispatchOnKind<Ctx, R>where
Ctx: Copy + 'static,
R: 'static,
impl<Ctx, R> Handler<Ctx, R> for FirstOf<Ctx, R>where
Ctx: Copy,
impl<Ctx, R> Handler<Ctx, R> for Never<Ctx, R>
impl<Ctx, R, A, B> Handler<Ctx, R> for Or<A, B>
impl<Ctx, R, F> Handler<Ctx, R> for HandlerFn<F>
impl<Ctx, R, F> Handler<Ctx, R> for F
Blanket implementation: any Fn(Input<'_, Ctx>) -> Option<R> is a handler.
§Example
use tree_sitter_utils::{Handler, Input};
fn use_handler<H: Handler<(), String>>(h: &H) { let _ = h; }
let f = |input: Input<()>| -> Option<String> {
Some(input.node.kind().to_owned())
};
use_handler(&f);