Skip to content

Resolve the majority of pedantic clippy warnings #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bin/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let args = match parse_args() {
Ok(args) => args,
Err(e) => {
eprintln!("Failed to process arguments: {}", e);
eprintln!("Failed to process arguments: {e}");
process::exit(1);
}
};
Expand All @@ -39,7 +39,7 @@ fn main() {
}
}
Err(e) => {
eprintln!("failed to spawn aoc-cli: {}", e);
eprintln!("failed to spawn aoc-cli: {e}");
process::exit(1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let args = match parse_args() {
Ok(args) => args,
Err(e) => {
eprintln!("Failed to process arguments: {}", e);
eprintln!("Failed to process arguments: {e}");
process::exit(1);
}
};
Expand All @@ -39,7 +39,7 @@ fn main() {
}
}
Err(e) => {
eprintln!("failed to spawn aoc-cli: {}", e);
eprintln!("failed to spawn aoc-cli: {e}");
process::exit(1);
}
}
Expand Down
29 changes: 14 additions & 15 deletions src/bin/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use std::{
process,
};

const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> Option<u32> {
const MODULE_TEMPLATE: &str = r###"#[must_use]
pub fn part_one(input: &str) -> Option<u32> {
None
}

#[must_use]
pub fn part_two(input: &str) -> Option<u32> {
None
}
Expand Down Expand Up @@ -54,24 +56,21 @@ fn create_file(path: &str) -> Result<File, std::io::Error> {
}

fn main() {
let day = match parse_args() {
Ok(day) => day,
Err(_) => {
eprintln!("Need to specify a day (as integer). example: `cargo scaffold 7`");
process::exit(1);
}
let Ok(day) = parse_args() else {
eprintln!("Need to specify a day (as integer). example: `cargo scaffold 7`");
process::exit(1);
};

let day_padded = format!("{:02}", day);
let day_padded = format!("{day:02}");

let input_path = format!("src/inputs/{}.txt", day_padded);
let example_path = format!("src/examples/{}.txt", day_padded);
let module_path = format!("src/bin/{}.rs", day_padded);
let input_path = format!("src/inputs/{day_padded}.txt");
let example_path = format!("src/examples/{day_padded}.txt");
let module_path = format!("src/bin/{day_padded}.rs");

let mut file = match safe_create_file(&module_path) {
Ok(file) => file,
Err(e) => {
eprintln!("Failed to create module file: {}", e);
eprintln!("Failed to create module file: {e}");
process::exit(1);
}
};
Expand All @@ -81,7 +80,7 @@ fn main() {
println!("Created module file \"{}\"", &module_path);
}
Err(e) => {
eprintln!("Failed to write module contents: {}", e);
eprintln!("Failed to write module contents: {e}");
process::exit(1);
}
}
Expand All @@ -91,7 +90,7 @@ fn main() {
println!("Created empty input file \"{}\"", &input_path);
}
Err(e) => {
eprintln!("Failed to create input file: {}", e);
eprintln!("Failed to create input file: {e}");
process::exit(1);
}
}
Expand All @@ -101,7 +100,7 @@ fn main() {
println!("Created empty example file \"{}\"", &example_path);
}
Err(e) => {
eprintln!("Failed to create example file: {}", e);
eprintln!("Failed to create example file: {e}");
process::exit(1);
}
}
Expand Down
46 changes: 24 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ macro_rules! solve {
}};
}

#[must_use]
pub fn read_file(folder: &str, day: u8) -> String {
let cwd = env::current_dir().unwrap();

let filepath = cwd.join("src").join(folder).join(format!("{:02}.txt", day));
let filepath = cwd.join("src").join(folder).join(format!("{day:02}.txt"));

let f = fs::read_to_string(filepath);
f.expect("could not open input file")
Expand All @@ -54,11 +55,10 @@ fn parse_time(val: &str, postfix: &str) -> f64 {
val.split(postfix).next().unwrap().parse().unwrap()
}

#[must_use]
pub fn parse_exec_time(output: &str) -> f64 {
output.lines().fold(0_f64, |acc, l| {
if !l.contains("elapsed:") {
acc
} else {
if l.contains("elapsed:") {
let timing = l.split("(elapsed: ").last().unwrap();
// use `contains` istd. of `ends_with`: string may contain ANSI escape sequences.
// for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200
Expand All @@ -73,6 +73,8 @@ pub fn parse_exec_time(output: &str) -> f64 {
} else {
acc
}
} else {
acc
}
})
}
Expand Down Expand Up @@ -131,45 +133,45 @@ pub mod aoc_cli {
process::{Command, Output, Stdio},
};

pub enum AocCliError {
pub enum CliError {
CommandNotFound,
CommandNotCallable,
BadExitStatus(Output),
IoError,
}

impl Display for AocCliError {
impl Display for CliError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."),
AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."),
AocCliError::BadExitStatus(_) => {
CliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."),
CliError::CommandNotCallable => write!(f, "aoc-cli could not be called."),
CliError::BadExitStatus(_) => {
write!(f, "aoc-cli exited with a non-zero status.")
}
AocCliError::IoError => write!(f, "could not write output files to file system."),
CliError::IoError => write!(f, "could not write output files to file system."),
}
}
}

pub fn check() -> Result<(), AocCliError> {
pub fn check() -> Result<(), CliError> {
Command::new("aoc")
.arg("-V")
.output()
.map_err(|_| AocCliError::CommandNotFound)?;
.map_err(|_| CliError::CommandNotFound)?;
Ok(())
}

pub fn read(day: u8, year: Option<u16>) -> Result<Output, AocCliError> {
pub fn read(day: u8, year: Option<u16>) -> Result<Output, CliError> {
// TODO: output local puzzle if present.
let args = build_args("read", &[], day, year);
call_aoc_cli(&args)
}

pub fn download(day: u8, year: Option<u16>) -> Result<Output, AocCliError> {
pub fn download(day: u8, year: Option<u16>) -> Result<Output, CliError> {
let input_path = get_input_path(day);

let puzzle_path = get_puzzle_path(day);
create_dir_all("src/puzzles").map_err(|_| AocCliError::IoError)?;
create_dir_all("src/puzzles").map_err(|_| CliError::IoError)?;

let args = build_args(
"download",
Expand All @@ -192,18 +194,18 @@ pub mod aoc_cli {
println!("🎄 Successfully wrote puzzle to \"{}\".", &puzzle_path);
Ok(output)
} else {
Err(AocCliError::BadExitStatus(output))
Err(CliError::BadExitStatus(output))
}
}

fn get_input_path(day: u8) -> String {
let day_padded = format!("{:02}", day);
format!("src/inputs/{}.txt", day_padded)
let day_padded = format!("{day:02}");
format!("src/inputs/{day_padded}.txt")
}

fn get_puzzle_path(day: u8) -> String {
let day_padded = format!("{:02}", day);
format!("src/puzzles/{}.md", day_padded)
let day_padded = format!("{day:02}");
format!("src/puzzles/{day_padded}.md")
}

fn build_args(command: &str, args: &[String], day: u8, year: Option<u16>) -> Vec<String> {
Expand All @@ -219,7 +221,7 @@ pub mod aoc_cli {
cmd_args
}

fn call_aoc_cli(args: &[String]) -> Result<Output, AocCliError> {
fn call_aoc_cli(args: &[String]) -> Result<Output, CliError> {
if cfg!(debug_assertions) {
println!("Calling >aoc with: {}", args.join(" "));
}
Expand All @@ -229,6 +231,6 @@ pub mod aoc_cli {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.map_err(|_| AocCliError::CommandNotCallable)
.map_err(|_| CliError::CommandNotCallable)
}
}
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::process::Command;
fn main() {
let total: f64 = (1..=25)
.map(|day| {
let day = format!("{:02}", day);
let day = format!("{day:02}");

let mut args = vec!["run", "--bin", &day];
if cfg!(not(debug_assertions)) {
Expand All @@ -18,7 +18,7 @@ fn main() {
let cmd = Command::new("cargo").args(&args).output().unwrap();

println!("----------");
println!("{}| Day {} |{}", ANSI_BOLD, day, ANSI_RESET);
println!("{ANSI_BOLD}| Day {day} |{ANSI_RESET}");
println!("----------");

let output = String::from_utf8(cmd.stdout).unwrap();
Expand All @@ -41,8 +41,5 @@ fn main() {
})
.sum();

println!(
"{}Total:{} {}{:.2}ms{}",
ANSI_BOLD, ANSI_RESET, ANSI_ITALIC, total, ANSI_RESET
);
println!("{ANSI_BOLD}Total:{ANSI_RESET} {ANSI_ITALIC}{total:.2}ms{ANSI_RESET}");
}