feat(scaffold): overwrite argument (#63)

This commit is contained in:
RG 2024-11-01 03:32:13 +05:30 committed by GitHub
parent e2062e33fd
commit 3657f8c6dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View file

@ -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)
}

View file

@ -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<File, std::io::Error> {
OpenOptions::new().write(true).create_new(true).open(path)
fn safe_create_file(path: &str, overwrite: bool) -> Result<File, std::io::Error> {
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<File, std::io::Error> {
@ -21,12 +27,12 @@ fn create_file(path: &str) -> Result<File, std::io::Error> {
.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}");