PipelineStage

Trait PipelineStage 

Source
pub trait PipelineStage:
    Send
    + Sync
    + Debug {
    type Config: Send + Sync + Debug + ConfigValidator + Default;
    type Result: Send + Sync + Debug + 'static;

    // Required methods
    fn stage_id(&self) -> StageId;
    fn stage_name(&self) -> &str;
    fn process(
        &self,
        context: &mut StageContext,
        data: StageData,
        config: Option<&Self::Config>,
    ) -> Result<StageResult<Self::Result>, OCRError>;

    // Provided methods
    fn dependencies(&self) -> Vec<StageDependency> { ... }
    fn is_enabled(
        &self,
        context: &StageContext,
        config: Option<&Self::Config>,
    ) -> bool { ... }
    fn validate_config(&self, config: &Self::Config) -> Result<(), OCRError> { ... }
    fn default_config(&self) -> Self::Config { ... }
}
Expand description

Trait for extensible pipeline stages.

This trait defines the interface that all pipeline stages must implement to participate in the extensible pipeline system.

§Default Contract

All pipeline stages must implement a default contract to ensure consistency, reliability, and maintainability. This contract eliminates silent failures and ensures all stages provide meaningful defaults and proper validation.

§Contract Requirements:

  1. Mandatory Default Configuration: default_config() must return a valid Self::Config
  2. ConfigValidator Implementation: All config types must implement ConfigValidator
  3. Validation Integration: Stages must use ConfigValidator in validate_config()
  4. Required Traits: Config types must implement ConfigValidator + Default + Clone + Debug + Send + Sync

§Example Implementation:

use oar_ocr::pipeline::stages::{PipelineStage, StageId, StageContext, StageData, StageResult};
use oar_ocr::core::config::{ConfigValidator, ConfigError};
use oar_ocr::core::OCRError;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MyStageConfig {
    pub threshold: f32,
    pub enabled: bool,
}

impl ConfigValidator for MyStageConfig {
    fn validate(&self) -> Result<(), ConfigError> {
        if !(0.0..=1.0).contains(&self.threshold) {
            return Err(ConfigError::InvalidConfig {
                message: "threshold must be between 0.0 and 1.0".to_string(),
            });
        }
        Ok(())
    }

    fn get_defaults() -> Self {
        Self { threshold: 0.5, enabled: true }
    }
}

#[derive(Debug)]
pub struct MyStage;

impl PipelineStage for MyStage {
    type Config = MyStageConfig;
    type Result = String;

    fn stage_id(&self) -> StageId { StageId::new("my_stage") }
    fn stage_name(&self) -> &str { "My Stage" }

    fn validate_config(&self, config: &Self::Config) -> Result<(), OCRError> {
        config.validate().map_err(|e| OCRError::ConfigError {
            message: format!("MyStageConfig validation failed: {}", e),
        })
    }

    fn default_config(&self) -> Self::Config {
        MyStageConfig::get_defaults()
    }

    fn process(
        &self,
        _context: &mut StageContext,
        _data: StageData,
        config: Option<&Self::Config>,
    ) -> Result<StageResult<Self::Result>, OCRError> {
        let config = config.cloned().unwrap_or_else(|| self.default_config());
        self.validate_config(&config)?;
        // Process with validated configuration...
    }
}

See DEFAULT_CONTRACT.md for detailed documentation.

Required Associated Types§

Source

type Config: Send + Sync + Debug + ConfigValidator + Default

The configuration type for this stage.

Must implement ConfigValidator to ensure validation is never skipped.

Source

type Result: Send + Sync + Debug + 'static

The result type produced by this stage.

Required Methods§

Source

fn stage_id(&self) -> StageId

Get the unique identifier for this stage.

Source

fn stage_name(&self) -> &str

Get the human-readable name of this stage.

Source

fn process( &self, context: &mut StageContext, data: StageData, config: Option<&Self::Config>, ) -> Result<StageResult<Self::Result>, OCRError>

Process the stage with the given context and configuration.

§Arguments
  • context - The stage execution context
  • data - The input data for this stage
  • config - Optional stage-specific configuration
§Returns

A StageResult containing the processed data and metrics

Provided Methods§

Source

fn dependencies(&self) -> Vec<StageDependency>

Get the dependencies for this stage.

Source

fn is_enabled( &self, context: &StageContext, config: Option<&Self::Config>, ) -> bool

Check if this stage is enabled based on the context and configuration.

Source

fn validate_config(&self, config: &Self::Config) -> Result<(), OCRError>

Validate the stage configuration.

Source

fn default_config(&self) -> Self::Config

Get default configuration for this stage.

This method must return a valid configuration. The default contract ensures all stages provide meaningful defaults.

Implementors§