Skip to main content

Decoder

Struct Decoder 

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

Incremental decoder for a GridConnect byte stream (serial port, TCP socket, or any other byte transport). Frames may be split arbitrarily across push calls and may be separated by any whitespace (or nothing at all).

Bytes outside a :…; token are ignored, so the decoder recovers from line noise and from joining a stream mid-frame. A : always starts a new token; if a previous token was still open, it is reported as Err(ParseError::Format("unterminated frame")). A token that grows past 32 characters without a ; is discarded (reported as ParseError::Length). The decoder never allocates on its own: its buffer is a fixed 32-byte array.

use rolcb::gridconnect::Decoder;

let mut d = Decoder::new();
// A read can end in the middle of a frame…
assert!(d.push(b"junk:X10700").is_empty());
// …and the next one may hold the rest plus more frames.
let frames = d.push(b"285N;\n:X19100285N050101012200;");
assert_eq!(frames.len(), 2);
assert_eq!(frames[0].as_ref().unwrap().id, 0x1070_0285);
assert_eq!(frames[1].as_ref().unwrap().data(), &[5, 1, 1, 1, 0x22, 0]);

// Malformed frames are reported, and decoding carries on.
let frames = d.push(b":X1G;:X10700285N;");
assert!(frames[0].is_err());
assert!(frames[1].is_ok());

Implementations§

Source§

impl Decoder

Source

pub fn new() -> Self

A decoder with an empty buffer.

Source

pub fn push(&mut self, bytes: &[u8]) -> Vec<Result<CanFrame, ParseError>>

Feed bytes; returns every complete frame found. Malformed frames are returned as Err so callers can log them, and decoding continues.

To skip errors, flatten the result: for frame in decoder.push(bytes).into_iter().flatten() { … }.

Source

pub fn push_with<F>(&mut self, bytes: &[u8], f: F)

Feed bytes and call f for every complete (or malformed) frame, in order, without allocating. Same rules as Decoder::push.

use rolcb::gridconnect::Decoder;

let mut d = Decoder::new();
let mut ids = Vec::new();
d.push_with(b":X10700285N;:X10701285N050101012200;", |r| {
    if let Ok(frame) = r {
        ids.push(frame.id);
    }
});
assert_eq!(ids, [0x1070_0285, 0x1070_1285]);

Trait Implementations§

Source§

impl Clone for Decoder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Decoder

Source§

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

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

impl Default for Decoder

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.