1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use std::error::Error; use std::fmt; use std::path::Path; use std::process::Command; #[derive(Debug)] pub struct BisonErr { pub message: String, pub code: Option<i32>, } impl fmt::Display for BisonErr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "BisonErr: {:#?} ({:#?})", self.message, self.code) } } impl Error for BisonErr {} pub fn process_bison_file(filepath: &Path) -> Result<(), BisonErr> { let input = filepath; let output = filepath.with_extension("rs"); let bison_root_dir = Path::new(file!()) .parent() .unwrap() .parent() .unwrap() .join("bison"); let bison_root_file = bison_root_dir.join("main.m4"); let args = &[ "-S", bison_root_file.to_str().unwrap(), "-o", output.to_str().unwrap(), input.to_str().unwrap(), ]; let output = Command::new("bison").args(args).output().unwrap(); if output.status.success() { Ok(()) } else { let stderr = String::from_utf8(output.stderr).unwrap(); Err(BisonErr { message: stderr, code: output.status.code(), }) } }