pipe_io/stage.rs
1//! The [`Stage`] trait.
2//!
3//! Stages receive items one at a time and emit zero or more outputs via
4//! an [`Emit`] handle. The closure-based adapters (`map`, `filter`, ...)
5//! are exposed as methods on [`crate::PipelineBuilder`].
6
7use crate::emit::Emit;
8use crate::error::StageError;
9
10/// Transform stage in a pipeline.
11///
12/// Stages emit zero or more outputs per input via the `out` handle.
13/// 1:1 maps emit once per input, filters emit 0..1 per input,
14/// batching/windowing emit 0+ per input and additionally use
15/// [`Stage::flush`] to emit a final partial group at end-of-stream.
16pub trait Stage {
17 /// Type of item this stage consumes.
18 type Input;
19 /// Type of item this stage emits.
20 type Output;
21 /// Error type the stage can produce.
22 type Error: StageError;
23
24 /// Process a single input item.
25 ///
26 /// # Errors
27 ///
28 /// Returns `Err(Self::Error)` on stage failure. The driver wraps
29 /// this in [`crate::Error::Stage`].
30 fn process(
31 &mut self,
32 item: Self::Input,
33 out: &mut dyn Emit<Item = Self::Output>,
34 ) -> Result<(), Self::Error>;
35
36 /// Emit any buffered output at end-of-stream.
37 ///
38 /// Default impl does nothing.
39 ///
40 /// # Errors
41 ///
42 /// Returns `Err(Self::Error)` on stage failure.
43 fn flush(&mut self, _out: &mut dyn Emit<Item = Self::Output>) -> Result<(), Self::Error> {
44 Ok(())
45 }
46}