spring-batch-rs 0.3.4

A toolkit for building enterprise-grade batch applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use csv::{ReaderBuilder, StringRecordsIntoIter, Terminator, Trim};
use serde::de::DeserializeOwned;
use std::{cell::RefCell, fs::File, io::Read, marker::PhantomData, path::Path};

use crate::{
    core::item::{ItemReader, ItemReaderResult},
    error::BatchError,
};

/// A CSV item reader that implements the `ItemReader` trait.
///
/// This reader deserializes CSV data into Rust structs row by row
/// using Serde's deserialization capabilities. It can process CSV
/// data from files, strings, or any source implementing the `Read` trait.
///
/// # Type Parameters
///
/// - `R`: The type of reader providing the CSV data. Must implement `Read`.
///
/// # Implementation Details
///
/// - Uses a `RefCell` to provide interior mutability for the CSV record iterator
/// - Requires `DeserializeOwned` for types that can be deserialized from CSV rows
/// - Automatically converts CSV parsing errors into Spring Batch errors
/// - Allows streaming data processing without loading the entire file into memory
///
/// # Examples
///
/// ```
/// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
/// use spring_batch_rs::core::item::ItemReader;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Record {
///     name: String,
///     value: i32,
/// }
///
/// // Create a CSV string
/// let data = "\
/// name,value
/// foo,123
/// bar,456
/// ";
///
/// // Build a reader
/// let reader = CsvItemReaderBuilder::<Record>::new()
///     .has_headers(true)
///     .from_reader(data.as_bytes());
///
/// // Read the first record
/// let record: Record = reader.read().unwrap().unwrap();
/// assert_eq!(record.name, "foo");
/// assert_eq!(record.value, 123);
///
/// // Read the second record
/// let record: Record = reader.read().unwrap().unwrap();
/// assert_eq!(record.name, "bar");
/// assert_eq!(record.value, 456);
///
/// // No more records - explicitly use Record type again
/// assert!(ItemReader::<Record>::read(&reader).unwrap().is_none());
/// ```
pub struct CsvItemReader<R: Read> {
    /// Iterator over the CSV records
    ///
    /// Uses `RefCell` to provide interior mutability so we can iterate
    /// through records while keeping the `read` method signature compatible
    /// with the `ItemReader` trait.
    records: RefCell<StringRecordsIntoIter<R>>,
}

impl<I: DeserializeOwned, R: Read> ItemReader<I> for CsvItemReader<R> {
    /// Reads the next item from the CSV file.
    ///
    /// This method reads and deserializes the next row from the CSV source.
    /// The row is converted to the specified type `T` using Serde's deserialization.
    ///
    /// # Deserialization Process
    ///
    /// 1. Gets the next record from the CSV iterator
    /// 2. If no more records, returns `Ok(None)`
    /// 3. Deserializes the record to type `T` using serde
    /// 4. Wraps errors in the Spring Batch error system
    ///
    /// # Returns
    /// - `Ok(Some(record))` if a record is successfully read
    /// - `Ok(None)` if there are no more records to read
    /// - `Err(BatchError::ItemReader(error))` if an error occurs during reading or deserialization
    ///
    /// # Examples
    ///
    /// ```
    /// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
    /// use spring_batch_rs::core::item::ItemReader;
    /// use serde::Deserialize;
    ///
    /// #[derive(Debug, Deserialize)]
    /// struct Person {
    ///     name: String,
    ///     age: u8,
    /// }
    ///
    /// let data = "name,age\nAlice,30\nBob,25";
    /// let reader = CsvItemReaderBuilder::<Person>::new()
    ///     .has_headers(true)
    ///     .from_reader(data.as_bytes());
    ///
    /// // Read all people
    /// let mut people: Vec<Person> = Vec::new();
    /// while let Some(person) = reader.read().unwrap() {
    ///     people.push(person);
    /// }
    ///
    /// assert_eq!(people.len(), 2);
    /// assert_eq!(people[0].name, "Alice");
    /// assert_eq!(people[0].age, 30);
    /// ```
    fn read(&self) -> ItemReaderResult<I> {
        // Try to get the next CSV record from the iterator
        if let Some(result) = self.records.borrow_mut().next() {
            match result {
                Ok(string_record) => {
                    // Attempt to deserialize the record to type T
                    let result: Result<I, _> = string_record.deserialize(None);

                    match result {
                        Ok(record) => Ok(Some(record)),
                        Err(error) => Err(BatchError::ItemReader(error.to_string())),
                    }
                }
                Err(error) => Err(BatchError::ItemReader(error.to_string())),
            }
        } else {
            // No more records in the CSV file
            Ok(None)
        }
    }
}

/// A builder for configuring CSV item reading.
///
/// This builder allows you to customize the CSV reading behavior,
/// including delimiter, terminator, and header handling.
///
/// # Design Pattern
///
/// This struct implements the Builder pattern, which allows for fluent, chainable
/// configuration of a `CsvItemReader` before creation. Each method returns `self`
/// to allow method chaining.
///
/// # Default Configuration
///
/// - Delimiter: comma (,)
/// - Terminator: CRLF (Windows-style line endings)
/// - Headers: disabled
/// - Trimming: All fields trimmed
///
/// # Examples
///
/// ```
/// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
/// use spring_batch_rs::core::item::ItemReader;
/// use serde::Deserialize;
/// use csv::Terminator;
///
/// #[derive(Deserialize)]
/// struct Person {
///     name: String,
///     age: u8,
/// }
///
/// // Custom CSV configuration
/// let reader = CsvItemReaderBuilder::<Person>::new()
///     .delimiter(b';')  // Use semicolon as delimiter
///     .terminator(Terminator::Any(b'\n'))  // Unix line endings
///     .has_headers(true)  // First row contains headers
///     .from_reader("name;age\nAlice;30".as_bytes());
/// ```
#[derive(Default)]
pub struct CsvItemReaderBuilder<I> {
    /// The delimiter character (default: comma ',')
    delimiter: u8,
    /// The line terminator (default: CRLF)
    terminator: Terminator,
    /// Whether the CSV has headers (default: false)
    has_headers: bool,
    _pd: PhantomData<I>,
}

impl<I> CsvItemReaderBuilder<I> {
    /// Creates a new `CsvItemReaderBuilder` with default configuration.
    ///
    /// Default settings:
    /// - Delimiter: comma (,)
    /// - Terminator: CRLF (Windows-style line endings)
    /// - Headers: disabled
    ///
    /// # Examples
    ///
    /// ```
    /// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct Record {
    ///     field: String,
    /// }
    ///
    /// let builder = CsvItemReaderBuilder::<Record>::new();
    /// ```
    pub fn new() -> Self {
        Self {
            delimiter: b',',
            terminator: Terminator::CRLF,
            has_headers: false,
            _pd: PhantomData,
        }
    }

    /// Sets the delimiter character for the CSV parsing.
    ///
    /// # Parameters
    /// - `delimiter`: The character to use as a field delimiter
    ///
    /// # Examples
    ///
    /// ```
    /// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct Record {
    ///     field: String,
    /// }
    ///
    /// // Use tab as delimiter
    /// let builder = CsvItemReaderBuilder::<Record>::new()
    ///     .delimiter(b'\t');
    ///
    /// // Use semicolon as delimiter
    /// let builder = CsvItemReaderBuilder::<Record>::new()
    ///     .delimiter(b';');
    /// ```
    pub fn delimiter(mut self, delimiter: u8) -> Self {
        self.delimiter = delimiter;
        self
    }

    /// Sets the line terminator for the CSV parsing.
    ///
    /// # Parameters
    /// - `terminator`: The line terminator to use
    ///
    /// # Terminator Options
    ///
    /// - `Terminator::CRLF`: Windows-style line endings (default)
    /// - `Terminator::Any(byte)`: Custom terminator, often `b'\n'` for Unix-style
    ///
    /// # Examples
    ///
    /// ```
    /// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
    /// use csv::Terminator;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct Record {
    ///     field: String,
    /// }
    ///
    /// // Use Unix-style line endings (LF)
    /// let builder = CsvItemReaderBuilder::<Record>::new()
    ///     .terminator(Terminator::Any(b'\n'));
    /// ```
    pub fn terminator(mut self, terminator: Terminator) -> Self {
        self.terminator = terminator;
        self
    }

    /// Sets whether the CSV file has headers.
    ///
    /// When enabled, the first row is treated as headers and is not returned
    /// as part of the data. The header names can be used to match fields in
    /// the deserialization process.
    ///
    /// # Parameters
    /// - `yes`: Whether headers are present
    ///
    /// # Deserialization Impact
    ///
    /// When enabled, column names from headers can be matched to struct field names
    /// during deserialization. This is often more robust than relying on column order.
    ///
    /// # Examples
    ///
    /// ```
    /// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct Record {
    ///     field: String,
    /// }
    ///
    /// // Enable headers (first row is column names)
    /// let builder = CsvItemReaderBuilder::<Record>::new()
    ///     .has_headers(true);
    ///
    /// // Disable headers (all rows are data)
    /// let builder = CsvItemReaderBuilder::<Record>::new()
    ///     .has_headers(false);
    /// ```
    pub fn has_headers(mut self, yes: bool) -> Self {
        self.has_headers = yes;
        self
    }

    /// Creates a `CsvItemReader` from a reader.
    ///
    /// This allows reading CSV data from any source that implements the `Read` trait,
    /// such as files, strings, or network connections.
    ///
    /// # Parameters
    /// - `rdr`: The reader containing CSV data
    ///
    /// # Configuration Applied
    ///
    /// The following configurations are applied:
    /// - Trims all whitespace from fields
    /// - Uses specified delimiter (default: comma)
    /// - Uses specified terminator (default: CRLF)
    /// - Handles headers according to configuration
    /// - Strict parsing (not flexible) to identify formatting issues
    ///
    /// # Examples
    ///
    /// ```
    /// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
    /// use spring_batch_rs::core::item::ItemReader;
    /// use serde::Deserialize;
    /// use std::io::Cursor;
    ///
    /// #[derive(Deserialize)]
    /// struct Record {
    ///     id: u32,
    ///     name: String,
    /// }
    ///
    /// // Read from a string
    /// let data = "id,name\n1,Alice\n2,Bob";
    /// let reader = CsvItemReaderBuilder::<Record>::new()
    ///     .has_headers(true)
    ///     .from_reader(data.as_bytes());
    ///
    /// // Or read from a Cursor
    /// let cursor = Cursor::new("id,name\n1,Alice\n2,Bob");
    /// let reader = CsvItemReaderBuilder::<Record>::new()
    ///     .has_headers(true)
    ///     .from_reader(cursor);
    /// ```
    pub fn from_reader<R: Read>(self, rdr: R) -> CsvItemReader<R> {
        // Configure the CSV reader with builder options
        let rdr = ReaderBuilder::new()
            .trim(Trim::All) // Trim whitespace from all fields
            .delimiter(self.delimiter)
            .terminator(self.terminator)
            .has_headers(self.has_headers)
            .flexible(false) // Use strict parsing to catch formatting errors
            .from_reader(rdr);

        // Convert to a record iterator
        let records = rdr.into_records();

        CsvItemReader {
            records: RefCell::new(records),
        }
    }

    /// Creates a `CsvItemReader` from a file path.
    ///
    /// # Parameters
    /// - `path`: The path to the CSV file
    ///
    /// # Returns
    /// A new `CsvItemReader` configured to read from the specified file
    ///
    /// # Panics
    /// Panics if the file cannot be opened
    ///
    /// # Error Handling
    ///
    /// This method panics immediately if the file cannot be opened, which is appropriate
    /// for initialization failures. Subsequent reading errors are returned as `Result` values
    /// from the `read` method.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use spring_batch_rs::item::csv::csv_reader::CsvItemReaderBuilder;
    /// use spring_batch_rs::core::item::ItemReader;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct Record {
    ///     id: u32,
    ///     name: String,
    /// }
    ///
    /// // Read from a file
    /// let reader = CsvItemReaderBuilder::<Record>::new()
    ///     .has_headers(true)
    ///     .from_path("data.csv");
    ///
    /// // Process records
    /// let mut records: Vec<Record> = Vec::new();
    /// while let Some(record) = ItemReader::<Record>::read(&reader).unwrap() {
    ///     println!("ID: {}, Name: {}", record.id, record.name);
    ///     records.push(record);
    /// }
    /// ```
    pub fn from_path<R: AsRef<Path>>(self, path: R) -> CsvItemReader<File> {
        // Configure the CSV reader with builder options
        let rdr = ReaderBuilder::new()
            .trim(Trim::All) // Trim whitespace from all fields
            .delimiter(self.delimiter)
            .terminator(self.terminator)
            .has_headers(self.has_headers)
            .flexible(false) // Use strict parsing to catch formatting errors
            .from_path(path);

        // Unwrap here is appropriate since file opening is an initialization step
        // If it fails, we want to fail fast rather than returning an error
        let records = rdr.unwrap().into_records();

        CsvItemReader {
            records: RefCell::new(records),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::item::ItemReader;
    use csv::StringRecord;
    use serde::Deserialize;
    use std::error::Error;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[derive(Debug, Deserialize, PartialEq)]
    struct City {
        city: String,
        country: String,
        pop: u32,
    }

    /// Tests basic CSV parsing functionality
    ///
    /// This test verifies that the CsvItemReader can correctly parse
    /// CSV data with headers and multiple records.
    #[test]
    fn should_parse_string_records_with_headers() -> Result<(), Box<dyn Error>> {
        let data = "city,country,pop
        Boston,United States,4628910
        Concord,United States,42695";

        let reader = CsvItemReaderBuilder::<City>::new()
            .has_headers(true)
            .delimiter(b',')
            .from_reader(data.as_bytes());

        let records = reader
            .records
            .into_inner()
            .collect::<Result<Vec<StringRecord>, csv::Error>>()?;

        assert_eq!(
            records,
            vec![
                vec!["Boston", "United States", "4628910"],
                vec!["Concord", "United States", "42695"],
            ]
        );

        Ok(())
    }

    /// Test deserializing typed records using ItemReader trait implementation
    #[test]
    fn test_deserialize_typed_records() -> Result<(), Box<dyn Error>> {
        let data = "city,country,pop
        Boston,United States,4628910
        Concord,United States,42695";

        let reader = CsvItemReaderBuilder::<City>::new()
            .has_headers(true)
            .from_reader(data.as_bytes());

        // Read first record
        let record1: City = reader.read()?.unwrap();
        assert_eq!(
            record1,
            City {
                city: "Boston".to_string(),
                country: "United States".to_string(),
                pop: 4628910,
            }
        );

        // Read second record
        let record2: City = reader.read()?.unwrap();
        assert_eq!(
            record2,
            City {
                city: "Concord".to_string(),
                country: "United States".to_string(),
                pop: 42695,
            }
        );

        // No more records
        assert!(ItemReader::<City>::read(&reader)?.is_none());

        Ok(())
    }

    /// Test reading from a file
    #[test]
    fn test_read_from_file() -> Result<(), Box<dyn Error>> {
        // Create a temporary file
        let mut temp_file = NamedTempFile::new()?;
        let csv_content = "city,country,pop\nParis,France,2161000\nLyon,France,513275";
        temp_file.write_all(csv_content.as_bytes())?;

        // Create reader from file path
        let reader = CsvItemReaderBuilder::<City>::new()
            .has_headers(true)
            .from_path(temp_file.path());

        // Read records
        let city1: City = reader.read()?.unwrap();
        let city2: City = reader.read()?.unwrap();

        assert_eq!(city1.city, "Paris");
        assert_eq!(city2.city, "Lyon");
        assert_eq!(city1.pop, 2161000);
        assert_eq!(city2.pop, 513275);

        Ok(())
    }

    /// Test different CSV formats (delimiters, terminators)
    #[test]
    fn test_different_csv_formats() -> Result<(), Box<dyn Error>> {
        // Test with semicolon delimiter and LF terminator
        let data = "city;country;pop\nBerlin;Germany;3645000\nMunich;Germany;1472000";

        let reader = CsvItemReaderBuilder::<City>::new()
            .has_headers(true)
            .delimiter(b';')
            .terminator(Terminator::Any(b'\n'))
            .from_reader(data.as_bytes());

        let city1: City = reader.read()?.unwrap();
        let city2: City = reader.read()?.unwrap();

        assert_eq!(city1.city, "Berlin");
        assert_eq!(city2.city, "Munich");
        assert_eq!(city1.country, "Germany");

        Ok(())
    }

    /// Test reading without headers
    #[test]
    fn test_no_headers() -> Result<(), Box<dyn Error>> {
        #[derive(Debug, Deserialize, PartialEq)]
        struct Record {
            field1: String,
            field2: String,
            field3: u32,
        }

        let data = "Tokyo,Japan,13960000\nOsaka,Japan,2691000";

        let reader = CsvItemReaderBuilder::<Record>::new()
            .has_headers(false)
            .from_reader(data.as_bytes());

        let record1: Record = ItemReader::<Record>::read(&reader)?.unwrap();
        let record2: Record = ItemReader::<Record>::read(&reader)?.unwrap();

        assert_eq!(
            record1,
            Record {
                field1: "Tokyo".to_string(),
                field2: "Japan".to_string(),
                field3: 13960000,
            }
        );

        assert_eq!(
            record2,
            Record {
                field1: "Osaka".to_string(),
                field2: "Japan".to_string(),
                field3: 2691000,
            }
        );

        Ok(())
    }

    /// Test error handling for malformed CSV
    #[test]
    fn test_deserialization_error() {
        // Malformed data - "not_a_number" isn't a valid u32
        let data = "city,country,pop\nMilan,Italy,not_a_number";

        let reader = CsvItemReaderBuilder::<City>::new()
            .has_headers(true)
            .from_reader(data.as_bytes());

        // Should fail to deserialize because "not_a_number" isn't a valid u32
        let result = ItemReader::<City>::read(&reader);
        assert!(result.is_err());
    }

    /// Test reading an empty file
    #[test]
    fn test_empty_file() -> Result<(), Box<dyn Error>> {
        let data = "";

        let reader = CsvItemReaderBuilder::<City>::new()
            .has_headers(false)
            .from_reader(data.as_bytes());

        assert!(ItemReader::<City>::read(&reader)?.is_none());

        Ok(())
    }

    /// Test reading only headers with no data
    #[test]
    fn test_headers_only() -> Result<(), Box<dyn Error>> {
        let data = "city,country,pop";

        let reader = CsvItemReaderBuilder::<City>::new()
            .has_headers(true)
            .from_reader(data.as_bytes());

        assert!(ItemReader::<City>::read(&reader)?.is_none());

        Ok(())
    }
}