@@ -53,14 +53,14 @@ impl Default for Config {
5353#[ derive( Debug , Clone , Serialize , Deserialize ) ]
5454pub struct GeoIpConfig {
5555 pub maxmind_db_path : Option < String > ,
56- #[ serde( deserialize_with = "deserialize_option_string_from_number " ) ]
56+ #[ serde( default , deserialize_with = "deserialize_string_from_number " ) ]
5757 pub maxmind_account_id : Option < String > ,
5858 pub maxmind_license_key : Option < String > ,
5959 #[ serde( default = "default_maxmind_edition" ) ]
6060 pub maxmind_edition : Option < String > ,
6161}
6262
63- pub fn deserialize_option_string_from_number < ' de , D > ( deserializer : D ) -> Result < Option < String > , D :: Error >
63+ pub fn deserialize_string_from_number < ' de , D > ( deserializer : D ) -> Result < Option < String > , D :: Error >
6464where
6565 D : serde:: Deserializer < ' de > ,
6666{
@@ -72,11 +72,10 @@ where
7272 Float ( f64 ) ,
7373 }
7474
75- match Option :: < StringOrNumber > :: deserialize ( deserializer) ? {
76- Some ( StringOrNumber :: String ( s) ) => Ok ( Some ( s) ) ,
77- Some ( StringOrNumber :: Number ( i) ) => Ok ( Some ( i. to_string ( ) ) ) ,
78- Some ( StringOrNumber :: Float ( f) ) => Ok ( Some ( f. to_string ( ) ) ) ,
79- None => Ok ( None ) ,
75+ match StringOrNumber :: deserialize ( deserializer) ? {
76+ StringOrNumber :: String ( s) => Ok ( Some ( s) ) ,
77+ StringOrNumber :: Number ( i) => Ok ( Some ( i. to_string ( ) ) ) ,
78+ StringOrNumber :: Float ( f) => Ok ( Some ( f. to_string ( ) ) ) ,
8079 }
8180}
8281
@@ -199,7 +198,10 @@ mod test {
199198 ) ?;
200199
201200 let config = Config :: load ( Some ( "liwan3.config.toml" . into ( ) ) ) . expect ( "failed to load config" ) ;
202- assert_eq ! ( config. geoip. unwrap( ) . maxmind_edition, Some ( "GeoLite2-City" . to_string( ) ) ) ;
201+ assert_eq ! ( config. geoip. clone( ) . unwrap( ) . maxmind_edition, Some ( "GeoLite2-City" . to_string( ) ) ) ;
202+ assert_eq ! ( config. geoip. unwrap( ) . maxmind_db_path, Some ( "test2" . to_string( ) ) ) ;
203+ assert_eq ! ( config. base_url, "http://localhost:8081" ) ;
204+ assert_eq ! ( config. data_dir, "./liwan-test-data" ) ;
203205 Ok ( ( ) )
204206 } ) ;
205207 }
0 commit comments