6
6
use self :: models:: * ;
7
7
use self :: schemas:: { problems:: dsl:: * , tags:: dsl:: * } ;
8
8
use self :: sql:: * ;
9
+ use crate :: cmds:: { CODE_END , CODE_START } ;
10
+ use crate :: helper:: test_cases_path;
9
11
use crate :: { cfg, err:: Error , plugins:: LeetCode } ;
10
12
use colored:: Colorize ;
11
13
use diesel:: prelude:: * ;
@@ -26,7 +28,7 @@ pub enum Run {
26
28
Submit ,
27
29
}
28
30
29
- impl std :: default :: Default for Run {
31
+ impl Default for Run {
30
32
fn default ( ) -> Self {
31
33
Run :: Submit
32
34
}
@@ -37,7 +39,7 @@ impl std::default::Default for Run {
37
39
pub struct Cache ( pub LeetCode ) ;
38
40
39
41
impl Cache {
40
- /// Ref to sqliteconnection
42
+ /// Ref to sqlite connection
41
43
fn conn ( & self ) -> Result < SqliteConnection , Error > {
42
44
Ok ( conn ( self . 0 . conf . storage . cache ( ) ?) )
43
45
}
@@ -236,10 +238,10 @@ impl Cache {
236
238
& self ,
237
239
run : Run ,
238
240
rfid : i32 ,
239
- testcase : Option < String > ,
241
+ test_case : Option < String > ,
240
242
) -> Result < ( HashMap < & ' static str , String > , [ String ; 2 ] ) , Error > {
241
243
trace ! ( "pre run code..." ) ;
242
- use crate :: helper:: { code_path, test_cases_path } ;
244
+ use crate :: helper:: code_path;
243
245
use std:: fs:: File ;
244
246
use std:: io:: Read ;
245
247
@@ -256,30 +258,48 @@ impl Cache {
256
258
let mut code: String = "" . to_string ( ) ;
257
259
258
260
let maybe_file_testcases: Option < String > = test_cases_path ( & p)
259
- . map ( |filename | {
261
+ . map ( |file_name | {
260
262
let mut tests = "" . to_string ( ) ;
261
- File :: open ( filename )
263
+ File :: open ( file_name )
262
264
. and_then ( |mut file_descriptor| file_descriptor. read_to_string ( & mut tests) )
263
265
. map ( |_| Some ( tests) )
264
266
. unwrap_or ( None )
265
267
} )
266
268
. unwrap_or ( None ) ;
267
269
270
+ let maybe_all_testcases: Option < String > = if d. all_cases . is_empty ( ) {
271
+ None
272
+ } else {
273
+ Some ( d. all_cases . to_string ( ) )
274
+ } ;
275
+
268
276
// Takes test cases using following priority
269
277
// 1. cli parameter
270
- // 2. test cases from the file
271
- // 3. sample test case from the task
272
- let testcase = testcase. or ( maybe_file_testcases) . unwrap_or ( d. case ) ;
278
+ // 2. if test cases file exist, use the file test cases(user can edit it)
279
+ // 3. test cases from problem desc all test cases
280
+ // 4. sample test case from the task
281
+ let test_case = test_case
282
+ . or ( maybe_file_testcases)
283
+ . or ( maybe_all_testcases)
284
+ . unwrap_or ( d. case ) ;
273
285
274
286
File :: open ( code_path ( & p, None ) ?) ?. read_to_string ( & mut code) ?;
275
287
288
+ let begin = code. find ( CODE_START ) . unwrap_or ( 0 ) ;
289
+ let end = code. find ( CODE_END ) . unwrap_or ( code. len ( ) ) ;
290
+ let code = if let Some ( solution) = code. get ( begin..end) {
291
+ solution. to_string ( )
292
+ } else {
293
+ code
294
+ } ;
295
+
276
296
json. insert ( "lang" , conf. code . lang . to_string ( ) ) ;
277
297
json. insert ( "question_id" , p. id . to_string ( ) ) ;
278
298
json. insert ( "typed_code" , code) ;
279
299
280
300
// pass manually data
281
301
json. insert ( "name" , p. name . to_string ( ) ) ;
282
- json. insert ( "data_input" , testcase ) ;
302
+ json. insert ( "data_input" , test_case ) ;
283
303
284
304
let url = match run {
285
305
Run :: Test => conf
@@ -315,7 +335,7 @@ impl Cache {
315
335
async fn recur_verify ( & self , rid : String ) -> Result < VerifyResult , Error > {
316
336
use std:: time:: Duration ;
317
337
318
- trace ! ( "Run veriy recursion..." ) ;
338
+ trace ! ( "Run verify recursion..." ) ;
319
339
std:: thread:: sleep ( Duration :: from_micros ( 3000 ) ) ;
320
340
321
341
let json: VerifyResult = self
@@ -330,10 +350,10 @@ impl Cache {
330
350
& self ,
331
351
rfid : i32 ,
332
352
run : Run ,
333
- testcase : Option < String > ,
353
+ test_case : Option < String > ,
334
354
) -> Result < VerifyResult , Error > {
335
355
trace ! ( "Exec problem filter —— Test or Submit" ) ;
336
- let ( json, [ url, refer] ) = self . pre_run_code ( run. clone ( ) , rfid, testcase ) . await ?;
356
+ let ( json, [ url, refer] ) = self . pre_run_code ( run. clone ( ) , rfid, test_case ) . await ?;
337
357
trace ! ( "Pre run code result {:?}, {:?}, {:?}" , json, url, refer) ;
338
358
339
359
let run_res: RunCode = self
0 commit comments