66use self :: models:: * ;
77use self :: schemas:: { problems:: dsl:: * , tags:: dsl:: * } ;
88use self :: sql:: * ;
9+ use crate :: cmds:: { CODE_END , CODE_START } ;
10+ use crate :: helper:: test_cases_path;
911use crate :: { cfg, err:: Error , plugins:: LeetCode } ;
1012use colored:: Colorize ;
1113use diesel:: prelude:: * ;
@@ -26,7 +28,7 @@ pub enum Run {
2628 Submit ,
2729}
2830
29- impl std :: default :: Default for Run {
31+ impl Default for Run {
3032 fn default ( ) -> Self {
3133 Run :: Submit
3234 }
@@ -37,7 +39,7 @@ impl std::default::Default for Run {
3739pub struct Cache ( pub LeetCode ) ;
3840
3941impl Cache {
40- /// Ref to sqliteconnection
42+ /// Ref to sqlite connection
4143 fn conn ( & self ) -> Result < SqliteConnection , Error > {
4244 Ok ( conn ( self . 0 . conf . storage . cache ( ) ?) )
4345 }
@@ -236,10 +238,10 @@ impl Cache {
236238 & self ,
237239 run : Run ,
238240 rfid : i32 ,
239- testcase : Option < String > ,
241+ test_case : Option < String > ,
240242 ) -> Result < ( HashMap < & ' static str , String > , [ String ; 2 ] ) , Error > {
241243 trace ! ( "pre run code..." ) ;
242- use crate :: helper:: { code_path, test_cases_path } ;
244+ use crate :: helper:: code_path;
243245 use std:: fs:: File ;
244246 use std:: io:: Read ;
245247
@@ -256,30 +258,48 @@ impl Cache {
256258 let mut code: String = "" . to_string ( ) ;
257259
258260 let maybe_file_testcases: Option < String > = test_cases_path ( & p)
259- . map ( |filename | {
261+ . map ( |file_name | {
260262 let mut tests = "" . to_string ( ) ;
261- File :: open ( filename )
263+ File :: open ( file_name )
262264 . and_then ( |mut file_descriptor| file_descriptor. read_to_string ( & mut tests) )
263265 . map ( |_| Some ( tests) )
264266 . unwrap_or ( None )
265267 } )
266268 . unwrap_or ( None ) ;
267269
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+
268276 // Takes test cases using following priority
269277 // 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 ) ;
273285
274286 File :: open ( code_path ( & p, None ) ?) ?. read_to_string ( & mut code) ?;
275287
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+
276296 json. insert ( "lang" , conf. code . lang . to_string ( ) ) ;
277297 json. insert ( "question_id" , p. id . to_string ( ) ) ;
278298 json. insert ( "typed_code" , code) ;
279299
280300 // pass manually data
281301 json. insert ( "name" , p. name . to_string ( ) ) ;
282- json. insert ( "data_input" , testcase ) ;
302+ json. insert ( "data_input" , test_case ) ;
283303
284304 let url = match run {
285305 Run :: Test => conf
@@ -315,7 +335,7 @@ impl Cache {
315335 async fn recur_verify ( & self , rid : String ) -> Result < VerifyResult , Error > {
316336 use std:: time:: Duration ;
317337
318- trace ! ( "Run veriy recursion..." ) ;
338+ trace ! ( "Run verify recursion..." ) ;
319339 std:: thread:: sleep ( Duration :: from_micros ( 3000 ) ) ;
320340
321341 let json: VerifyResult = self
@@ -330,10 +350,10 @@ impl Cache {
330350 & self ,
331351 rfid : i32 ,
332352 run : Run ,
333- testcase : Option < String > ,
353+ test_case : Option < String > ,
334354 ) -> Result < VerifyResult , Error > {
335355 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 ?;
337357 trace ! ( "Pre run code result {:?}, {:?}, {:?}" , json, url, refer) ;
338358
339359 let run_res: RunCode = self
0 commit comments