From 039a54cd7cca2d0803cfea8a285fcc44652bb0c2 Mon Sep 17 00:00:00 2001 From: Connor Slade Date: Fri, 1 Dec 2023 18:11:35 -0500 Subject: [PATCH] Cleanup scaffold --- .gitignore | 4 ++-- aoc_2023/src/day_01.rs | 4 ++-- scaffold/src/args.rs | 3 +++ scaffold/src/commands/init.rs | 10 +++++++--- scaffold/src/commands/timer.rs | 9 +++++++++ scaffold/template.txt | 5 +++-- 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 81ffea9..49aa4ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /target -scripts/new.rs +scripts/ todo.txt -.env \ No newline at end of file +.env diff --git a/aoc_2023/src/day_01.rs b/aoc_2023/src/day_01.rs index 1ef7fdc..2512166 100644 --- a/aoc_2023/src/day_01.rs +++ b/aoc_2023/src/day_01.rs @@ -88,12 +88,12 @@ mod test { "}; #[test] - fn test_a() { + fn part_a() { assert_eq!(Day01.part_a(CASE_A), 142.into()); } #[test] - fn test_b() { + fn part_b() { assert_eq!(Day01.part_b(CASE_B), 281.into()); } } diff --git a/scaffold/src/args.rs b/scaffold/src/args.rs index fb7ca48..7cec775 100644 --- a/scaffold/src/args.rs +++ b/scaffold/src/args.rs @@ -79,6 +79,9 @@ pub struct InitArgs { /// Useful if you want to use this command with a different language or organization. #[arg(short, long)] pub no_scaffold: bool, + /// Allows overwriting the existing solution file. + #[arg(long)] + pub allow_overwrite: bool, /// Automatically open the solution file in your editor. /// Only works if you are not using `--no-scaffold`. /// Configure the editor with the `--editor` argument. diff --git a/scaffold/src/commands/init.rs b/scaffold/src/commands/init.rs index 71b7a90..7147b7e 100644 --- a/scaffold/src/commands/init.rs +++ b/scaffold/src/commands/init.rs @@ -54,7 +54,7 @@ pub fn init(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> { fn write_scaffold(cmd: &InitArgs, formats: &[(&str, String)]) -> Result { let location = Formatter::new(&cmd.solution_location)?.format(formats)?; let file_location = Path::new(&location); - let mut file = create_file(&file_location)?; + let mut file = create_file(&file_location, cmd.allow_overwrite)?; println!("[*] Loading template"); let template = match cmd.solution_template { @@ -88,7 +88,7 @@ fn modify_module(cmd: &InitArgs, formats: &[(&str, String)]) -> Result<()> { fn write_input(cmd: &InitArgs, input: ProblemInput, formats: &[(&str, String)]) -> Result<()> { let file_location = Formatter::new(&cmd.input_location)?.format(formats)?; - let mut file = create_file(&Path::new(&file_location))?; + let mut file = create_file(&Path::new(&file_location), true)?; file.write_all(input.body.as_bytes())?; println!("[*] Wrote input to {file_location}"); Ok(()) @@ -126,12 +126,16 @@ struct ProblemInput { body: String, } -fn create_file(path: &Path) -> Result { +fn create_file(path: &Path, allow_overwrite: bool) -> Result { if let Some(parent) = path.parent() { if !parent.exists() { fs::create_dir_all(parent)?; } } + if !allow_overwrite && path.exists() { + return Err(anyhow::anyhow!("File already exists: {}", path.display())); + } + Ok(File::create(path)?) } diff --git a/scaffold/src/commands/timer.rs b/scaffold/src/commands/timer.rs index 9dc7e92..f1eefd1 100644 --- a/scaffold/src/commands/timer.rs +++ b/scaffold/src/commands/timer.rs @@ -77,5 +77,14 @@ fn next_release() -> Result> { .context("Can not represent the next first of December.")?; } + if Utc::now() > next { + next = next + .date_naive() + .succ_opt() + .unwrap() + .and_time(next.time()) + .and_utc(); + } + Ok(next) } diff --git a/scaffold/template.txt b/scaffold/template.txt index 5946329..1088b90 100644 --- a/scaffold/template.txt +++ b/scaffold/template.txt @@ -18,6 +18,7 @@ impl Solution for Day{{day:pad(2)}} { #[cfg(test)] mod test { + use common::Solution; use indoc::indoc; use super::Day{{day:pad(2)}}; @@ -27,12 +28,12 @@ mod test { "}; #[test] - fn test_a() { + fn part_a() { assert_eq!(Day{{day:pad(2)}}.part_a(CASE), Answer::Unimplemented); } #[test] - fn test_b() { + fn part_b() { assert_eq!(Day{{day:pad(2)}}.part_b(CASE), Answer::Unimplemented); } } \ No newline at end of file