Skip to content

Commit bc6fb49

Browse files
committed
extract solving logic
1 parent 955822d commit bc6fb49

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

src/main.rs

+42-34
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn main() {
4848
.as_str()
4949
.parse()
5050
.unwrap();
51+
deal_solving(&id);
5152
} else {
5253
id = id_arg
5354
.parse::<u32>()
@@ -79,40 +80,6 @@ fn main() {
7980
problem.title_slug.replace("-", "_")
8081
);
8182
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
82-
if is_solving {
83-
// check problem/ existence
84-
if !file_path.exists() {
85-
panic!("problem does not exist");
86-
}
87-
// check solution/ no existence
88-
let solution_name = format!(
89-
"s{:04}_{}",
90-
problem.question_id,
91-
problem.title_slug.replace("-", "_")
92-
);
93-
let solution_path = Path::new("./src/solution").join(format!("{}.rs", solution_name));
94-
if solution_path.exists() {
95-
panic!("solution exists");
96-
}
97-
// rename/move file
98-
fs::rename(file_path, solution_path).unwrap();
99-
// remove from problem/mod.rs
100-
let mod_file = "./src/problem/mod.rs";
101-
let target_line = format!("mod {};", file_name);
102-
let lines: Vec<String> = io::BufReader::new(File::open(mod_file).unwrap())
103-
.lines()
104-
.map(|x| x.unwrap())
105-
.filter(|x| *x != target_line)
106-
.collect();
107-
fs::write(mod_file, lines.join("\n"));
108-
// insert into solution/mod.rs
109-
let mut lib_file = fs::OpenOptions::new()
110-
.append(true)
111-
.open("./src/solution/mod.rs")
112-
.unwrap();
113-
writeln!(lib_file, "mod {};", solution_name);
114-
break;
115-
}
11683
if file_path.exists() {
11784
panic!("problem already initialized");
11885
}
@@ -265,3 +232,44 @@ fn build_desc(content: &str) -> String {
265232
.replace("\n\n", "\n")
266233
.replace("\n", "\n * ")
267234
}
235+
236+
fn deal_solving(id: &u32) {
237+
let problem = fetcher::get_problem(*id).unwrap();
238+
let file_name = format!(
239+
"p{:04}_{}",
240+
problem.question_id,
241+
problem.title_slug.replace("-", "_")
242+
);
243+
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
244+
// check problem/ existence
245+
if !file_path.exists() {
246+
panic!("problem does not exist");
247+
}
248+
// check solution/ no existence
249+
let solution_name = format!(
250+
"s{:04}_{}",
251+
problem.question_id,
252+
problem.title_slug.replace("-", "_")
253+
);
254+
let solution_path = Path::new("./src/solution").join(format!("{}.rs", solution_name));
255+
if solution_path.exists() {
256+
panic!("solution exists");
257+
}
258+
// rename/move file
259+
fs::rename(file_path, solution_path).unwrap();
260+
// remove from problem/mod.rs
261+
let mod_file = "./src/problem/mod.rs";
262+
let target_line = format!("mod {};", file_name);
263+
let lines: Vec<String> = io::BufReader::new(File::open(mod_file).unwrap())
264+
.lines()
265+
.map(|x| x.unwrap())
266+
.filter(|x| *x != target_line)
267+
.collect();
268+
fs::write(mod_file, lines.join("\n"));
269+
// insert into solution/mod.rs
270+
let mut lib_file = fs::OpenOptions::new()
271+
.append(true)
272+
.open("./src/solution/mod.rs")
273+
.unwrap();
274+
writeln!(lib_file, "mod {};", solution_name);
275+
}

0 commit comments

Comments
 (0)