@@ -7,20 +7,21 @@ mod problem;
7
7
8
8
use std:: env;
9
9
use std:: fs;
10
- use std:: path:: { Path } ;
11
- use std:: io:: Write ;
12
10
use std:: io;
11
+ use std:: io:: Write ;
12
+ use std:: path:: Path ;
13
13
14
14
/// main() helps to generate the submission template .rs
15
15
fn main ( ) {
16
16
println ! ( "Welcome to leetcode-rust system." ) ;
17
17
let mut solved_ids = get_solved_ids ( ) ;
18
18
loop {
19
- println ! ( "Please enter a problem id, or enter \" random\" to generate a random problem ." ) ;
19
+ println ! ( "Please enter a frontend problem id, or \" random\" to generate a random one ." ) ;
20
20
let mut is_random = false ;
21
- let mut id : u32 = 0 ;
21
+ let mut id: u32 = 0 ;
22
22
let mut id_arg = String :: new ( ) ;
23
- io:: stdin ( ) . read_line ( & mut id_arg)
23
+ io:: stdin ( )
24
+ . read_line ( & mut id_arg)
24
25
. expect ( "Failed to read line" ) ;
25
26
let id_arg = id_arg. trim ( ) ;
26
27
match id_arg {
@@ -29,23 +30,30 @@ fn main() {
29
30
id = generate_random_id ( & solved_ids) ;
30
31
is_random = true ;
31
32
println ! ( "Generate random problem: {}" , & id) ;
32
- } ,
33
+ }
33
34
_ => {
34
- id = id_arg. parse :: < u32 > ( ) . expect ( & format ! ( "not a number: {}" , id_arg) ) ;
35
+ id = id_arg
36
+ . parse :: < u32 > ( )
37
+ . expect ( & format ! ( "not a number: {}" , id_arg) ) ;
35
38
if solved_ids. contains ( & id) {
36
- println ! ( "The problem you chose is invalid (the problem may have been solved \
37
- or may have no rust version).") ;
39
+ println ! (
40
+ "The problem you chose is invalid (the problem may have been solved \
41
+ or may have no rust version)."
42
+ ) ;
38
43
continue ;
39
44
}
40
45
}
41
46
}
42
47
43
- let problem = problem:: get_problem ( id)
44
- . expect ( & format ! ( "Error: failed to get problem #{} \
45
- (The problem may be paid-only or may not be exist).",
46
- id) ) ;
47
- let code = problem. code_definition . iter ( )
48
- . filter ( |& d| { d. value == "rust" } )
48
+ let problem = problem:: get_problem ( id) . expect ( & format ! (
49
+ "Error: failed to get problem #{} \
50
+ (The problem may be paid-only or may not be exist).",
51
+ id
52
+ ) ) ;
53
+ let code = problem
54
+ . code_definition
55
+ . iter ( )
56
+ . filter ( |& d| d. value == "rust" )
49
57
. next ( ) ;
50
58
if code. is_none ( ) {
51
59
println ! ( "Problem {} has no rust version." , & id) ;
@@ -54,7 +62,11 @@ fn main() {
54
62
}
55
63
let code = code. unwrap ( ) ;
56
64
57
- let file_name = format ! ( "n{:04}_{}" , problem. question_id, problem. title_slug. replace( "-" , "_" ) ) ;
65
+ let file_name = format ! (
66
+ "n{:04}_{}" ,
67
+ problem. question_id,
68
+ problem. title_slug. replace( "-" , "_" )
69
+ ) ;
58
70
let file_path = Path :: new ( "./src" ) . join ( format ! ( "{}.rs" , file_name) ) ;
59
71
if file_path. exists ( ) {
60
72
panic ! ( "problem already initialized" ) ;
@@ -88,17 +100,20 @@ fn main() {
88
100
}
89
101
}
90
102
91
- fn generate_random_id ( except_ids : & Vec < u32 > ) -> u32 {
92
- use std:: fs;
103
+ fn generate_random_id ( except_ids : & Vec < u32 > ) -> u32 {
93
104
use rand:: Rng ;
105
+ use std:: fs;
94
106
let mut rng = rand:: thread_rng ( ) ;
95
107
loop {
96
- let res : u32 = rng. gen_range ( 1 , 1106 ) ;
108
+ let res: u32 = rng. gen_range ( 1 , 1106 ) ;
97
109
if !except_ids. contains ( & res) {
98
110
return res;
99
111
}
100
- println ! ( "Generate a random num ({}), but it is invalid (the problem may have been solved \
101
- or may have no rust version). Regenerate..", res) ;
112
+ println ! (
113
+ "Generate a random num ({}), but it is invalid (the problem may have been solved \
114
+ or may have no rust version). Regenerate..",
115
+ res
116
+ ) ;
102
117
}
103
118
}
104
119
0 commit comments