@@ -5,10 +5,12 @@ extern crate serde_json;
55
66mod fetcher;
77
8+ use regex:: Regex ;
89use std:: env;
910use std:: fs;
11+ use std:: fs:: File ;
1012use std:: io;
11- use std:: io:: Write ;
13+ use std:: io:: { BufRead , Write } ;
1214use std:: path:: Path ;
1315
1416/// main() helps to generate the submission template .rs
@@ -18,30 +20,44 @@ fn main() {
1820 loop {
1921 println ! ( "Please enter a frontend problem id, or \" random\" to generate a random one." ) ;
2022 let mut is_random = false ;
23+ let mut is_solving = false ;
2124 let mut id: u32 = 0 ;
2225 let mut id_arg = String :: new ( ) ;
2326 io:: stdin ( )
2427 . read_line ( & mut id_arg)
2528 . expect ( "Failed to read line" ) ;
2629 let id_arg = id_arg. trim ( ) ;
27- match id_arg {
28- "random" => {
29- println ! ( "You select random mode." ) ;
30- id = generate_random_id ( & solved_ids) ;
31- is_random = true ;
32- println ! ( "Generate random problem: {}" , & id) ;
33- }
34- _ => {
35- id = id_arg
36- . parse :: < u32 > ( )
37- . unwrap_or_else ( |_| panic ! ( "not a number: {}" , id_arg) ) ;
38- if solved_ids. contains ( & id) {
39- println ! (
40- "The problem you chose is invalid (the problem may have been initialized \
41- or may have no rust version)."
42- ) ;
43- continue ;
44- }
30+
31+ let random_pattern = Regex :: new ( r"^random$" ) . unwrap ( ) ;
32+ let solving_pattern = Regex :: new ( r"^solve (\d+)$" ) . unwrap ( ) ;
33+
34+ if random_pattern. is_match ( id_arg) {
35+ println ! ( "You select random mode." ) ;
36+ id = generate_random_id ( & solved_ids) ;
37+ is_random = true ;
38+ println ! ( "Generate random problem: {}" , & id) ;
39+ } else if solving_pattern. is_match ( id_arg) {
40+ // solve a problem
41+ // move it from problem/ to solution/
42+ is_solving = true ;
43+ id = solving_pattern
44+ . captures ( id_arg)
45+ . unwrap ( )
46+ . get ( 1 )
47+ . unwrap ( )
48+ . as_str ( )
49+ . parse ( )
50+ . unwrap ( ) ;
51+ } else {
52+ id = id_arg
53+ . parse :: < u32 > ( )
54+ . unwrap_or_else ( |_| panic ! ( "not a number: {}" , id_arg) ) ;
55+ if solved_ids. contains ( & id) {
56+ println ! (
57+ "The problem you chose is invalid (the problem may have been initialized \
58+ or may have no rust version)."
59+ ) ;
60+ continue ;
4561 }
4662 }
4763
@@ -66,6 +82,40 @@ fn main() {
6682 problem. title_slug. replace( "-" , "_" )
6783 ) ;
6884 let file_path = Path :: new ( "./src/problem" ) . join ( format ! ( "{}.rs" , file_name) ) ;
85+ if is_solving {
86+ // check problem/ existence
87+ if !file_path. exists ( ) {
88+ panic ! ( "problem does not exist" ) ;
89+ }
90+ // check solution/ no existence
91+ let solution_name = format ! (
92+ "s{:04}_{}" ,
93+ problem. question_id,
94+ problem. title_slug. replace( "-" , "_" )
95+ ) ;
96+ let solution_path = Path :: new ( "./src/solution" ) . join ( format ! ( "{}.rs" , solution_name) ) ;
97+ if solution_path. exists ( ) {
98+ panic ! ( "solution exists" ) ;
99+ }
100+ // rename/move file
101+ fs:: rename ( file_path, solution_path) . unwrap ( ) ;
102+ // remove from problem/mod.rs
103+ let mod_file = "./src/problem/mod.rs" ;
104+ let target_line = format ! ( "mod {};" , file_name) ;
105+ let lines: Vec < String > = io:: BufReader :: new ( File :: open ( mod_file) . unwrap ( ) )
106+ . lines ( )
107+ . map ( |x| x. unwrap ( ) )
108+ . filter ( |x| * x != target_line)
109+ . collect ( ) ;
110+ fs:: write ( mod_file, lines. join ( "\n " ) ) ;
111+ // insert into solution/mod.rs
112+ let mut lib_file = fs:: OpenOptions :: new ( )
113+ . append ( true )
114+ . open ( "./src/solution/mod.rs" )
115+ . unwrap ( ) ;
116+ writeln ! ( lib_file, "mod {};" , solution_name) ;
117+ break ;
118+ }
69119 if file_path. exists ( ) {
70120 panic ! ( "problem already initialized" ) ;
71121 }
0 commit comments