easymacros/src/macro_writer.rs
2022-10-11 03:07:22 +02:00

30 lines
867 B
Rust

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