pub fn parse<T: Read>(
reader: BufReader<T>,
) -> Result<GerberDoc, (GerberDoc, ParseError)>Expand description
Parse a gerber string (in BufReader) to a GerberDoc
Take the contents of a Gerber (.gbr) file and parse it to a GerberDoc struct. The parsing does some semantic checking, but is certainly not exhaustive - so don’t rely on it to check if your Gerber file is valid according to the spec. Some of the parsing steps are greedy - they may match something unexpected (rather than panicking) if there is a typo/fault in your file.
If a fatal error occurs (like an IO error), then a partial GerberDoc will be returned along with the error.
Examples found in repository?
examples/example1.rs (line 17)
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}