pub struct BinaryEncoder { /* private fields */ }Expand description
Binary encoder for LNMP v0.4
Converts LNMP records from text format (v0.3) to binary format (v0.4). The encoder ensures canonical form by sorting fields by FID before encoding.
§Examples
use lnmp_codec::binary::BinaryEncoder;
use lnmp_core::{LnmpRecord, LnmpField, LnmpValue};
let mut record = LnmpRecord::new();
record.add_field(LnmpField {
fid: 7,
value: LnmpValue::Bool(true),
});
record.add_field(LnmpField {
fid: 12,
value: LnmpValue::Int(14532),
});
let encoder = BinaryEncoder::new();
let binary = encoder.encode(&record).unwrap();Implementations§
Source§impl BinaryEncoder
impl BinaryEncoder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new binary encoder with default configuration
Default configuration:
validate_canonical: falsesort_fields: true
Sourcepub fn with_config(config: EncoderConfig) -> Self
pub fn with_config(config: EncoderConfig) -> Self
Creates a binary encoder with custom configuration
Sourcepub fn with_delta_mode(self, enable: bool) -> Self
pub fn with_delta_mode(self, enable: bool) -> Self
Sets delta mode on the encoder instance in a fluent interface style.
Sourcepub fn encode(&self, record: &LnmpRecord) -> Result<Vec<u8>, BinaryError>
pub fn encode(&self, record: &LnmpRecord) -> Result<Vec<u8>, BinaryError>
Encodes an LnmpRecord to binary format
The encoder will:
- Sort fields by FID (if sort_fields is enabled)
- Convert the record to a BinaryFrame
- Encode the frame to bytes
§Arguments
record- The LNMP record to encode
§Returns
A vector of bytes representing the binary-encoded record
§Errors
Returns BinaryError if:
- The record contains nested structures when v0.4 compatibility is enabled
- Field conversion fails
Sourcepub fn encode_text(&self, text: &str) -> Result<Vec<u8>, BinaryError>
pub fn encode_text(&self, text: &str) -> Result<Vec<u8>, BinaryError>
Encodes text format directly to binary
This method:
- Parses the text using the v0.3 parser
- Converts the parsed record to binary format
- Ensures fields are sorted by FID
§Arguments
text- LNMP text format string (v0.3)
§Returns
A vector of bytes representing the binary-encoded record
§Errors
Returns BinaryError if:
- Text parsing fails
- The record contains nested structures (not supported in v0.4)
- Field conversion fails
§Examples
use lnmp_codec::binary::BinaryEncoder;
let text = "F7=1;F12=14532;F23=[\"admin\",\"dev\"]";
let encoder = BinaryEncoder::new();
let binary = encoder.encode_text(text).unwrap();Sourcepub fn encode_text_strict(&self, text: &str) -> Result<Vec<u8>, BinaryError>
pub fn encode_text_strict(&self, text: &str) -> Result<Vec<u8>, BinaryError>
Encodes text using strict input rules (no sanitization).
Sourcepub fn encode_text_lenient(&self, text: &str) -> Result<Vec<u8>, BinaryError>
pub fn encode_text_lenient(&self, text: &str) -> Result<Vec<u8>, BinaryError>
Encodes text using lenient sanitization before parsing.
Sourcepub fn encode_text_strict_profile(
&self,
text: &str,
) -> Result<Vec<u8>, BinaryError>
pub fn encode_text_strict_profile( &self, text: &str, ) -> Result<Vec<u8>, BinaryError>
Convenience: enforce strict input + strict grammar.
Sourcepub fn encode_text_llm_profile(
&self,
text: &str,
) -> Result<Vec<u8>, BinaryError>
pub fn encode_text_llm_profile( &self, text: &str, ) -> Result<Vec<u8>, BinaryError>
Convenience: lenient input + loose grammar (LLM-facing).
Sourcepub fn encode_text_with_profile(
&self,
text: &str,
text_mode: TextInputMode,
parsing_mode: ParsingMode,
) -> Result<Vec<u8>, BinaryError>
pub fn encode_text_with_profile( &self, text: &str, text_mode: TextInputMode, parsing_mode: ParsingMode, ) -> Result<Vec<u8>, BinaryError>
Encodes text with both text input mode and parsing mode specified.
This is useful to enforce strict grammar (ParsingMode::Strict) together with strict input, or to provide a fully lenient LLM-facing path (ParsingMode::Loose + Lenient input).
Sourcepub fn encode_delta_from(
&self,
base: &LnmpRecord,
updated: &LnmpRecord,
delta_config: Option<DeltaConfig>,
) -> Result<Vec<u8>, BinaryError>
pub fn encode_delta_from( &self, base: &LnmpRecord, updated: &LnmpRecord, delta_config: Option<DeltaConfig>, ) -> Result<Vec<u8>, BinaryError>
Encodes delta packet (binary) from a base record and updated record.
If the encoder config has delta_mode=true or a non-None delta_config is provided,
this method computes a delta using DeltaEncoder and returns the encoded delta bytes.
Otherwise it returns an error.