Skip to main content

decode_record

Function decode_record 

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

Decode a single record from binary data to JSON

§Arguments

  • schema - The parsed copybook schema
  • data - The binary record data
  • options - Decoding options

§Examples

use copybook_core::parse_copybook;
use copybook_codec::{decode_record, DecodeOptions};
use copybook_codec::options::{Codepage, RecordFormat};

let schema = parse_copybook("01 FLD PIC X(5).").unwrap();
let data = b"HELLO";
let options = DecodeOptions::new()
    .with_codepage(Codepage::ASCII)
    .with_format(RecordFormat::Fixed);
let json = decode_record(&schema, data, &options).unwrap();
assert_eq!(json["fields"]["FLD"], "HELLO");

§Errors

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