stream_file/
stream_file.rs

1use std::{
2    env,
3    fs::File,
4    io::{BufReader, Read},
5};
6
7use binarygcode::{DeserialisedResult, Deserialiser};
8
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}