Skip to main content

subtr_actor/collector/
mod.rs

1pub mod callback;
2pub mod decorator;
3pub mod ndarray;
4pub mod replay_data;
5pub mod stats_timeline;
6
7pub use self::ndarray::*;
8pub use callback::*;
9pub use decorator::*;
10pub use replay_data::*;
11pub use stats_timeline::*;
12
13use crate::*;
14use boxcars;
15
16/// Enum used to control the progress of time during replay processing.
17pub enum TimeAdvance {
18    /// Move forward in time by a specified amount.
19    Time(f32),
20    /// Advance to the next frame.
21    NextFrame,
22}
23
24/// Trait for types that collect data from a replay.
25///
26/// A `Collector` processes frames from a replay, potentially using a
27/// [`ReplayProcessor`] for access to additional replay data and context. It
28/// determines the pace of replay progression via the [`TimeAdvance`] return
29/// value.
30pub trait Collector {
31    /// Process a single frame from a replay.
32    ///
33    /// # Arguments
34    ///
35    /// * `processor` - The [`ReplayProcessor`] providing context for the replay.
36    /// * `frame` - The [`boxcars::Frame`] to process.
37    /// * `frame_number` - The number of the current frame.
38    /// * `current_time` - The current target time in the replay.
39    ///
40    /// # Returns
41    ///
42    /// Returns a [`TimeAdvance`] enum which determines the next step in replay
43    /// progression.
44    fn process_frame(
45        &mut self,
46        processor: &ReplayProcessor,
47        frame: &boxcars::Frame,
48        frame_number: usize,
49        current_time: f32,
50    ) -> SubtrActorResult<TimeAdvance>;
51
52    /// Process an entire replay.
53    ///
54    /// # Arguments
55    ///
56    /// * `replay` - The [`boxcars::Replay`] to process.
57    ///
58    /// # Returns
59    ///
60    /// Returns the [`Collector`] itself, potentially modified by the processing
61    /// of the replay.
62    fn process_replay(mut self, replay: &boxcars::Replay) -> SubtrActorResult<Self>
63    where
64        Self: Sized,
65    {
66        ReplayProcessor::new(replay)?.process(&mut self)?;
67        Ok(self)
68    }
69
70    /// Finalize replay-derived state after the last frame has been processed.
71    ///
72    /// Collectors that aggregate state across frame boundaries can override
73    /// this to flush any in-progress segment once replay traversal is complete.
74    fn finish_replay(&mut self, _processor: &ReplayProcessor) -> SubtrActorResult<()> {
75        Ok(())
76    }
77}
78
79impl<G> Collector for G
80where
81    G: FnMut(&ReplayProcessor, &boxcars::Frame, usize, f32) -> SubtrActorResult<TimeAdvance>,
82{
83    fn process_frame(
84        &mut self,
85        processor: &ReplayProcessor,
86        frame: &boxcars::Frame,
87        frame_number: usize,
88        current_time: f32,
89    ) -> SubtrActorResult<TimeAdvance> {
90        self(processor, frame, frame_number, current_time)
91    }
92}