1- use std:: collections:: HashMap ;
21use std:: fs:: File ;
32use std:: io:: Read ;
43use std:: path:: Path ;
4+ use std:: { collections:: HashMap , str:: FromStr } ;
55
6+ use log:: warn;
67use serde:: { de:: DeserializeOwned , Deserialize } ;
78
8- use crate :: { LeetUpError , Result } ;
9+ use crate :: { service :: Lang , LeetUpError , Result } ;
910
1011type LangInjectCode = HashMap < String , InjectCode > ;
1112type PickHookConfig = HashMap < String , PickHook > ;
@@ -16,6 +17,7 @@ pub struct Config {
1617 pub urls : Urls ,
1718 pub inject_code : Option < LangInjectCode > ,
1819 pub pick_hook : Option < PickHookConfig > ,
20+ pub lang : Lang ,
1921}
2022
2123impl Config {
@@ -37,19 +39,23 @@ impl Config {
3739 verify : format ! ( "{}/submissions/detail/$id/check/" , base) ,
3840 } ;
3941
40- let mut config: Result < Config > = Config :: get_config ( path) ;
41-
42- if let Ok ( ref mut config) = config {
43- config. urls = urls. clone ( ) ;
42+ let config: Result < Config > = Config :: get_config ( path) ;
43+
44+ match config {
45+ Ok ( mut c) => {
46+ c. urls = urls. clone ( ) ;
47+ c
48+ }
49+ Err ( e) => {
50+ warn ! ( "{:#?}" , e) ;
51+ Config {
52+ urls,
53+ inject_code : None ,
54+ pick_hook : None ,
55+ lang : Lang :: from_str ( "rust" ) . unwrap ( ) ,
56+ }
57+ }
4458 }
45-
46- let config = config. unwrap_or ( Config {
47- urls,
48- inject_code : None ,
49- pick_hook : None ,
50- } ) ;
51-
52- config
5359 }
5460
5561 fn get_config < P : AsRef < Path > , T : DeserializeOwned > ( path : P ) -> Result < T > {
@@ -159,7 +165,8 @@ fn test_config() {
159165 "urls" : {
160166 "base" : vec![ "" ]
161167 } ,
162- "pick_hook" : { }
168+ "pick_hook" : { } ,
169+ "lang" : "java"
163170 } ) ;
164171 let file_path = data_dir. path ( ) . join ( "config.json" ) ;
165172
@@ -170,6 +177,7 @@ fn test_config() {
170177 assert ! ( config. inject_code. is_some( ) ) ;
171178 assert ! ( !config. urls. base. is_empty( ) ) ;
172179 assert ! ( config. pick_hook. is_some( ) ) ;
180+ assert ! ( matches!( config. lang, Lang :: Java ( ..) ) ) ;
173181 drop ( file) ;
174182 data_dir. close ( ) . unwrap ( ) ;
175183}
0 commit comments