Struct DleEncoder

Source
pub struct DleEncoder {
    pub escape_stx_etx: bool,
    pub escape_cr: bool,
    pub add_stx_etx: bool,
}
Expand description

This struct is used to create a DleEncoder instance. It can also be used to configure the encoder

Fields§

§escape_stx_etx: bool

Configure whether the encoder uses the escaped or non-escaped mode

§escape_cr: bool

It is possible to escape CR characters as well in the escaped mode

§add_stx_etx: bool

Configure the encoder to not add STX and ETX characters at the start and end when encoding

Implementations§

Source§

impl DleEncoder

Source

pub fn encode( &self, source_stream: &[u8], dest_stream: &mut [u8], ) -> Result<usize, DleError>

This method encodes a given byte stream with ASCII based DLE encoding. It returns the number of encoded bytes or a DLE error code.

§Arguments
  • source_stream - The stream to encode
  • dest_stream - Encoded stream will be written here
§Examples
use dle_encoder::DleEncoder;

let dle_encoder = DleEncoder::default();
let mut encoding_buffer: [u8; 16] = [0; 16];
let example_array: [u8; 3] = [0, 0x02, 0x10];

let encode_result = dle_encoder.encode(
    &example_array, &mut encoding_buffer
);
assert!(encode_result.is_ok());
let encoded_len = encode_result.unwrap();
assert_eq!(encoded_len, 7);

println!("Source buffer: {:?}", example_array);
println!("Encoded stream: {:?}", &encoding_buffer[ .. encoded_len])
Source

pub fn encode_escaped( &self, source_stream: &[u8], dest_stream: &mut [u8], ) -> Result<usize, DleError>

This method encodes a given byte stream with ASCII based DLE encoding. It explicitely does so in the escaped mode, which is the default mode.

§Arguments
  • source_stream - The stream to encode
  • dest_stream - Encoded stream will be written here
Source

pub fn encode_non_escaped( &self, source_stream: &[u8], dest_stream: &mut [u8], ) -> Result<usize, DleError>

This method encodes a given byte stream with ASCII based DLE encoding. It explicitely does so in the non-escaped mode.

§Arguments
  • source_stream - The stream to encode
  • dest_stream - Encoded stream will be written here
§Example
use dle_encoder::DleEncoder;

let dle_encoder = DleEncoder::default();
let mut encoding_buffer: [u8; 16] = [0; 16];
let example_array: [u8; 3] = [0, 0x02, 0x10];

let encode_result = dle_encoder.encode_non_escaped(
    &example_array, &mut encoding_buffer
);
assert!(encode_result.is_ok());
let encoded_len = encode_result.unwrap();
assert_eq!(encoded_len, 8);

println!("Source buffer: {:?}", example_array);
println!("Encoded stream: {:?}", &encoding_buffer[ .. encoded_len])
Source

pub fn decode( &self, source_stream: &[u8], dest_stream: &mut [u8], read_len: &mut usize, ) -> Result<usize, DleError>

This method decodes a given byte stream which was encoded with a ASCII DLE encoder. It explicitely does so in the escaped mode, which is the default mode. It returns the length of the decoded buffer or an error code if there is a decoder failure or the destination stream is too short.

§Arguments
  • source_stream - The stream to decode
  • dest_stream - Decoded stream will be written here
  • read_len - The number of read bytes in the source stream will be assigned to this variable
§Examples
use dle_encoder::DleEncoder;

let dle_encoder = DleEncoder::default();
let mut decoding_buffer: [u8; 16] = [0; 16];
let encoded_array: [u8; 4] = [0x02, 0x10, 0x02 + 0x40, 0x03];
let mut read_len = 0;
let decode_result = dle_encoder.decode(
    &encoded_array, &mut decoding_buffer, &mut read_len
);
assert!(decode_result.is_ok());
let decoded_len = decode_result.unwrap();
assert_eq!(decoded_len, 1);

println!("Source buffer: {:?}", encoded_array);
println!("Encoded stream: {:?}", &decoding_buffer[ .. decoded_len])
Source

pub fn decode_escaped( &self, source_stream: &[u8], dest_stream: &mut [u8], read_len: &mut usize, ) -> Result<usize, DleError>

This method decodes a given byte stream which was encoded with a ASCII DLE encoder. It explicitely does so in the escaped mode, which is the default mode. It returns the length of the decoded buffer or an error code if there is a decoder failure or the destination stream is too short.

§Arguments
  • source_stream - The stream to decode
  • dest_stream - Decoded stream will be written here
  • read_len - The number of read bytes in the source stream will be assigned to this variable
Source

pub fn decode_non_escaped( &self, source_stream: &[u8], dest_stream: &mut [u8], read_len: &mut usize, ) -> Result<usize, DleError>

This method decodes a given byte stream which was encoded with a ASCII DLE encoder. It explicitely does so in the non-escaped mode. It returns the length of the decoded buffer or an error code if there is a decoder failure or the destination stream is too short.

§Arguments
  • source_stream - The stream to decode
  • dest_stream - Decoded stream will be written here
  • read_len - The number of read bytes in the source stream will be assigned to this variable
§Examples
use dle_encoder::DleEncoder;

let dle_encoder = DleEncoder::default();
let mut decoding_buffer: [u8; 16] = [0; 16];
let encoded_array: [u8; 6] = [0x10, 0x02, 0x02, 0x03, 0x10, 0x03];
let mut read_len = 0;
let decode_result = dle_encoder.decode_non_escaped(
    &encoded_array, &mut decoding_buffer, &mut read_len
);
assert!(decode_result.is_ok());
let decoded_len = decode_result.unwrap();
assert_eq!(decoded_len, 2);

println!("Source buffer: {:?}", encoded_array);
println!("Encoded stream: {:?}", &decoding_buffer[ .. decoded_len])

Trait Implementations§

Source§

impl Clone for DleEncoder

Source§

fn clone(&self) -> DleEncoder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for DleEncoder

Source§

fn default() -> DleEncoder

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

impl Copy for DleEncoder

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.