Skip to content

Commit d7af3dc

Browse files
fspoettelandypymont
andcommitted
refactor: fix most pedantic clippy warnings
closes #29 Co-authored-by: Andy Pymont <[email protected]>
1 parent f46d1e2 commit d7af3dc

File tree

10 files changed

+107
-114
lines changed

10 files changed

+107
-114
lines changed

src/main.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
use advent_of_code::template::commands::{
2-
all::all_handler, download::download_handler, read::read_handler, scaffold::scaffold_handler,
3-
solve::solve_handler,
4-
};
5-
use args::{parse_args, AppArgs};
1+
use advent_of_code::template::commands::{all, download, read, scaffold, solve};
2+
use args::{parse, AppArguments};
63

74
mod args {
85
use std::process;
96

10-
pub enum AppArgs {
7+
pub enum AppArguments {
118
Download {
129
day: u8,
1310
},
@@ -29,31 +26,31 @@ mod args {
2926
},
3027
}
3128

32-
pub fn parse_args() -> Result<AppArgs, Box<dyn std::error::Error>> {
29+
pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
3330
let mut args = pico_args::Arguments::from_env();
3431

3532
let app_args = match args.subcommand()?.as_deref() {
36-
Some("all") => AppArgs::All {
33+
Some("all") => AppArguments::All {
3734
release: args.contains("--release"),
3835
time: args.contains("--time"),
3936
},
40-
Some("download") => AppArgs::Download {
37+
Some("download") => AppArguments::Download {
4138
day: args.free_from_str()?,
4239
},
43-
Some("read") => AppArgs::Read {
40+
Some("read") => AppArguments::Read {
4441
day: args.free_from_str()?,
4542
},
46-
Some("scaffold") => AppArgs::Scaffold {
43+
Some("scaffold") => AppArguments::Scaffold {
4744
day: args.free_from_str()?,
4845
},
49-
Some("solve") => AppArgs::Solve {
46+
Some("solve") => AppArguments::Solve {
5047
day: args.free_from_str()?,
5148
release: args.contains("--release"),
5249
submit: args.opt_value_from_str("--submit")?,
5350
time: args.contains("--time"),
5451
},
5552
Some(x) => {
56-
eprintln!("Unknown command: {}", x);
53+
eprintln!("Unknown command: {x}");
5754
process::exit(1);
5855
}
5956
None => {
@@ -64,30 +61,30 @@ mod args {
6461

6562
let remaining = args.finish();
6663
if !remaining.is_empty() {
67-
eprintln!("Warning: unknown argument(s): {:?}.", remaining);
64+
eprintln!("Warning: unknown argument(s): {remaining:?}.");
6865
}
6966

7067
Ok(app_args)
7168
}
7269
}
7370

7471
fn main() {
75-
match parse_args() {
72+
match parse() {
7673
Err(err) => {
77-
eprintln!("Error: {}", err);
74+
eprintln!("Error: {err}");
7875
std::process::exit(1);
7976
}
8077
Ok(args) => match args {
81-
AppArgs::All { release, time } => all_handler(release, time),
82-
AppArgs::Download { day } => download_handler(day),
83-
AppArgs::Read { day } => read_handler(day),
84-
AppArgs::Scaffold { day } => scaffold_handler(day),
85-
AppArgs::Solve {
78+
AppArguments::All { release, time } => all::handle(release, time),
79+
AppArguments::Download { day } => download::handle(day),
80+
AppArguments::Read { day } => read::handle(day),
81+
AppArguments::Scaffold { day } => scaffold::handle(day),
82+
AppArguments::Solve {
8683
day,
8784
release,
8885
time,
8986
submit,
90-
} => solve_handler(day, release, time, submit),
87+
} => solve::handle(day, release, time, submit),
9188
},
9289
};
9390
}

src/template/aoc_cli.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@ use std::{
55
};
66

77
#[derive(Debug)]
8-
pub enum AocCliError {
8+
pub enum AocCommandError {
99
CommandNotFound,
1010
CommandNotCallable,
1111
BadExitStatus(Output),
1212
IoError,
1313
}
1414

15-
impl Display for AocCliError {
15+
impl Display for AocCommandError {
1616
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1717
match self {
18-
AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."),
19-
AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."),
20-
AocCliError::BadExitStatus(_) => {
18+
AocCommandError::CommandNotFound => write!(f, "aoc-cli is not present in environment."),
19+
AocCommandError::CommandNotCallable => write!(f, "aoc-cli could not be called."),
20+
AocCommandError::BadExitStatus(_) => {
2121
write!(f, "aoc-cli exited with a non-zero status.")
2222
}
23-
AocCliError::IoError => write!(f, "could not write output files to file system."),
23+
AocCommandError::IoError => write!(f, "could not write output files to file system."),
2424
}
2525
}
2626
}
2727

28-
pub fn check() -> Result<(), AocCliError> {
28+
pub fn check() -> Result<(), AocCommandError> {
2929
Command::new("aoc")
3030
.arg("-V")
3131
.output()
32-
.map_err(|_| AocCliError::CommandNotFound)?;
32+
.map_err(|_| AocCommandError::CommandNotFound)?;
3333
Ok(())
3434
}
3535

36-
pub fn read(day: u8) -> Result<Output, AocCliError> {
36+
pub fn read(day: u8) -> Result<Output, AocCommandError> {
3737
let puzzle_path = get_puzzle_path(day);
3838

3939
let args = build_args(
@@ -49,7 +49,7 @@ pub fn read(day: u8) -> Result<Output, AocCliError> {
4949
call_aoc_cli(&args)
5050
}
5151

52-
pub fn download(day: u8) -> Result<Output, AocCliError> {
52+
pub fn download(day: u8) -> Result<Output, AocCommandError> {
5353
let input_path = get_input_path(day);
5454
let puzzle_path = get_puzzle_path(day);
5555

@@ -72,7 +72,7 @@ pub fn download(day: u8) -> Result<Output, AocCliError> {
7272
Ok(output)
7373
}
7474

75-
pub fn submit(day: u8, part: u8, result: &str) -> Result<Output, AocCliError> {
75+
pub fn submit(day: u8, part: u8, result: &str) -> Result<Output, AocCommandError> {
7676
// workaround: the argument order is inverted for submit.
7777
let mut args = build_args("submit", &[], day);
7878
args.push(part.to_string());
@@ -81,13 +81,13 @@ pub fn submit(day: u8, part: u8, result: &str) -> Result<Output, AocCliError> {
8181
}
8282

8383
fn get_input_path(day: u8) -> String {
84-
let day_padded = format!("{:02}", day);
85-
format!("data/inputs/{}.txt", day_padded)
84+
let day_padded = format!("{day:02}");
85+
format!("data/inputs/{day_padded}.txt")
8686
}
8787

8888
fn get_puzzle_path(day: u8) -> String {
89-
let day_padded = format!("{:02}", day);
90-
format!("data/puzzles/{}.md", day_padded)
89+
let day_padded = format!("{day:02}");
90+
format!("data/puzzles/{day_padded}.md")
9191
}
9292

9393
fn get_year() -> Option<u16> {
@@ -110,18 +110,18 @@ fn build_args(command: &str, args: &[String], day: u8) -> Vec<String> {
110110
cmd_args
111111
}
112112

113-
fn call_aoc_cli(args: &[String]) -> Result<Output, AocCliError> {
113+
fn call_aoc_cli(args: &[String]) -> Result<Output, AocCommandError> {
114114
// println!("Calling >aoc with: {}", args.join(" "));
115115
let output = Command::new("aoc")
116116
.args(args)
117117
.stdout(Stdio::inherit())
118118
.stderr(Stdio::inherit())
119119
.output()
120-
.map_err(|_| AocCliError::CommandNotCallable)?;
120+
.map_err(|_| AocCommandError::CommandNotCallable)?;
121121

122122
if output.status.success() {
123123
Ok(output)
124124
} else {
125-
Err(AocCliError::BadExitStatus(output))
125+
Err(AocCommandError::BadExitStatus(output))
126126
}
127127
}

src/template/commands/all.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use crate::template::{
55
ANSI_BOLD, ANSI_ITALIC, ANSI_RESET,
66
};
77

8-
pub fn all_handler(is_release: bool, is_timed: bool) {
8+
pub fn handle(is_release: bool, is_timed: bool) {
99
let mut timings: Vec<Timings> = vec![];
1010

1111
(1..=25).for_each(|day| {
1212
if day > 1 {
1313
println!();
1414
}
1515

16-
println!("{}Day {}{}", ANSI_BOLD, day, ANSI_RESET);
16+
println!("{ANSI_BOLD}Day {day}{ANSI_RESET}");
1717
println!("------");
1818

1919
let output = child_commands::run_solution(day, is_timed, is_release).unwrap();
@@ -27,16 +27,13 @@ pub fn all_handler(is_release: bool, is_timed: bool) {
2727
});
2828

2929
if is_timed {
30-
let total_millis = timings.iter().map(|x| x.total_nanos).sum::<f64>() / 1000000_f64;
30+
let total_millis = timings.iter().map(|x| x.total_nanos).sum::<f64>() / 1_000_000_f64;
3131

32-
println!(
33-
"\n{}Total:{} {}{:.2}ms{}",
34-
ANSI_BOLD, ANSI_RESET, ANSI_ITALIC, total_millis, ANSI_RESET
35-
);
32+
println!("\n{ANSI_BOLD}Total:{ANSI_RESET} {ANSI_ITALIC}{total_millis:.2}ms{ANSI_RESET}");
3633

3734
if is_release {
3835
match readme_benchmarks::update(timings, total_millis) {
39-
Ok(_) => println!("Successfully updated README with benchmarks."),
36+
Ok(()) => println!("Successfully updated README with benchmarks."),
4037
Err(_) => {
4138
eprintln!("Failed to update readme with benchmarks.");
4239
}
@@ -58,9 +55,10 @@ impl From<std::io::Error> for Error {
5855
}
5956
}
6057

58+
#[must_use]
6159
pub fn get_path_for_bin(day: usize) -> String {
62-
let day_padded = format!("{:02}", day);
63-
format!("./src/bin/{}.rs", day_padded)
60+
let day_padded = format!("{day:02}");
61+
format!("./src/bin/{day_padded}.rs")
6462
}
6563

6664
/// All solutions live in isolated binaries.
@@ -80,7 +78,7 @@ mod child_commands {
8078
is_timed: bool,
8179
is_release: bool,
8280
) -> Result<Vec<String>, Error> {
83-
let day_padded = format!("{:02}", day);
81+
let day_padded = format!("{day:02}");
8482

8583
// skip command invocation for days that have not been scaffolded yet.
8684
if !Path::new(&get_path_for_bin(day)).exists() {
@@ -121,7 +119,7 @@ mod child_commands {
121119

122120
for line in stdout.lines() {
123121
let line = line.unwrap();
124-
println!("{}", line);
122+
println!("{line}");
125123
output.push(line);
126124
}
127125

@@ -146,12 +144,9 @@ mod child_commands {
146144
return None;
147145
}
148146

149-
let (timing_str, nanos) = match parse_time(l) {
150-
Some(v) => v,
151-
None => {
152-
eprintln!("Could not parse timings from line: {l}");
153-
return None;
154-
}
147+
let Some((timing_str, nanos)) = parse_time(l) else {
148+
eprintln!("Could not parse timings from line: {l}");
149+
return None;
155150
};
156151

157152
let part = l.split(':').next()?;
@@ -188,8 +183,8 @@ mod child_commands {
188183
let parsed_timing = match str_timing {
189184
s if s.contains("ns") => s.split("ns").next()?.parse::<f64>().ok(),
190185
s if s.contains("µs") => parse_to_float(s, "µs").map(|x| x * 1000_f64),
191-
s if s.contains("ms") => parse_to_float(s, "ms").map(|x| x * 1000000_f64),
192-
s => parse_to_float(s, "s").map(|x| x * 1000000000_f64),
186+
s if s.contains("ms") => parse_to_float(s, "ms").map(|x| x * 1_000_000_f64),
187+
s => parse_to_float(s, "s").map(|x| x * 1_000_000_000_f64),
193188
}?;
194189

195190
Some((str_timing, parsed_timing))

src/template/commands/download.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::template::aoc_cli;
22
use std::process;
33

4-
pub fn download_handler(day: u8) {
4+
pub fn handle(day: u8) {
55
if aoc_cli::check().is_err() {
66
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
77
process::exit(1);
88
}
99

1010
if let Err(e) = aoc_cli::download(day) {
11-
eprintln!("failed to call aoc-cli: {}", e);
11+
eprintln!("failed to call aoc-cli: {e}");
1212
process::exit(1);
1313
};
1414
}

src/template/commands/read.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::process;
22

33
use crate::template::aoc_cli;
44

5-
pub fn read_handler(day: u8) {
5+
pub fn handle(day: u8) {
66
if aoc_cli::check().is_err() {
77
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
88
process::exit(1);
99
}
1010

1111
if let Err(e) = aoc_cli::read(day) {
12-
eprintln!("failed to call aoc-cli: {}", e);
12+
eprintln!("failed to call aoc-cli: {e}");
1313
process::exit(1);
1414
};
1515
}

src/template/commands/scaffold.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,27 @@ fn create_file(path: &str) -> Result<File, std::io::Error> {
4040
OpenOptions::new().write(true).create(true).open(path)
4141
}
4242

43-
pub fn scaffold_handler(day: u8) {
44-
let day_padded = format!("{:02}", day);
43+
pub fn handle(day: u8) {
44+
let day_padded = format!("{day:02}");
4545

46-
let input_path = format!("data/inputs/{}.txt", day_padded);
47-
let example_path = format!("data/examples/{}.txt", day_padded);
48-
let module_path = format!("src/bin/{}.rs", day_padded);
46+
let input_path = format!("data/inputs/{day_padded}.txt");
47+
let example_path = format!("data/examples/{day_padded}.txt");
48+
let module_path = format!("src/bin/{day_padded}.rs");
4949

5050
let mut file = match safe_create_file(&module_path) {
5151
Ok(file) => file,
5252
Err(e) => {
53-
eprintln!("Failed to create module file: {}", e);
53+
eprintln!("Failed to create module file: {e}");
5454
process::exit(1);
5555
}
5656
};
5757

5858
match file.write_all(MODULE_TEMPLATE.replace("DAY", &day.to_string()).as_bytes()) {
59-
Ok(_) => {
59+
Ok(()) => {
6060
println!("Created module file \"{}\"", &module_path);
6161
}
6262
Err(e) => {
63-
eprintln!("Failed to write module contents: {}", e);
63+
eprintln!("Failed to write module contents: {e}");
6464
process::exit(1);
6565
}
6666
}
@@ -70,7 +70,7 @@ pub fn scaffold_handler(day: u8) {
7070
println!("Created empty input file \"{}\"", &input_path);
7171
}
7272
Err(e) => {
73-
eprintln!("Failed to create input file: {}", e);
73+
eprintln!("Failed to create input file: {e}");
7474
process::exit(1);
7575
}
7676
}
@@ -80,7 +80,7 @@ pub fn scaffold_handler(day: u8) {
8080
println!("Created empty example file \"{}\"", &example_path);
8181
}
8282
Err(e) => {
83-
eprintln!("Failed to create example file: {}", e);
83+
eprintln!("Failed to create example file: {e}");
8484
process::exit(1);
8585
}
8686
}

0 commit comments

Comments
 (0)