diff --git a/src/main.rs b/src/main.rs index 9322423..0ee1c39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ mod args { Scaffold { day: Day, download: bool, + overwrite: bool, }, Solve { day: Day, @@ -65,6 +66,7 @@ mod args { Some("scaffold") => AppArguments::Scaffold { day: args.free_from_str()?, download: args.contains("--download"), + overwrite: args.contains("--overwrite"), }, Some("solve") => AppArguments::Solve { day: args.free_from_str()?, @@ -104,8 +106,8 @@ fn main() { AppArguments::Time { day, all, store } => time::handle(day, all, store), AppArguments::Download { day } => download::handle(day), AppArguments::Read { day } => read::handle(day), - AppArguments::Scaffold { day, download } => { - scaffold::handle(day); + AppArguments::Scaffold { day, download, overwrite } => { + scaffold::handle(day, overwrite); if download { download::handle(day); } @@ -120,7 +122,7 @@ fn main() { AppArguments::Today => { match Day::today() { Some(day) => { - scaffold::handle(day); + scaffold::handle(day, false); download::handle(day); read::handle(day) } diff --git a/src/template/commands/scaffold.rs b/src/template/commands/scaffold.rs index 9a0021f..fc3d950 100644 --- a/src/template/commands/scaffold.rs +++ b/src/template/commands/scaffold.rs @@ -9,8 +9,14 @@ use crate::template::Day; const MODULE_TEMPLATE: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt")); -fn safe_create_file(path: &str) -> Result { - OpenOptions::new().write(true).create_new(true).open(path) +fn safe_create_file(path: &str, overwrite: bool) -> Result { + let mut file = OpenOptions::new(); + if overwrite { + file.create(true); + } else { + file.create_new(true); + } + file.truncate(true).write(true).open(path) } fn create_file(path: &str) -> Result { @@ -21,12 +27,12 @@ fn create_file(path: &str) -> Result { .open(path) } -pub fn handle(day: Day) { +pub fn handle(day: Day, overwrite: bool) { let input_path = format!("data/inputs/{day}.txt"); let example_path = format!("data/examples/{day}.txt"); let module_path = format!("src/bin/{day}.rs"); - let mut file = match safe_create_file(&module_path) { + let mut file = match safe_create_file(&module_path, overwrite) { Ok(file) => file, Err(e) => { eprintln!("Failed to create module file: {e}");