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
impl BinaryDecoder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new binary decoder with default configuration
Default configuration:
validate_ordering: falsestrict_parsing: false
Sourcepub fn with_config(config: DecoderConfig) -> Self
pub fn with_config(config: DecoderConfig) -> Self
Creates a binary decoder with custom configuration
Sourcepub fn decode(&self, bytes: &[u8]) -> Result<LnmpRecord, BinaryError>
pub fn decode(&self, bytes: &[u8]) -> Result<LnmpRecord, BinaryError>
Decodes binary format to LnmpRecord
The decoder will:
- Validate the version byte (must be 0x04)
- Decode the BinaryFrame from bytes
- Validate field ordering (if validate_ordering is enabled)
- Check for trailing data (if strict_parsing is enabled)
- 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)
Sourcepub fn decode_to_text(&self, bytes: &[u8]) -> Result<String, BinaryError>
pub fn decode_to_text(&self, bytes: &[u8]) -> Result<String, BinaryError>
Decodes binary format to text format
This method:
- Decodes the binary data to an LnmpRecord
- 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();Sourcepub fn detect_version(&self, bytes: &[u8]) -> Result<u8, BinaryError>
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);Sourcepub fn supports_nested(&self, bytes: &[u8]) -> bool
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));