|
1 | | -// make cargo happy |
2 | | -pub mod n1_two_sum; |
3 | | -pub mod n2_add_two_numbers; |
| 1 | +#[macro_use] |
| 2 | +extern crate serde_derive; |
| 3 | +#[macro_use] |
| 4 | +extern crate serde_json; |
4 | 5 |
|
| 6 | +mod problem; |
| 7 | + |
| 8 | +use std::env; |
| 9 | +use std::fs; |
| 10 | +use std::path::{Path}; |
| 11 | +use std::io::Write; |
| 12 | + |
| 13 | +/// main() helps to generate the submission template .rs |
5 | 14 | fn main() { |
| 15 | + let args: Vec<String> = env::args().collect(); |
| 16 | + if args.len() < 2 { |
| 17 | + panic!("problem id must be provided"); |
| 18 | + } |
| 19 | + let id = &args[1]; |
| 20 | + let id = id.parse::<u32>().expect(&format!("not a number: {}", id)); |
| 21 | + |
| 22 | + let problem = problem::get_problem(id) |
| 23 | + .expect(&format!("problem #{} not found", id)); |
| 24 | + let code = problem.code_definition.iter() |
| 25 | + .filter(|&d| { d.value == "rust" }) |
| 26 | + .next() |
| 27 | + .expect("problem has no rust support yet"); |
| 28 | + |
| 29 | + let file_name = format!("n{:04}_{}", id, problem.title_slug.replace("-", "_")); |
| 30 | + let file_path = Path::new("./src").join(format!("{}.rs", file_name)); |
| 31 | + if file_path.exists() { |
| 32 | + panic!("problem already initialized"); |
| 33 | + } |
| 34 | + |
| 35 | + let template = fs::read_to_string("./template.rs").unwrap(); |
| 36 | + let source = template |
| 37 | + .replace("__PROBLEM_TITLE__", &problem.title) |
| 38 | + .replace("__PROBLEM_DESC__", &build_desc(&problem.content)) |
| 39 | + .replace("__PROBLEM_DEFAULT_CODE__", &code.default_code) |
| 40 | + .replace("__PROBLEM_ID__", &format!("{}", id)); |
| 41 | + |
| 42 | + let mut file = fs::OpenOptions::new() |
| 43 | + .write(true) |
| 44 | + .create(true) |
| 45 | + .truncate(true) |
| 46 | + .open(&file_path) |
| 47 | + .unwrap(); |
| 48 | + |
| 49 | + file.write_all(source.as_bytes()).unwrap(); |
| 50 | + drop(file); |
| 51 | + |
| 52 | + let mut lib_file = fs::OpenOptions::new() |
| 53 | + .write(true) |
| 54 | + .append(true) |
| 55 | + .open("./src/lib.rs") |
| 56 | + .unwrap(); |
| 57 | + writeln!(lib_file, "mod {};", file_name); |
| 58 | +} |
6 | 59 |
|
| 60 | +fn build_desc(content: &str) -> String { |
| 61 | + content |
| 62 | + .replace("<strong>", "") |
| 63 | + .replace("</strong>", "") |
| 64 | + .replace("<em>", "") |
| 65 | + .replace("</em>", "") |
| 66 | + .replace("</p>", "") |
| 67 | + .replace("<p>", "") |
| 68 | + .replace("<b>", "") |
| 69 | + .replace("</b>", "") |
| 70 | + .replace("</pre>", "") |
| 71 | + .replace("<pre>", "") |
| 72 | + .replace(" ", "") |
| 73 | + .replace("\n\n", "\n") |
| 74 | + .replace("\n", "\n * ") |
7 | 75 | } |
0 commit comments