Unpacker

Struct Unpacker 

Source
pub struct Unpacker<const S: usize> { /* private fields */ }
Expand description

A struct for that unpacks bytes and emits lines of gcode.

Implementations§

Source§

impl<const S: usize> Unpacker<S>

Source

pub fn unpack(&mut self, byte: &u8) -> Result<MeatPackResult<'_>, MeatPackError>

Unpacks a single meatpacked byte checking on the history of the previously unpacked items. It returns detailing what it is waiting for next.

Examples found in repository?
examples/unpack.rs (line 22)
5fn main() {
6    // Some meatpacked gcode
7    let packed: [u8; 93] = [
8        255, 255, 251, 255, 255, 247, 255, 255, 250, 59, 32, 10, 255, 255, 251, 127, 77, 243, 32,
9        15, 80, 255, 32, 82, 195, 127, 77, 243, 32, 15, 81, 255, 32, 83, 195, 47, 77, 16, 239, 32,
10        4, 0, 255, 32, 89, 4, 0, 255, 32, 90, 2, 240, 32, 43, 5, 192, 47, 77, 48, 239, 32, 3, 240,
11        32, 63, 89, 0, 255, 32, 90, 4, 191, 32, 1, 192, 47, 77, 64, 255, 32, 80, 4, 0, 255, 32, 82,
12        33, 0, 255, 32, 84, 4, 0,
13    ];
14
15    // Initiliase the packer with buffer size depending
16    // on your application
17    let mut unpacker = Unpacker::<64>::default();
18
19    // Imagine receiving the bytes from some I/O and we want
20    // to construct gcode lines and deal with them as we form them.
21    for b in packed.iter() {
22        let res = unpacker.unpack(b);
23        match res {
24            Ok(MeatPackResult::WaitingForNextByte) => {
25                //println!("Waiting for next byte");
26            }
27            Ok(MeatPackResult::Line(line)) => {
28                let line = str::from_utf8(line).unwrap();
29                println!("{:?}", line);
30            }
31            Err(e) => {
32                println!("{:?}", e);
33                panic!();
34            }
35        }
36    }
37}
More examples
Hide additional examples
examples/pack_unpack.rs (line 51)
5fn main() {
6    let gcode = "M73 P0 R3
7M73 Q0 S3 ; Hello
8M201 X4000 Y4000 Z200 E2500
9M203 X300 Y300 Z40 E100
10M204 P4000 R1200 T4000
11";
12
13    println!("## IN ##");
14    println!("{}", gcode);
15    println!("####");
16
17    // Initiliase the packer with buffer size depending
18    // on your application
19    let mut packer = Packer::<64>::default();
20    let mut out: Vec<u8> = vec![];
21
22    // This will store the entire packed version of the gcode.
23    // Don't forget the header.
24    out.extend(&MEATPACK_HEADER);
25
26    // Feed in the bytes as you receive them and
27    // the packer will return completed lines of
28    // meatpacked gcode.
29    for byte in gcode.as_bytes() {
30        let packed = packer.pack(byte);
31        match packed {
32            Ok(MeatPackResult::Line(line)) => {
33                println!("{:?}", line);
34                out.extend(line);
35            }
36            Ok(MeatPackResult::WaitingForNextByte) => {}
37            Err(e) => println!("{:?}", e),
38        }
39    }
40
41    println!("{:?}", out);
42
43    println!("## OUT ##");
44
45    // Now we create an unpacker to unpack the meatpacked data.
46    let mut unpacker = Unpacker::<64>::default();
47
48    // Imagine receiving the bytes from some I/O and we want
49    // to construct gcode lines and deal with them as we form them.
50    for byte in out.iter() {
51        let res = unpacker.unpack(byte);
52        match res {
53            Ok(MeatPackResult::WaitingForNextByte) => {}
54            Ok(MeatPackResult::Line(line)) => {
55                // If in std.
56                for byte in line {
57                    let c = char::from(*byte);
58                    print!("{}", c);
59                }
60            }
61            Err(e) => {
62                println!("{:?}", e);
63                process::exit(0)
64            }
65        }
66    }
67
68    println!("####");
69}
Source

pub fn data_remains(&self) -> bool

A utility function to check if any data remains in the internal buffer. We expect all meatpack lines to newline end.

Trait Implementations§

Source§

impl<const S: usize> Default for Unpacker<S>

Source§

fn default() -> Self

The default implementation of the unpacker.

Auto Trait Implementations§

§

impl<const S: usize> Freeze for Unpacker<S>

§

impl<const S: usize> RefUnwindSafe for Unpacker<S>

§

impl<const S: usize> Send for Unpacker<S>

§

impl<const S: usize> Sync for Unpacker<S>

§

impl<const S: usize> Unpin for Unpacker<S>

§

impl<const S: usize> UnwindSafe for Unpacker<S>

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.