BinaryEncoder

Struct BinaryEncoder 

Source
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

Source

pub fn new() -> Self

Creates a new binary encoder with default configuration

Default configuration:

  • validate_canonical: false
  • sort_fields: true
Source

pub fn with_config(config: EncoderConfig) -> Self

Creates a binary encoder with custom configuration

Source

pub fn with_delta_mode(self, enable: bool) -> Self

Sets delta mode on the encoder instance in a fluent interface style.

Source

pub fn encode(&self, record: &LnmpRecord) -> Result<Vec<u8>, BinaryError>

Encodes an LnmpRecord to binary format

The encoder will:

  1. Sort fields by FID (if sort_fields is enabled)
  2. Convert the record to a BinaryFrame
  3. 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
Source

pub fn encode_text(&self, text: &str) -> Result<Vec<u8>, BinaryError>

Encodes text format directly to binary

This method:

  1. Parses the text using the v0.3 parser
  2. Converts the parsed record to binary format
  3. 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();
Source

pub fn encode_text_strict(&self, text: &str) -> Result<Vec<u8>, BinaryError>

Encodes text using strict input rules (no sanitization).

Source

pub fn encode_text_lenient(&self, text: &str) -> Result<Vec<u8>, BinaryError>

Encodes text using lenient sanitization before parsing.

Source

pub fn encode_text_strict_profile( &self, text: &str, ) -> Result<Vec<u8>, BinaryError>

Convenience: enforce strict input + strict grammar.

Source

pub fn encode_text_llm_profile( &self, text: &str, ) -> Result<Vec<u8>, BinaryError>

Convenience: lenient input + loose grammar (LLM-facing).

Source

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).

Source

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.

Trait Implementations§

Source§

impl Debug for BinaryEncoder

Source§

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

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

impl Default for BinaryEncoder

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.