@@ -45,8 +45,8 @@ pub enum ValueType {
4545///
4646/// The file format is determined by the `format` parameter, not by file extension.
4747pub fn parse_config_file ( path : & str , format : ConfigFormat ) -> Result < Vec < ParsedEntry > > {
48- let content =
49- std :: fs :: read_to_string ( path ) . with_context ( || format ! ( "Failed to read config: {}" , path) ) ?;
48+ let content = std :: fs :: read_to_string ( path )
49+ . with_context ( || format ! ( "Failed to read config: {}" , path) ) ?;
5050 parse_config_string ( & content, format)
5151}
5252
@@ -66,8 +66,9 @@ pub fn parse_config_string(content: &str, format: ConfigFormat) -> Result<Vec<Pa
6666
6767/// Parse TOML content into flat key-value entries.
6868fn parse_toml ( content : & str ) -> Result < Vec < ParsedEntry > > {
69- let table: toml:: Table =
70- content. parse ( ) . with_context ( || "Failed to parse TOML content" ) ?;
69+ let table: toml:: Table = content
70+ . parse ( )
71+ . with_context ( || "Failed to parse TOML content" ) ?;
7172 let mut entries = Vec :: new ( ) ;
7273 flatten_toml_value ( & toml:: Value :: Table ( table) , "" , & mut entries) ;
7374 Ok ( entries)
@@ -178,7 +179,9 @@ fn parse_json_value(input: &str) -> Result<JsonValue> {
178179 _ => {
179180 // Try to parse as number
180181 let end = input
181- . find ( |c : char | !c. is_ascii_digit ( ) && c != '.' && c != '-' && c != '+' && c != 'e' && c != 'E' )
182+ . find ( |c : char | {
183+ !c. is_ascii_digit ( ) && c != '.' && c != '-' && c != '+' && c != 'e' && c != 'E'
184+ } )
182185 . unwrap_or ( input. len ( ) ) ;
183186 let num_str = & input[ ..end] ;
184187 let num: f64 = num_str
@@ -262,13 +265,34 @@ fn parse_json_string(input: &str) -> Result<(String, &str)> {
262265 while i < bytes. len ( ) {
263266 if bytes[ i] == b'\\' && i + 1 < bytes. len ( ) {
264267 match bytes[ i + 1 ] {
265- b'"' => { result. push ( '"' ) ; i += 2 ; }
266- b'\\' => { result. push ( '\\' ) ; i += 2 ; }
267- b'n' => { result. push ( '\n' ) ; i += 2 ; }
268- b't' => { result. push ( '\t' ) ; i += 2 ; }
269- b'r' => { result. push ( '\r' ) ; i += 2 ; }
270- b'/' => { result. push ( '/' ) ; i += 2 ; }
271- _ => { result. push ( bytes[ i + 1 ] as char ) ; i += 2 ; }
268+ b'"' => {
269+ result. push ( '"' ) ;
270+ i += 2 ;
271+ }
272+ b'\\' => {
273+ result. push ( '\\' ) ;
274+ i += 2 ;
275+ }
276+ b'n' => {
277+ result. push ( '\n' ) ;
278+ i += 2 ;
279+ }
280+ b't' => {
281+ result. push ( '\t' ) ;
282+ i += 2 ;
283+ }
284+ b'r' => {
285+ result. push ( '\r' ) ;
286+ i += 2 ;
287+ }
288+ b'/' => {
289+ result. push ( '/' ) ;
290+ i += 2 ;
291+ }
292+ _ => {
293+ result. push ( bytes[ i + 1 ] as char ) ;
294+ i += 2 ;
295+ }
272296 }
273297 } else if bytes[ i] == b'"' {
274298 return Ok ( ( result, & input[ i + 1 ..] ) ) ;
@@ -307,7 +331,9 @@ fn parse_json_value_with_rest(input: &str) -> Result<(JsonValue, &str)> {
307331 b'n' if input. starts_with ( "null" ) => Ok ( ( JsonValue :: Null , & input[ 4 ..] ) ) ,
308332 _ => {
309333 let end = input
310- . find ( |c : char | !c. is_ascii_digit ( ) && c != '.' && c != '-' && c != '+' && c != 'e' && c != 'E' )
334+ . find ( |c : char | {
335+ !c. is_ascii_digit ( ) && c != '.' && c != '-' && c != '+' && c != 'e' && c != 'E'
336+ } )
311337 . unwrap_or ( input. len ( ) ) ;
312338 let num_str = & input[ ..end] ;
313339 let num: f64 = num_str
@@ -496,12 +522,12 @@ fn classify_yaml_value(s: &str) -> (String, ValueType) {
496522 }
497523
498524 // Integer
499- if let Ok ( _ ) = s. parse :: < i64 > ( ) {
525+ if s. parse :: < i64 > ( ) . is_ok ( ) {
500526 return ( s. to_string ( ) , ValueType :: Int ) ;
501527 }
502528
503529 // Float
504- if let Ok ( _ ) = s. parse :: < f64 > ( ) {
530+ if s. parse :: < f64 > ( ) . is_ok ( ) {
505531 return ( s. to_string ( ) , ValueType :: Float ) ;
506532 }
507533
@@ -576,12 +602,12 @@ fn classify_ini_value(s: &str) -> (String, ValueType) {
576602 }
577603
578604 // Integer
579- if let Ok ( _ ) = s. parse :: < i64 > ( ) {
605+ if s. parse :: < i64 > ( ) . is_ok ( ) {
580606 return ( s. to_string ( ) , ValueType :: Int ) ;
581607 }
582608
583609 // Float
584- if let Ok ( _ ) = s. parse :: < f64 > ( ) {
610+ if s. parse :: < f64 > ( ) . is_ok ( ) {
585611 return ( s. to_string ( ) , ValueType :: Float ) ;
586612 }
587613
@@ -602,35 +628,63 @@ host = "localhost"
602628debug = true
603629"# ;
604630 let entries = parse_toml ( content) . unwrap ( ) ;
605- assert ! ( entries. iter( ) . any( |e| e. key == "server.port" && e. value == "8080" && e. value_type == ValueType :: Int ) ) ;
606- assert ! ( entries. iter( ) . any( |e| e. key == "server.host" && e. value == "localhost" && e. value_type == ValueType :: String ) ) ;
607- assert ! ( entries. iter( ) . any( |e| e. key == "server.debug" && e. value == "true" && e. value_type == ValueType :: Bool ) ) ;
631+ assert ! ( entries. iter( ) . any( |e| e. key == "server.port"
632+ && e. value == "8080"
633+ && e. value_type == ValueType :: Int ) ) ;
634+ assert ! ( entries. iter( ) . any( |e| e. key == "server.host"
635+ && e. value == "localhost"
636+ && e. value_type == ValueType :: String ) ) ;
637+ assert ! ( entries. iter( ) . any( |e| e. key == "server.debug"
638+ && e. value == "true"
639+ && e. value_type == ValueType :: Bool ) ) ;
608640 }
609641
610642 #[ test]
611643 fn test_parse_json_simple ( ) {
612644 let content = r#"{"port": 8080, "host": "localhost", "debug": true}"# ;
613645 let entries = parse_json ( content) . unwrap ( ) ;
614- assert ! ( entries. iter( ) . any( |e| e. key == "port" && e. value == "8080" && e. value_type == ValueType :: Int ) ) ;
615- assert ! ( entries. iter( ) . any( |e| e. key == "host" && e. value == "localhost" && e. value_type == ValueType :: String ) ) ;
616- assert ! ( entries. iter( ) . any( |e| e. key == "debug" && e. value == "true" && e. value_type == ValueType :: Bool ) ) ;
646+ assert ! (
647+ entries
648+ . iter( )
649+ . any( |e| e. key == "port" && e. value == "8080" && e. value_type == ValueType :: Int )
650+ ) ;
651+ assert ! ( entries. iter( ) . any( |e| e. key == "host"
652+ && e. value == "localhost"
653+ && e. value_type == ValueType :: String ) ) ;
654+ assert ! (
655+ entries
656+ . iter( )
657+ . any( |e| e. key == "debug" && e. value == "true" && e. value_type == ValueType :: Bool )
658+ ) ;
617659 }
618660
619661 #[ test]
620662 fn test_parse_yaml_simple ( ) {
621663 let content = "server:\n port: 8080\n host: localhost\n debug: true\n " ;
622664 let entries = parse_yaml ( content) . unwrap ( ) ;
623- assert ! ( entries. iter( ) . any( |e| e. key == "server.port" && e. value == "8080" && e. value_type == ValueType :: Int ) ) ;
624- assert ! ( entries. iter( ) . any( |e| e. key == "server.host" && e. value == "localhost" && e. value_type == ValueType :: String ) ) ;
625- assert ! ( entries. iter( ) . any( |e| e. key == "server.debug" && e. value == "true" && e. value_type == ValueType :: Bool ) ) ;
665+ assert ! ( entries. iter( ) . any( |e| e. key == "server.port"
666+ && e. value == "8080"
667+ && e. value_type == ValueType :: Int ) ) ;
668+ assert ! ( entries. iter( ) . any( |e| e. key == "server.host"
669+ && e. value == "localhost"
670+ && e. value_type == ValueType :: String ) ) ;
671+ assert ! ( entries. iter( ) . any( |e| e. key == "server.debug"
672+ && e. value == "true"
673+ && e. value_type == ValueType :: Bool ) ) ;
626674 }
627675
628676 #[ test]
629677 fn test_parse_ini_simple ( ) {
630678 let content = "[server]\n port = 8080\n host = localhost\n debug = true\n " ;
631679 let entries = parse_ini ( content) . unwrap ( ) ;
632- assert ! ( entries. iter( ) . any( |e| e. key == "server.port" && e. value == "8080" && e. value_type == ValueType :: Int ) ) ;
633- assert ! ( entries. iter( ) . any( |e| e. key == "server.host" && e. value == "localhost" && e. value_type == ValueType :: String ) ) ;
634- assert ! ( entries. iter( ) . any( |e| e. key == "server.debug" && e. value == "true" && e. value_type == ValueType :: Bool ) ) ;
680+ assert ! ( entries. iter( ) . any( |e| e. key == "server.port"
681+ && e. value == "8080"
682+ && e. value_type == ValueType :: Int ) ) ;
683+ assert ! ( entries. iter( ) . any( |e| e. key == "server.host"
684+ && e. value == "localhost"
685+ && e. value_type == ValueType :: String ) ) ;
686+ assert ! ( entries. iter( ) . any( |e| e. key == "server.debug"
687+ && e. value == "true"
688+ && e. value_type == ValueType :: Bool ) ) ;
635689 }
636690}
0 commit comments