Packer

Struct Packer 

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

A struct for that packs bytes and emits lines of meatpacked gcode. Stripping comments is on by default and empty lines are omitted.

Implementations§

Source§

impl<const S: usize> Packer<S>

Source

pub fn new(strip_comments: bool, strip_whitespace: bool) -> Self

Create a new instance of the packer

Source

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

Pack a byte into the current line.

Examples found in repository?
examples/pack.rs (line 20)
3fn main() {
4    // Some example gcode.
5    let gcode = "M73 P0 R3
6M73 Q0 S3 ; Hello
7M201 X4000 Y4000 Z200 E2500
8M203 X300 Y300 Z40 E100
9M204 P4000 R1200 T4000
10";
11
12    // Initiliase the packer with buffer size depending
13    // on your application
14    let mut packer = Packer::<64>::default();
15
16    // Feed in the bytes as you receive them and
17    // the packer will return completed lines of
18    // meatpacked gcode to send onwards.
19    for byte in gcode.as_bytes() {
20        let packed = packer.pack(byte);
21        match packed {
22            Ok(MeatPackResult::Line(line)) => {
23                println!("{:?}", line);
24            }
25            Ok(MeatPackResult::WaitingForNextByte) => {}
26            Err(e) => {
27                println!("{:?}", e);
28                panic!()
29            }
30        }
31    }
32}
More examples
Hide additional examples
examples/pack_unpack.rs (line 30)
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 inner. We expect all meatpack lines to newline end.

Trait Implementations§

Source§

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

Source§

fn default() -> Self

The default implementation of a Packer.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<const S: usize> UnwindSafe for Packer<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.