Skip to main content

decode_record_with_scratch

Function decode_record_with_scratch 

Source
pub fn decode_record_with_scratch(
    schema: &Schema,
    data: &[u8],
    options: &DecodeOptions,
    scratch: &mut ScratchBuffers,
) -> Result<Value>
Expand description

High-performance decode using reusable scratch buffers

This optimized version reuses memory buffers across calls to minimize allocations, providing significant performance improvements for high-throughput scenarios.

§Arguments

  • schema - The parsed copybook schema
  • data - The binary record data
  • options - Decoding options
  • scratch - Reusable scratch buffers for optimization

§Examples

use copybook_core::parse_copybook;
use copybook_codec::{decode_record_with_scratch, DecodeOptions};
use copybook_codec::memory::ScratchBuffers;
use copybook_codec::options::{Codepage, RecordFormat};

let schema = parse_copybook("01 FLD PIC X(5).").unwrap();
let options = DecodeOptions::new()
    .with_codepage(Codepage::ASCII)
    .with_format(RecordFormat::Fixed);
let mut scratch = ScratchBuffers::new();

// Decode multiple records reusing the same scratch buffers
for record_data in [b"AAAAA", b"BBBBB", b"CCCCC"] {
    let json = decode_record_with_scratch(&schema, record_data, &options, &mut scratch).unwrap();
    assert!(json["fields"]["FLD"].is_string());
}

§Errors

Returns an error if the data cannot be decoded according to the schema.