Deserialiser

Struct Deserialiser 

Source
pub struct Deserialiser {
    pub inner: Vec<u8>,
    /* private fields */
}
Expand description

A binarygcode deserialiser that can parse a bgcode file. It can digest data in chunks and returns header and blocks when available. The block remain compressed so the user can decide which ones they which to decompress.

Fields§

§inner: Vec<u8>

Implementations§

Source§

impl Deserialiser

Source

pub fn digest(&mut self, buf: &[u8])

Provide some more bytes for the deserialiser to process/

Examples found in repository?
examples/stream_file.rs (line 35)
9fn main() {
10    // Create the path to the gcode file
11    let mut path = env::current_dir().unwrap();
12    path.push("test_files");
13    //path.push("mini_cube_b.bgcode");
14    path.push("mini_cube_ps2.8.1.bgcode");
15
16    // Open the file and attach a reader
17    let file = File::open(path).unwrap();
18    let mut reader = BufReader::new(file);
19
20    // Initialise the deserialiser
21    let mut deserialiser = Deserialiser::default();
22
23    // Initialise the read buffer. This could be reading from a file
24    // or waiting for intermittent bytes from a network transfer.
25    let mut buf = [0u8; 256];
26
27    loop {
28        // Read bytes into the buffer
29        let read = reader.read(buf.as_mut_slice()).unwrap();
30        // Exit when exhausted
31        if read == 0 {
32            break;
33        }
34        // Provide the read bytes to the deserialiser
35        deserialiser.digest(&buf[..read]);
36
37        // Loop through running deserialise on the deserialisers inner
38        // buffer with it returning either a header, block or request for more bytes.
39        // Or an error when deserialising.
40        loop {
41            let r = deserialiser.deserialise().unwrap();
42            match r {
43                DeserialisedResult::FileHeader(fh) => {
44                    println!("{:?}", fh);
45                }
46                DeserialisedResult::Block(b) => {
47                    println!("{}", b);
48                }
49                DeserialisedResult::MoreBytesRequired(_) => {
50                    break;
51                }
52            }
53        }
54    }
55}
Source

pub fn reset(&mut self)

Reset the deserialisor to its default state.

Source

pub fn deserialise(&mut self) -> Result<DeserialisedResult, BinaryGcodeError>

Try and deserialised either a file header or block element from the current digest.

Examples found in repository?
examples/stream_file.rs (line 41)
9fn main() {
10    // Create the path to the gcode file
11    let mut path = env::current_dir().unwrap();
12    path.push("test_files");
13    //path.push("mini_cube_b.bgcode");
14    path.push("mini_cube_ps2.8.1.bgcode");
15
16    // Open the file and attach a reader
17    let file = File::open(path).unwrap();
18    let mut reader = BufReader::new(file);
19
20    // Initialise the deserialiser
21    let mut deserialiser = Deserialiser::default();
22
23    // Initialise the read buffer. This could be reading from a file
24    // or waiting for intermittent bytes from a network transfer.
25    let mut buf = [0u8; 256];
26
27    loop {
28        // Read bytes into the buffer
29        let read = reader.read(buf.as_mut_slice()).unwrap();
30        // Exit when exhausted
31        if read == 0 {
32            break;
33        }
34        // Provide the read bytes to the deserialiser
35        deserialiser.digest(&buf[..read]);
36
37        // Loop through running deserialise on the deserialisers inner
38        // buffer with it returning either a header, block or request for more bytes.
39        // Or an error when deserialising.
40        loop {
41            let r = deserialiser.deserialise().unwrap();
42            match r {
43                DeserialisedResult::FileHeader(fh) => {
44                    println!("{:?}", fh);
45                }
46                DeserialisedResult::Block(b) => {
47                    println!("{}", b);
48                }
49                DeserialisedResult::MoreBytesRequired(_) => {
50                    break;
51                }
52            }
53        }
54    }
55}

Trait Implementations§

Source§

impl Default for Deserialiser

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.