BinaryDecoder

Struct BinaryDecoder 

Source
pub struct BinaryDecoder { /* private fields */ }
Expand description

Binary decoder for LNMP v0.4

Converts LNMP records from binary format (v0.4) to text format (v0.3). The decoder validates the binary structure and can optionally enforce canonical form compliance.

§Examples

use lnmp_codec::binary::{BinaryDecoder, BinaryEncoder};
use lnmp_core::{LnmpRecord, LnmpField, LnmpValue};

// Create and encode a record
let mut record = LnmpRecord::new();
record.add_field(LnmpField {
    fid: 7,
    value: LnmpValue::Bool(true),
});

let encoder = BinaryEncoder::new();
let binary = encoder.encode(&record).unwrap();

// Decode back to record
let decoder = BinaryDecoder::new();
let decoded_record = decoder.decode(&binary).unwrap();

Implementations§

Source§

impl BinaryDecoder

Source

pub fn new() -> Self

Creates a new binary decoder with default configuration

Default configuration:

  • validate_ordering: false
  • strict_parsing: false
Source

pub fn with_config(config: DecoderConfig) -> Self

Creates a binary decoder with custom configuration

Source

pub fn decode(&self, bytes: &[u8]) -> Result<LnmpRecord, BinaryError>

Decodes binary format to LnmpRecord

The decoder will:

  1. Validate the version byte (must be 0x04)
  2. Decode the BinaryFrame from bytes
  3. Validate field ordering (if validate_ordering is enabled)
  4. Check for trailing data (if strict_parsing is enabled)
  5. Convert the frame to an LnmpRecord
§Arguments
  • bytes - Binary-encoded LNMP data
§Returns

An LnmpRecord representing the decoded data

§Errors

Returns BinaryError if:

  • Version byte is not 0x04 (UnsupportedVersion)
  • Binary data is malformed (UnexpectedEof, InvalidVarInt, etc.)
  • Field ordering is invalid (CanonicalViolation, if validate_ordering is enabled)
  • Trailing data is present (TrailingData, if strict_parsing is enabled)
Source

pub fn decode_to_text(&self, bytes: &[u8]) -> Result<String, BinaryError>

Decodes binary format to text format

This method:

  1. Decodes the binary data to an LnmpRecord
  2. Encodes the record to canonical text format using the v0.3 encoder
§Arguments
  • bytes - Binary-encoded LNMP data
§Returns

A string containing the canonical text representation

§Errors

Returns BinaryError if decoding fails

§Examples
use lnmp_codec::binary::{BinaryDecoder, BinaryEncoder};

let text = "F7=1;F12=14532";
let encoder = BinaryEncoder::new();
let binary = encoder.encode_text(text).unwrap();

let decoder = BinaryDecoder::new();
let decoded_text = decoder.decode_to_text(&binary).unwrap();
Source

pub fn detect_version(&self, bytes: &[u8]) -> Result<u8, BinaryError>

Detects the binary format version from the first byte

This method examines the version byte to determine which version of the LNMP binary protocol is being used. This is useful for backward compatibility and version-specific handling.

§Arguments
  • bytes - Binary data to inspect
§Returns

The version byte (0x04 for v0.4, 0x05 for v0.5, etc.)

§Errors

Returns BinaryError::UnexpectedEof if the data is too short to contain a version byte

§Examples
use lnmp_codec::binary::BinaryDecoder;

let decoder = BinaryDecoder::new();
let v04_data = vec![0x04, 0x00, 0x00]; // v0.4 binary
let version = decoder.detect_version(&v04_data).unwrap();
assert_eq!(version, 0x04);
Source

pub fn supports_nested(&self, bytes: &[u8]) -> bool

Checks if the binary data contains nested structures (v0.5+ feature)

This method scans the binary data to determine if it uses v0.5+ type tags (NestedRecord 0x06, NestedArray 0x07, or reserved types 0x08-0x0F). This is useful for determining compatibility with v0.4 decoders.

§Arguments
  • bytes - Binary data to inspect
§Returns

true if the data contains v0.5+ type tags, false otherwise

§Examples
use lnmp_codec::binary::BinaryDecoder;

let decoder = BinaryDecoder::new();
let v04_data = vec![0x04, 0x00, 0x01, 0x07, 0x00, 0x03, 0x01]; // v0.4 with Bool
assert!(!decoder.supports_nested(&v04_data));

Trait Implementations§

Source§

impl Debug for BinaryDecoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BinaryDecoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.