use std::{
fs::{self, File, OpenOptions},
io::{BufRead, BufReader, Read, Write},
path::PathBuf,
process,
};
use clap::Parser;
#[derive(Parser)]
struct Cli {
platform: String,
sn: String,
}
fn main() {
if !check_rust_src() {
eprintln!("当前目录不是rust项目");
return;
}
let args = Cli::parse();
let mut path = PathBuf::new();
let mut template = Vec::new();
let mut full_name = String::new();
let mut mod_file_path = PathBuf::from("src");
path.push("src");
match args.platform.as_str() {
"lc" => {
path.push("leetcode");
mod_file_path.push("leetcode");
full_name = "leetcode".to_string();
template = include_str!("template1.rs").as_bytes().to_vec();
}
"luogu" => {
path.push("luogu");
full_name = "luogu".to_string();
mod_file_path.push("luogu");
template = include_str!("template2.rs").as_bytes().to_vec();
}
"at" => {
path.push("atcoder");
full_name = "atcoder".to_string();
mod_file_path.push("atcoder");
template = include_str!("template2.rs").as_bytes().to_vec();
}
_ => {
eprintln!("这是没有预先准备的平台:{}", args.platform);
process::exit(1);
}
}
let mut file_name = full_name.clone();
file_name.push('_');
file_name.push_str(&args.sn);
file_name.push_str(".rs");
path.push(file_name.clone());
let mut mod_append_text = full_name;
mod_append_text.push('_');
mod_append_text.push_str(&args.sn);
mod_file_path.push("mod.rs");
if !check_solution_repeat(mod_file_path.clone(), mod_append_text.clone()) {
let mut mod_file = OpenOptions::new().append(true).open(mod_file_path).unwrap();
writeln!(mod_file, "\nmod {};", mod_append_text).unwrap();
} else {
eprintln!("您已经创建过这一题");
process::exit(1);
}
let mut file = File::create(path).unwrap();
file.write_all(&template).unwrap();
}
fn check_rust_src() -> bool {
for entry in fs::read_dir(".").unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.file_name().unwrap() == "Cargo.toml" {
return true;
}
}
false
}
fn check_solution_repeat(path: PathBuf, sn: String) -> bool {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
for line in reader.lines() {
let line = line.unwrap();
let words = line.split_whitespace();
match words.last() {
Some(now_sn) => {
let now_sn: String = now_sn.chars().filter(|x| *x != ';').collect();
if now_sn == sn {
return true;
}
}
None => {
continue;
}
}
}
false
}