pub fn iter_records<R: Read>(
reader: R,
schema: &Schema,
options: &DecodeOptions,
) -> Result<RecordIterator<R>>Expand description
Convenience function to create a record iterator from any readable source
This function provides maximum flexibility by accepting any type that implements
the Read trait, including files, cursors, network streams, or custom readers.
§Arguments
reader- Any type implementing Read (File, Cursor,TcpStream, etc.)schema- The parsed copybook schemaoptions- Decoding options
§Errors
Returns an error if the iterator cannot be created.
§Examples
§Using with In-Memory Data (Cursor)
use copybook_codec::{iter_records, DecodeOptions, RecordFormat};
use copybook_core::parse_copybook;
use std::io::Cursor;
let copybook_text = "01 RECORD.\n 05 ID PIC 9(3).\n 05 NAME PIC X(5).";
let schema = parse_copybook(copybook_text)?;
let options = DecodeOptions::new()
.with_format(RecordFormat::Fixed);
// Create iterator from in-memory data
let data = b"001ALICE002BOB 003CAROL";
let iterator = iter_records(Cursor::new(data), &schema, &options)?;
let records: Vec<_> = iterator.collect::<Result<Vec<_>, _>>()?;
assert_eq!(records.len(), 3);§Using with File
use copybook_codec::{iter_records, DecodeOptions};
use copybook_core::parse_copybook;
use std::fs::File;
let schema = parse_copybook("01 RECORD.\n 05 DATA PIC X(10).")?;
let options = DecodeOptions::default();
let file = File::open("data.bin")?;
let iterator = iter_records(file, &schema, &options)?;
for result in iterator {
let record = result?;
println!("{}", record);
}§Using with Compressed Data
use copybook_codec::{iter_records, DecodeOptions};
use copybook_core::parse_copybook;
use std::fs::File;
use flate2::read::GzDecoder;
# fn example() -> Result<(), Box<dyn std::error::Error>> {
let schema = parse_copybook("01 RECORD.\n 05 DATA PIC X(10).")?;
let options = DecodeOptions::default();
// Read from gzipped file
let file = File::open("data.bin.gz")?;
let decoder = GzDecoder::new(file);
let iterator = iter_records(decoder, &schema, &options)?;
for result in iterator {
let record = result?;
// Process decompressed record...
}
# Ok(())
# }