Skip to content

Commit

Permalink
Adding new source file for writing output
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 3, 2024
1 parent 615946c commit d5ecf97
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
** Documentation
** Tidy up code
** Find todos
** New source files: write.rs, print.rs
** New source files: print.rs
** Tests better output
** Better file backup
* Bugs
** Better errors including line numbers in source file
** Handle list items properly
16 changes: 4 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use clap::Parser;
use std::env::temp_dir;
use std::fs;
use std::path;

const TAB: i8 = 2;

Expand All @@ -16,8 +14,10 @@ mod parse;
mod regexes;
mod subs;
mod wrap;
mod write;
use crate::format::*;
use crate::parse::Cli;
use crate::write::*;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -57,16 +57,8 @@ fn main() {
// print new file
println!("{}", &new_file);
} else {
// backup original file
let filepath = path::Path::new(&filename).canonicalize().unwrap();
let mut filebak = temp_dir();
filebak.push("tex-fmt");
fs::create_dir_all(&filebak).unwrap();
filebak.push(filepath.file_name().unwrap());
fs::copy(filepath.clone(), &filebak).unwrap();

// write new file
fs::write(filepath, new_file).unwrap();
backup_file(&filename);
write_file(&filename, &new_file);
}
}
}
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Parser;
use clap::Parser;

#[derive(Parser)]
pub struct Cli {
Expand Down
18 changes: 18 additions & 0 deletions src/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::fs;
use std::path;
use std::env::temp_dir;

pub fn backup_file(filename: &str) {
let filepath = path::Path::new(&filename).canonicalize().unwrap();
let mut filebak = temp_dir();
filebak.push("tex-fmt");
fs::create_dir_all(&filebak).unwrap();
filebak.push(filepath.file_name().unwrap());
fs::copy(filepath.clone(), &filebak).unwrap();
}

pub fn write_file(filename: &str, new_file: &str) {
let filepath = path::Path::new(&filename).canonicalize().unwrap();
fs::write(filepath, new_file).unwrap();
}

0 comments on commit d5ecf97

Please sign in to comment.