example1/
example1.rs

1extern crate gerber_parser;
2
3use anyhow::anyhow;
4use gerber_types::Command;
5
6pub fn main() -> anyhow::Result<()> {
7    use std::fs::File;
8    use std::io::BufReader;
9
10    let path = "assets/reference_files/two_square_boxes.gbr";
11
12    // open a .gbr file from system
13    let file = File::open(path).unwrap();
14    let reader = BufReader::new(file);
15
16    // Now we parse the file to a GerberDoc, if io errors occur a partial gerber_doc will be returned along with the error
17    let gerber_doc = gerber_parser::parse(reader)
18        .map_err(|(_doc, parse_error)| anyhow!("Error parsing file: {:?}", parse_error))?;
19
20    let commands: Vec<&Command> = gerber_doc.commands();
21
22    // Now you can use the commands as you wish
23    println!("Parsed document. command_count: {} ", commands.len());
24    dump_commands(&commands);
25
26    // there are other methods that consume the document to yield an 'atomic' representation purely
27    // in terms of types defined in the gerber-types crate
28    let _commands: Vec<Command> = gerber_doc.into_commands();
29
30    Ok(())
31}
32
33pub fn dump_commands(commands: &Vec<&Command>) {
34    for command in commands {
35        println!("{:?}", command);
36    }
37}