@@ -48,6 +48,7 @@ fn main() {
48
48
. as_str ( )
49
49
. parse ( )
50
50
. unwrap ( ) ;
51
+ deal_solving ( & id) ;
51
52
} else {
52
53
id = id_arg
53
54
. parse :: < u32 > ( )
@@ -79,40 +80,6 @@ fn main() {
79
80
problem. title_slug. replace( "-" , "_" )
80
81
) ;
81
82
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
- }
116
83
if file_path. exists ( ) {
117
84
panic ! ( "problem already initialized" ) ;
118
85
}
@@ -265,3 +232,44 @@ fn build_desc(content: &str) -> String {
265
232
. replace ( "\n \n " , "\n " )
266
233
. replace ( "\n " , "\n * " )
267
234
}
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