update code template
This commit is contained in:
parent
ecbe5f281a
commit
e60222a207
3 changed files with 38 additions and 13 deletions
25
src/day.rs
25
src/day.rs
|
@ -27,8 +27,9 @@ impl Day {
|
||||||
Some(Self(day))
|
Some(Self(day))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_unchecked(day: u8) -> Self {
|
// Not part of the public API
|
||||||
debug_assert!(day != 0 && day <= 25);
|
#[doc(hidden)]
|
||||||
|
pub const fn __new_unchecked(day: u8) -> Self {
|
||||||
Self(day)
|
Self(day)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +100,7 @@ impl Iterator for EveryDay {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
// NOTE: the iterator start at 1 and we have verified that the value is not above 25.
|
// NOTE: the iterator start at 1 and we have verified that the value is not above 25.
|
||||||
let day = Day::new_unchecked(self.current);
|
let day = Day(self.current);
|
||||||
self.current += 1;
|
self.current += 1;
|
||||||
|
|
||||||
Some(day)
|
Some(day)
|
||||||
|
@ -108,6 +109,24 @@ impl Iterator for EveryDay {
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/// Crates a [`Day`] value in a const context.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! day {
|
||||||
|
($day:expr) => {{
|
||||||
|
const _ASSERT: () = assert!(
|
||||||
|
$day != 0 && $day <= 25,
|
||||||
|
concat!(
|
||||||
|
"invalid day number `",
|
||||||
|
$day,
|
||||||
|
"`, expecting a value between 1 and 25"
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$crate::Day::__new_unchecked($day)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
#[cfg(feature = "test_lib")]
|
#[cfg(feature = "test_lib")]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{every_day, Day};
|
use super::{every_day, Day};
|
||||||
|
|
|
@ -14,7 +14,7 @@ pub fn part_two(input: &str) -> Option<u32> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
advent_of_code::main!(DAY);
|
advent_of_code::main!(DAY_NUMBER);
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -55,7 +55,11 @@ pub fn handle(day: Day) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match file.write_all(MODULE_TEMPLATE.replace("DAY", &day.to_string()).as_bytes()) {
|
match file.write_all(
|
||||||
|
MODULE_TEMPLATE
|
||||||
|
.replace("DAY_NUMBER", &day.into_inner().to_string())
|
||||||
|
.as_bytes(),
|
||||||
|
) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
println!("Created module file \"{}\"", &module_path);
|
println!("Created module file \"{}\"", &module_path);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::Day;
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
|
||||||
pub mod aoc_cli;
|
pub mod aoc_cli;
|
||||||
|
@ -10,12 +11,10 @@ pub const ANSI_BOLD: &str = "\x1b[1m";
|
||||||
pub const ANSI_RESET: &str = "\x1b[0m";
|
pub const ANSI_RESET: &str = "\x1b[0m";
|
||||||
|
|
||||||
/// Helper function that reads a text file to a string.
|
/// Helper function that reads a text file to a string.
|
||||||
#[must_use] pub fn read_file(folder: &str, day: u8) -> String {
|
#[must_use]
|
||||||
|
pub fn read_file(folder: &str, day: Day) -> String {
|
||||||
let cwd = env::current_dir().unwrap();
|
let cwd = env::current_dir().unwrap();
|
||||||
let filepath = cwd
|
let filepath = cwd.join("data").join(folder).join(format!("{day}.txt"));
|
||||||
.join("data")
|
|
||||||
.join(folder)
|
|
||||||
.join(format!("{day:02}.txt"));
|
|
||||||
let f = fs::read_to_string(filepath);
|
let f = fs::read_to_string(filepath);
|
||||||
f.expect("could not open input file")
|
f.expect("could not open input file")
|
||||||
}
|
}
|
||||||
|
@ -24,11 +23,14 @@ pub const ANSI_RESET: &str = "\x1b[0m";
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! main {
|
macro_rules! main {
|
||||||
($day:expr) => {
|
($day:expr) => {
|
||||||
|
/// The current day.
|
||||||
|
const DAY: advent_of_code::Day = advent_of_code::day!($day);
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use advent_of_code::template::runner::*;
|
use advent_of_code::template::runner::*;
|
||||||
let input = advent_of_code::template::read_file("inputs", $day);
|
let input = advent_of_code::template::read_file("inputs", DAY);
|
||||||
run_part(part_one, &input, $day, 1);
|
run_part(part_one, &input, DAY, 1);
|
||||||
run_part(part_two, &input, $day, 2);
|
run_part(part_two, &input, DAY, 2);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue