diff options
Diffstat (limited to 'apps/cassiopeia/src/format/mod.rs')
-rw-r--r-- | apps/cassiopeia/src/format/mod.rs | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/apps/cassiopeia/src/format/mod.rs b/apps/cassiopeia/src/format/mod.rs index 814c08656dbe..89f3a6ccb466 100644 --- a/apps/cassiopeia/src/format/mod.rs +++ b/apps/cassiopeia/src/format/mod.rs @@ -1,18 +1,22 @@ //! cassiopeia file format -mod ir; +mod gen; +pub(crate) mod ir; mod lexer; mod parser; -pub(crate) use ir::{IrItem, IrStream, IrType, MakeIr}; pub(crate) use lexer::{LineLexer, LineToken, Token}; pub(crate) use parser::LineCfg; use crate::TimeFile; -use std::{fs::File, io::Read}; +use ir::{IrItem, IrStream}; +use std::{ + fs::{File, OpenOptions}, + io::{Read, Write}, +}; #[derive(Default)] -pub struct ParseOutput { +pub(crate) struct ParseOutput { pub(crate) ir: IrStream, pub(crate) tf: TimeFile, } @@ -27,7 +31,7 @@ impl ParseOutput { /// Load a file from disk and parse it into a /// [`TimeFile`](crate::TimeFile) -pub fn load_file(path: &str) -> Option<ParseOutput> { +pub(crate) fn load_file(path: &str) -> Option<ParseOutput> { let mut f = File::open(path).ok()?; let mut content = String::new(); f.read_to_string(&mut content).ok()?; @@ -45,3 +49,19 @@ pub fn load_file(path: &str) -> Option<ParseOutput> { .fold(ParseOutput::default(), |output, ir| output.append(ir)), ) } + +/// Write a file with the updated IR stream +pub(crate) fn write_file(path: &str, ir: &mut IrStream) -> Option<()> { + ir::update_header(ir); + let mut lines = ir.into_iter().map(|ir| gen::line(ir)).collect::<Vec<_>>(); + lines.insert(0, gen::head_comment()); + + let mut f = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(path) + .ok()?; + f.write_all(lines.join("\n").as_bytes()).ok()?; + Some(()) +} |