diff options
author | Mx Kookie <kookie@spacekookie.de> | 2020-12-15 19:41:48 +0000 |
---|---|---|
committer | Mx Kookie <kookie@spacekookie.de> | 2020-12-15 19:41:48 +0000 |
commit | d7fff0b83e0bc931e29a3e64c1f978a09a3fd429 (patch) | |
tree | 6328b2700e74fae191967391c9f8f6b054c36c1a /apps/cassiopeia/src/format/mod.rs | |
parent | bfe67afa8a9238bd2a3bfdc9079917c81c8cfef3 (diff) |
cassiopeia: finishing up version 0.3.0
This commit does kind of a lot to get cass(1) over the finish line.
For one it implements all the CLI functions (well, almost all) with
their respective parameters, and also creates a new `gen` module which
uses the IR stream to generate a new file based on the old one, while
updating header fields that need to be updated (currently only
`version`).
This version does nothing with the actual header values, and probably
has a lot of bugs. More documentation will follow in future
cassiopeia commits.
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(()) +} |