styx_parse/
callback.rs

1//! Callback trait for event-based parsing.
2
3use crate::Event;
4
5/// Trait for receiving parse events.
6pub trait ParseCallback<'src> {
7    /// Called for each event. Return `false` to stop parsing early.
8    fn event(&mut self, event: Event<'src>) -> bool;
9}
10
11/// Convenience implementation: collect all events into a Vec.
12impl<'src> ParseCallback<'src> for Vec<Event<'src>> {
13    fn event(&mut self, event: Event<'src>) -> bool {
14        self.push(event);
15        true
16    }
17}
18
19/// A callback that discards all events.
20pub struct Discard;
21
22impl<'src> ParseCallback<'src> for Discard {
23    fn event(&mut self, _event: Event<'src>) -> bool {
24        true
25    }
26}
27
28/// A callback that counts events.
29pub struct Counter {
30    pub count: usize,
31}
32
33impl Counter {
34    pub fn new() -> Self {
35        Self { count: 0 }
36    }
37}
38
39impl Default for Counter {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl<'src> ParseCallback<'src> for Counter {
46    fn event(&mut self, _event: Event<'src>) -> bool {
47        self.count += 1;
48        true
49    }
50}