Skip to content

Commit

Permalink
Cleanup scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 1, 2023
1 parent 6f64029 commit 039a54c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
scripts/new.rs
scripts/
todo.txt
.env
.env
4 changes: 2 additions & 2 deletions aoc_2023/src/day_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
3 changes: 3 additions & 0 deletions scaffold/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions scaffold/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn init(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> {
fn write_scaffold(cmd: &InitArgs, formats: &[(&str, String)]) -> Result<PathBuf> {
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 {
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -126,12 +126,16 @@ struct ProblemInput {
body: String,
}

fn create_file(path: &Path) -> Result<File> {
fn create_file(path: &Path, allow_overwrite: bool) -> Result<File> {
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)?)
}
9 changes: 9 additions & 0 deletions scaffold/src/commands/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,14 @@ fn next_release() -> Result<DateTime<Utc>> {
.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)
}
5 changes: 3 additions & 2 deletions scaffold/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)}};
Expand All @@ -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);
}
}

0 comments on commit 039a54c

Please sign in to comment.