spring_batch_rs/core/
item.rs1use crate::error::BatchError;
2use serde::{de::DeserializeOwned, Serialize};
3
4pub type ItemReaderResult<R> = Result<Option<R>, BatchError>;
6
7pub type ItemProcessorResult<W> = Result<W, BatchError>;
9
10pub type ItemWriterResult = Result<(), BatchError>;
12
13pub trait ItemReader<R> {
15 fn read(&self) -> ItemReaderResult<R>;
17}
18
19pub trait ItemProcessor<R, W> {
21 fn process(&self, item: &R) -> ItemProcessorResult<W>;
23}
24
25pub trait ItemWriter<W> {
27 fn write(&self, items: &[W]) -> ItemWriterResult;
29
30 fn flush(&self) -> ItemWriterResult {
32 Ok(())
33 }
34
35 fn open(&self) -> ItemWriterResult {
37 Ok(())
38 }
39
40 fn close(&self) -> ItemWriterResult {
42 Ok(())
43 }
44}
45
46#[derive(Default)]
48pub struct DefaultProcessor;
49
50impl<R: Serialize, W: DeserializeOwned> ItemProcessor<R, W> for DefaultProcessor {
51 fn process(&self, item: &R) -> ItemProcessorResult<W> {
53 let serialised = serde_json::to_string(&item).unwrap();
56 let item = serde_json::from_str(&serialised).unwrap();
57 Ok(item)
58 }
59}