Skip to content

Commit

Permalink
refactor: extract template to its own file (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
tguichaoua authored Dec 6, 2023
1 parent b71cbdd commit a79ce7a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
26 changes: 26 additions & 0 deletions src/template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
advent_of_code::solution!(%DAY_NUMBER%);

pub fn part_one(input: &str) -> Option<u32> {
None
}

pub fn part_two(input: &str) -> Option<u32> {
None
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}
31 changes: 3 additions & 28 deletions src/template/commands/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,8 @@ use std::{

use crate::template::Day;

const MODULE_TEMPLATE: &str = r#"advent_of_code::solution!(DAY_NUMBER);
pub fn part_one(input: &str) -> Option<u32> {
None
}
pub fn part_two(input: &str) -> Option<u32> {
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}
"#;
const MODULE_TEMPLATE: &str =
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt"));

fn safe_create_file(path: &str) -> Result<File, std::io::Error> {
OpenOptions::new().write(true).create_new(true).open(path)
Expand All @@ -57,7 +32,7 @@ pub fn handle(day: Day) {

match file.write_all(
MODULE_TEMPLATE
.replace("DAY_NUMBER", &day.into_inner().to_string())
.replace("%DAY_NUMBER%", &day.into_inner().to_string())
.as_bytes(),
) {
Ok(()) => {
Expand Down

0 comments on commit a79ce7a

Please sign in to comment.