/
scaffold.rs
69 lines (59 loc) 路 1.82 KB
/
scaffold.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::{
fs::{File, OpenOptions},
io::Write,
process,
};
use crate::template::Day;
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)
}
fn create_file(path: &str) -> Result<File, std::io::Error> {
OpenOptions::new().write(true).create(true).open(path)
}
pub fn handle(day: Day) {
let input_path = format!("data/inputs/{day}.txt");
let example_path = format!("data/examples/{day}.txt");
let module_path = format!("src/bin/{day}.rs");
let mut file = match safe_create_file(&module_path) {
Ok(file) => file,
Err(e) => {
eprintln!("Failed to create module file: {e}");
process::exit(1);
}
};
match file.write_all(
MODULE_TEMPLATE
.replace("%DAY_NUMBER%", &day.into_inner().to_string())
.as_bytes(),
) {
Ok(()) => {
println!("Created module file \"{}\"", &module_path);
}
Err(e) => {
eprintln!("Failed to write module contents: {e}");
process::exit(1);
}
}
match create_file(&input_path) {
Ok(_) => {
println!("Created empty input file \"{}\"", &input_path);
}
Err(e) => {
eprintln!("Failed to create input file: {e}");
process::exit(1);
}
}
match create_file(&example_path) {
Ok(_) => {
println!("Created empty example file \"{}\"", &example_path);
}
Err(e) => {
eprintln!("Failed to create example file: {e}");
process::exit(1);
}
}
println!("---");
println!("馃巹 Type `cargo solve {day}` to run your solution.");
}