Skip to main content

decode_zoned_decimal_with_encoding

Function decode_zoned_decimal_with_encoding 

Source
pub fn decode_zoned_decimal_with_encoding(
    data: &[u8],
    digits: u16,
    scale: i16,
    signed: bool,
    codepage: Codepage,
    blank_when_zero: bool,
    preserve_encoding: bool,
) -> Result<(SmallDecimal, Option<ZonedEncodingInfo>)>
Expand description

Decode zoned decimal field with encoding detection and preservation

Returns both the decoded decimal and encoding information for preservation. When preserve_encoding is true, analyzes the input data to detect whether it uses ASCII or EBCDIC encoding, and whether mixed encodings are present within the field.

§Arguments

  • data - Raw byte data containing the zoned decimal
  • digits - Number of digit characters (field length)
  • scale - Number of decimal places (can be negative for scaling)
  • signed - Whether the field is signed (true) or unsigned (false)
  • codepage - Character encoding (ASCII or EBCDIC variant)
  • blank_when_zero - If true, all-space fields decode as zero
  • preserve_encoding - If true, detect and return encoding information

§Returns

A tuple of (SmallDecimal, Option<ZonedEncodingInfo>) containing:

  • The decoded decimal value
  • Encoding information (if preserve_encoding was true)

§Policy

Mirrors decode_zoned_decimal, defaulting to preferred-zero handling for EBCDIC unless a preserved format dictates otherwise.

§Errors

Returns an error if the zoned decimal data is invalid or contains bad sign zones. All errors include proper context information (record_index, field_path, byte_offset).

§Examples

§Basic Decoding Without Preservation

use copybook_codec::numeric::{decode_zoned_decimal_with_encoding};
use copybook_codec::options::Codepage;

let data = b"123";
let (decimal, encoding_info) = decode_zoned_decimal_with_encoding(
    data, 3, 0, false, Codepage::ASCII, false, false
)?;
assert_eq!(decimal.to_string(), "123");
assert!(encoding_info.is_none());

§Encoding Detection

use copybook_codec::numeric::{decode_zoned_decimal_with_encoding};
use copybook_codec::options::Codepage;
use copybook_codec::options::ZonedEncodingFormat;

let data = b"123";
let (decimal, encoding_info) = decode_zoned_decimal_with_encoding(
    data, 3, 0, false, Codepage::ASCII, false, true
)?;
assert_eq!(decimal.to_string(), "123");
let info = encoding_info.unwrap();
assert_eq!(info.detected_format, ZonedEncodingFormat::Ascii);
assert!(!info.has_mixed_encoding);

§See Also