Skip to content

Commit

Permalink
Scaffold: Init scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Nov 27, 2023
1 parent 03deb5d commit 7a06b69
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
27 changes: 2 additions & 25 deletions scaffold/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,6 @@ pub struct Args {
pub subcommand: SubCommand,
}

impl Args {
pub fn as_token_args(&self) -> &TokenArgs {
match &self.subcommand {
SubCommand::Token(args) => args,
_ => panic!("Expected token subcommand"),
}
}

pub fn as_timer_args(&self) -> &TimerArgs {
match &self.subcommand {
SubCommand::Timer(args) => args,
_ => panic!("Expected timer subcommand"),
}
}

pub fn as_init_args(&self) -> &InitArgs {
match &self.subcommand {
SubCommand::Init(args) => args,
_ => panic!("Expected init subcommand"),
}
}
}

#[derive(Parser, Debug)]
pub enum SubCommand {
/// Verify that the session token provided is still valid
Expand Down Expand Up @@ -80,7 +57,7 @@ pub struct InitArgs {
pub input_location: String,
/// A formatter that will be used to get the path for the solution file.
#[arg(short, long, default_value = "aoc_{{year}}/src/day_{{day:pad(2)}}.rs")]
solution_location: String,
pub solution_location: String,
/// Location formatter of the file importing each solution module.
#[arg(long, default_value = "aoc_{{year}}/src/lib.rs")]
module_location: String,
Expand All @@ -94,7 +71,7 @@ pub struct InitArgs {
/// Path to a template file that will be used to create the solution file.
/// If not provided, a default template will be used.
#[arg(short = 't', long)]
solution_template: Option<String>,
pub solution_template: Option<String>,
/// Don't create a solution file.
/// Useful if you want to use this command with a different language or organization.
#[arg(short, long)]
Expand Down
40 changes: 32 additions & 8 deletions scaffold/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,36 @@ use crate::{
};

pub fn init(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> {
if !cmd.no_scaffold {
write_scaffold(cmd, args)?;
}

write_input(session, cmd, args)?;
Ok(())
}

fn write_scaffold(cmd: &InitArgs, args: &Args) -> Result<()> {
let args: &[(&str, u16)] = &[("year", cmd.year), ("day", cmd.day as u16)];
let file_location = Formatter::new(&cmd.solution_location)?.format(args)?;
let mut file = create_file(&Path::new(&file_location))?;

println!("[*] Loading template");
let template = match cmd.solution_template {
Some(ref path) => fs::read_to_string(path)?,
None => include_str!("../../template.txt").to_owned(),
};
let template = Formatter::new(&template)?.format(args)?;

file.write_all(template.as_bytes())?;
println!("[*] Wrote scaffold to {file_location}");
Ok(())
}

fn write_input(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> {
let file_location = Formatter::new(&cmd.input_location)?
.format::<&[_]>(&[("year", cmd.year), ("day", cmd.day as u16)])?;

let path = Path::new(&file_location);
if let Some(parent) = path.parent() {
if !parent.exists() {
fs::create_dir_all(parent)?;
}
}

let mut file = File::create(path)?;
let mut file = create_file(&Path::new(&file_location))?;
let input = fetch_input(session, &args.address, cmd.day, Some(cmd.year))?;
file.write_all(input.as_bytes())?;
println!("[*] Wrote input to {file_location}");
Expand All @@ -49,3 +63,13 @@ fn fetch_input(session: &Session, base: &Url, day: u8, year: Option<u16>) -> Res

Ok(body)
}

fn create_file(path: &Path) -> Result<File> {
if let Some(parent) = path.parent() {
if !parent.exists() {
fs::create_dir_all(parent)?;
}
}

Ok(File::create(path)?)
}
2 changes: 1 addition & 1 deletion scaffold/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<T: Display> Arguments for &[(&str, T)] {
}

mod tokenize {
use anyhow::{bail, Result};
use anyhow::Result;

use super::{Component, Processor};

Expand Down
3 changes: 3 additions & 0 deletions scaffold/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- [ ] Formatter
- [ ] Commands
- [ ] Init
- [x] Scaffold
- [ ] Module
- [x] Input
- [x] Verify
- [x] Token
- [x] Timer

0 comments on commit 7a06b69

Please sign in to comment.