pub trait FormatWriter: Debug {
// Required methods
fn write_record(&mut self, record: &Record) -> Result<()>;
fn finish(&mut self) -> Result<()>;
// Provided methods
fn write_batch(&mut self, records: &[Record]) -> Result<()> { ... }
fn records_written(&self) -> Option<usize> { ... }
}Expand description
Trait for writers that can serialize MARC records to a format.
This trait provides a uniform interface for writing MARC records to different serialization formats. All format-specific writers implement this trait.
§Usage Pattern
Writers follow a standard pattern:
- Create the writer with format-specific configuration
- Write records using
write_recordorwrite_batch - Call
finishto flush and finalize output
§Important: Always Call finish
The finish method MUST be called to ensure all data is written.
Some formats buffer data for efficiency and only write on finish.
Dropping a writer without calling finish may result in data loss.
§Example
use mrrc::formats::FormatWriter;
fn write_records<W: FormatWriter>(writer: &mut W, records: &[Record]) -> mrrc::Result<()> {
writer.write_batch(records)?;
writer.finish()?; // Always call finish!
Ok(())
}Required Methods§
Sourcefn write_record(&mut self, record: &Record) -> Result<()>
fn write_record(&mut self, record: &Record) -> Result<()>
Write a single record to the output.
§Fidelity Requirements
The written record MUST preserve:
- Exact field ordering (fields written in same sequence as input)
- Exact subfield ordering within each field
- All indicator values (including blank indicators)
- All whitespace in field/subfield values
- Leader data exactly as provided
§Errors
Returns an error if the record cannot be serialized (e.g., invalid structure) or if writing to the underlying output fails.
Sourcefn finish(&mut self) -> Result<()>
fn finish(&mut self) -> Result<()>
Finish writing and flush any buffered data.
This method MUST be called to ensure all data is written to the output.
After calling finish, the writer should not be used for further writes.
§Errors
Returns an error if flushing fails or if the underlying output cannot be finalized (e.g., network error, disk full).
Provided Methods§
Sourcefn write_batch(&mut self, records: &[Record]) -> Result<()>
fn write_batch(&mut self, records: &[Record]) -> Result<()>
Write multiple records to the output.
This method may be more efficient than calling write_record repeatedly
for formats that benefit from batch operations.
The default implementation calls write_record for each record.
§Errors
Returns an error if any record cannot be written.
Sourcefn records_written(&self) -> Option<usize>
fn records_written(&self) -> Option<usize>
Returns the number of records written so far.
This is useful for progress reporting and debugging.
The default implementation returns None if tracking is not supported.