easymacros/src/macro_writer.rs

31 lines
870 B
Rust
Raw Normal View History

2022-10-10 20:03:00 +02:00
use crate::Instructions;
2022-10-10 20:36:25 +02:00
use std::{io, fs};
2022-07-21 10:54:56 +02:00
pub struct MacroWriter {
2022-10-10 20:36:25 +02:00
outfile: Box<dyn io::Write>,
2022-10-10 20:03:00 +02:00
ignore_delay_capturing: bool,
2022-07-21 10:54:56 +02:00
}
impl MacroWriter {
2022-10-10 20:03:00 +02:00
pub fn new(outfile: Option<std::path::PathBuf>, ignore_delay_capturing: bool) -> Self {
Self {
2022-10-10 20:36:25 +02:00
outfile:
if let Some(outfile) = outfile {
Box::new(fs::File::create(outfile).expect("Failed to create output file"))
} else {
Box::new(io::stdout())
},
2022-10-10 20:03:00 +02:00
ignore_delay_capturing,
}
}
2022-07-21 10:54:56 +02:00
2022-10-10 20:03:00 +02:00
pub fn write(&mut self, instruction: Instructions) {
if self.ignore_delay_capturing {
2022-10-10 20:36:25 +02:00
if let Instructions::Delay(_) = instruction { () }
2022-10-10 20:03:00 +02:00
}
2022-07-21 10:54:56 +02:00
2022-10-10 20:03:00 +02:00
writeln!(&mut self.outfile, "{}", instruction)
.expect("Failed to write instruction to outfile");
}
2022-07-21 10:54:56 +02:00
}