Skip to main content

parse_record_from_bytes

Function parse_record_from_bytes 

Source
pub fn parse_record_from_bytes(
    record_bytes: Vec<u8>,
    recovery_mode: RecoveryMode,
    validation_level: ValidationLevel,
) -> Result<Option<Record>>
Expand description

Parse one complete MARC record (24-byte leader + body) from owned in-memory bytes, with no reader I/O and no per-record byte copies: the buffer is moved into a shared handle that both the parser and the error-diagnostics context borrow from.

Each call parses one record with fresh per-record state, like constructing a MarcReader over the bytes and reading once — minus the copies. Use MarcReader for streams; use this for bytes you already hold (one record per call).

Non-fatal diagnostics accumulated in Lenient/Permissive modes are attached to the returned record’s errors, matching MarcReader::read_record.

An empty buffer returns Ok(None). One deliberate difference from the streaming reader: a non-empty buffer too short to hold the 24-byte leader is a TruncatedRecord error here (in Strict mode), whereas MarcReader treats a partial leader at end of input as end of stream and returns Ok(None).

§Errors

Returns an error if the bytes are malformed (in Strict mode, the first structural defect; in recovery modes, only unrecoverable ones).

§Examples

use mrrc::{parse_record_from_bytes, RecoveryMode, ValidationLevel};
let bytes: Vec<u8> = std::fs::read("one_record.mrc")?;
let record = parse_record_from_bytes(
    bytes,
    RecoveryMode::Strict,
    ValidationLevel::Structural,
)?;