Skip to main content

Handler

Trait Handler 

Source
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§

Source

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§

Source§

impl<Ctx, R> Handler<Ctx, R> for BoxedHandler<Ctx, R>
where Ctx: Copy,

Source§

impl<Ctx, R> Handler<Ctx, R> for DispatchOnKind<Ctx, R>
where Ctx: Copy + 'static, R: 'static,

Source§

impl<Ctx, R> Handler<Ctx, R> for FirstOf<Ctx, R>
where Ctx: Copy,

Source§

impl<Ctx, R> Handler<Ctx, R> for Never<Ctx, R>

Source§

impl<Ctx, R, A, B> Handler<Ctx, R> for Or<A, B>
where A: Handler<Ctx, R>, B: Handler<Ctx, R>, Ctx: Copy,

Source§

impl<Ctx, R, F> Handler<Ctx, R> for HandlerFn<F>
where F: Fn(Input<'_, Ctx>) -> R + Send + Sync, Ctx: Copy,

Source§

impl<Ctx, R, F> Handler<Ctx, R> for F
where F: Fn(Input<'_, Ctx>) -> Option<R> + Send + Sync,

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);
Source§

impl<Ctx, R, H> Handler<Ctx, Vec<R>> for ForChildren<H>
where H: Handler<Ctx, R>, Ctx: Copy,

Source§

impl<Ctx, R, H> Handler<Ctx, R> for Climb<H>
where H: Handler<Ctx, R>, Ctx: Copy,

Source§

impl<Ctx, R, H> Handler<Ctx, R> for FindAncestor<H>
where H: Handler<Ctx, R>, Ctx: Copy,

Source§

impl<Ctx, R, H> Handler<Ctx, R> for ScanChildren<H>
where H: Handler<Ctx, R>, Ctx: Copy,

Source§

impl<Ctx, R, H, F> Handler<Ctx, R> for MapInput<H, F>
where H: Handler<Ctx, R>, F: for<'tree> Fn(Input<'tree, Ctx>) -> Input<'tree, Ctx> + Send + Sync, Ctx: Copy,

Source§

impl<Ctx, R, H, O> Handler<Ctx, R> for OrElseClimb<H, O>
where H: Handler<Ctx, R>, O: Handler<Ctx, R>, Ctx: Copy,

Source§

impl<Ctx, R, H, P> Handler<Ctx, R> for When<H, P>
where H: Handler<Ctx, R>, P: NodePredicate<Ctx>, Ctx: Copy,

Source§

impl<Ctx, R, R2, H, F> Handler<Ctx, R2> for AndThen<H, F, R>
where H: Handler<Ctx, R>, F: Fn(Input<'_, Ctx>, R) -> HandlerResult<R2> + Send + Sync, Ctx: Copy,

Source§

impl<Ctx, R, R2, H, F> Handler<Ctx, R2> for Map<H, F, R>
where H: Handler<Ctx, R>, F: Fn(R) -> R2 + Send + Sync, Ctx: Copy,

Source§

impl<Ctx, R: Clone + Send + Sync> Handler<Ctx, R> for Always<R>