Skip to content

Commit 77a6acc

Browse files
committed
extract problem dealing logic
1 parent b3d28ec commit 77a6acc

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

src/main.rs

+42-38
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate serde_json;
55

66
mod fetcher;
77

8+
use crate::fetcher::{CodeDefinition, Problem};
89
use regex::Regex;
910
use std::env;
1011
use std::fs;
@@ -74,44 +75,7 @@ fn main() {
7475
continue;
7576
}
7677
let code = code.unwrap();
77-
78-
let file_name = format!(
79-
"p{:04}_{}",
80-
problem.question_id,
81-
problem.title_slug.replace("-", "_")
82-
);
83-
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
84-
if file_path.exists() {
85-
panic!("problem already initialized");
86-
}
87-
88-
let template = fs::read_to_string("./template.rs").unwrap();
89-
let source = template
90-
.replace("__PROBLEM_TITLE__", &problem.title)
91-
.replace("__PROBLEM_DESC__", &build_desc(&problem.content))
92-
.replace(
93-
"__PROBLEM_DEFAULT_CODE__",
94-
&insert_return_in_code(&problem.return_type, &code.default_code),
95-
)
96-
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
97-
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
98-
99-
let mut file = fs::OpenOptions::new()
100-
.write(true)
101-
.create(true)
102-
.truncate(true)
103-
.open(&file_path)
104-
.unwrap();
105-
106-
file.write_all(source.as_bytes()).unwrap();
107-
drop(file);
108-
109-
let mut lib_file = fs::OpenOptions::new()
110-
.write(true)
111-
.append(true)
112-
.open("./src/problem/mod.rs")
113-
.unwrap();
114-
writeln!(lib_file, "mod {};", file_name);
78+
deal_problem(&problem, &code);
11579
break;
11680
}
11781
}
@@ -274,3 +238,43 @@ fn deal_solving(id: &u32) {
274238
.unwrap();
275239
writeln!(lib_file, "mod {};", solution_name);
276240
}
241+
242+
fn deal_problem(problem: &Problem, code: &CodeDefinition) {
243+
let file_name = format!(
244+
"p{:04}_{}",
245+
problem.question_id,
246+
problem.title_slug.replace("-", "_")
247+
);
248+
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
249+
if file_path.exists() {
250+
panic!("problem already initialized");
251+
}
252+
253+
let template = fs::read_to_string("./template.rs").unwrap();
254+
let source = template
255+
.replace("__PROBLEM_TITLE__", &problem.title)
256+
.replace("__PROBLEM_DESC__", &build_desc(&problem.content))
257+
.replace(
258+
"__PROBLEM_DEFAULT_CODE__",
259+
&insert_return_in_code(&problem.return_type, &code.default_code),
260+
)
261+
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
262+
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
263+
264+
let mut file = fs::OpenOptions::new()
265+
.write(true)
266+
.create(true)
267+
.truncate(true)
268+
.open(&file_path)
269+
.unwrap();
270+
271+
file.write_all(source.as_bytes()).unwrap();
272+
drop(file);
273+
274+
let mut lib_file = fs::OpenOptions::new()
275+
.write(true)
276+
.append(true)
277+
.open("./src/problem/mod.rs")
278+
.unwrap();
279+
writeln!(lib_file, "mod {};", file_name);
280+
}

0 commit comments

Comments
 (0)