Skip to main content

ItemProcessor

Trait ItemProcessor 

Source
pub trait ItemProcessor<I, O> {
    // Required method
    fn process(&self, item: &I) -> ItemProcessorResult<O>;
}
Expand description

A trait for processing items.

This trait defines the contract for components that transform or process items in a batch processing pipeline. It takes an input item of type I and produces an output item of type O.

§Filtering

Returning Ok(None) filters the item silently: it is not passed to the writer and is counted in crate::core::step::StepExecution::filter_count. This is different from returning Err(BatchError) which counts as a processing error and may trigger fault tolerance.

§Design Pattern

This follows the Strategy Pattern, allowing different processing strategies to be interchangeable while maintaining a consistent interface.

§Type Parameters

  • I: The input item type
  • O: The output item type

§Example

use spring_batch_rs::core::item::{ItemProcessor, ItemProcessorResult};
use spring_batch_rs::error::BatchError;

struct AdultFilter;

#[derive(Clone)]
struct Person { name: String, age: u32 }

impl ItemProcessor<Person, Person> for AdultFilter {
    fn process(&self, item: &Person) -> ItemProcessorResult<Person> {
        if item.age >= 18 {
            Ok(Some(item.clone())) // keep adults
        } else {
            Ok(None) // filter out minors
        }
    }
}

Required Methods§

Source

fn process(&self, item: &I) -> ItemProcessorResult<O>

Processes an item and returns the processed result.

§Parameters
  • item: The item to process
§Returns
  • Ok(Some(processed_item)) when the item is successfully processed
  • Ok(None) when the item is intentionally filtered out
  • Err(BatchError) when an error occurs during processing

Implementations on Foreign Types§

Source§

impl<I, O, P: ItemProcessor<I, O> + ?Sized> ItemProcessor<I, O> for Box<P>

Allows any Box<P> where P: ItemProcessor<I, O> to be used wherever &dyn ItemProcessor<I, O> is expected — including boxed concrete types (Box<MyProcessor>) and boxed trait objects (Box<dyn ItemProcessor<I, O>>).

The ?Sized bound is what makes this cover trait objects: dyn Trait is unsized, so without ?Sized the impl would not apply to them.

Source§

fn process(&self, item: &I) -> ItemProcessorResult<O>

Implementors§

Source§

impl<I, M, O, P1, P2> ItemProcessor<I, O> for CompositeItemProcessor<P1, P2, M>
where P1: ItemProcessor<I, M>, P2: ItemProcessor<M, O>,

Source§

impl<T: Clone> ItemProcessor<T, T> for PassThroughProcessor<T>