@@ -32,6 +32,7 @@ pub struct Ini {
32
32
map : Map < String , Map < String , Option < String > > > ,
33
33
default_section : std:: string:: String ,
34
34
comment_symbols : Vec < char > ,
35
+ inline_comment_symbols : Option < Vec < char > > ,
35
36
delimiters : Vec < char > ,
36
37
boolean_values : HashMap < bool , Vec < String > > ,
37
38
case_sensitive : bool ,
@@ -71,6 +72,17 @@ pub struct IniDefault {
71
72
///assert_eq!(default.comment_symbols, vec![';', '#']);
72
73
///```
73
74
pub comment_symbols : Vec < char > ,
75
+ ///Denotes the set of inline comment symbols for the object. The default of
76
+ ///`None` means to fall back to the normal comment symbols.
77
+ ///## Example
78
+ ///```rust
79
+ ///use configparser::ini::Ini;
80
+ ///
81
+ ///let mut config = Ini::new();
82
+ ///let default = config.defaults();
83
+ ///assert_eq!(default.inline_comment_symbols, None);
84
+ ///```
85
+ pub inline_comment_symbols : Option < Vec < char > > ,
74
86
///Denotes the set delimiters for the key-value pairs.
75
87
///## Example
76
88
///```rust
@@ -109,6 +121,7 @@ impl Default for IniDefault {
109
121
Self {
110
122
default_section : "default" . to_owned ( ) ,
111
123
comment_symbols : vec ! [ ';' , '#' ] ,
124
+ inline_comment_symbols : None ,
112
125
delimiters : vec ! [ '=' , ':' ] ,
113
126
multiline : false ,
114
127
boolean_values : [
@@ -285,6 +298,7 @@ impl Ini {
285
298
map : Map :: new ( ) ,
286
299
default_section : defaults. default_section ,
287
300
comment_symbols : defaults. comment_symbols ,
301
+ inline_comment_symbols : defaults. inline_comment_symbols ,
288
302
delimiters : defaults. delimiters ,
289
303
boolean_values : defaults. boolean_values ,
290
304
case_sensitive : defaults. case_sensitive ,
@@ -305,6 +319,7 @@ impl Ini {
305
319
IniDefault {
306
320
default_section : self . default_section . to_owned ( ) ,
307
321
comment_symbols : self . comment_symbols . to_owned ( ) ,
322
+ inline_comment_symbols : self . inline_comment_symbols . to_owned ( ) ,
308
323
delimiters : self . delimiters . to_owned ( ) ,
309
324
boolean_values : self . boolean_values . to_owned ( ) ,
310
325
case_sensitive : self . case_sensitive ,
@@ -330,6 +345,7 @@ impl Ini {
330
345
pub fn load_defaults ( & mut self , defaults : IniDefault ) {
331
346
self . default_section = defaults. default_section ;
332
347
self . comment_symbols = defaults. comment_symbols ;
348
+ self . inline_comment_symbols = defaults. inline_comment_symbols ;
333
349
self . delimiters = defaults. delimiters ;
334
350
self . boolean_values = defaults. boolean_values ;
335
351
self . case_sensitive = defaults. case_sensitive ;
@@ -366,6 +382,21 @@ impl Ini {
366
382
self . comment_symbols = symlist. to_vec ( ) ;
367
383
}
368
384
385
+ ///Sets the default inline comment symbols to the defined character slice (the default is `None` which falls back to the normal comment symbols).
386
+ ///Keep in mind that this will remove the default symbols. It must be set before `load()` or `read()` is called in order to take effect.
387
+ ///## Example
388
+ ///```rust
389
+ ///use configparser::ini::Ini;
390
+ ///
391
+ ///let mut config = Ini::new();
392
+ ///config.set_inline_comment_symbols(Some(&['!', '#']));
393
+ ///let map = config.load("tests/test.ini").unwrap();
394
+ ///```
395
+ ///Returns nothing.
396
+ pub fn set_inline_comment_symbols ( & mut self , symlist : Option < & [ char ] > ) {
397
+ self . inline_comment_symbols = symlist. map ( |val| val. to_vec ( ) ) ;
398
+ }
399
+
369
400
///Sets multiline string support.
370
401
///It must be set before `load()` or `read()` is called in order to take effect.
371
402
///## Example
@@ -724,6 +755,7 @@ impl Ini {
724
755
725
756
///Private function that parses ini-style syntax into a Map.
726
757
fn parse ( & self , input : String ) -> Result < Map < String , Map < String , Option < String > > > , String > {
758
+ let inline_comment_symbols: & [ char ] = self . inline_comment_symbols . as_deref ( ) . unwrap_or_else ( || self . comment_symbols . as_ref ( ) ) ;
727
759
let mut map: Map < String , Map < String , Option < String > > > = Map :: new ( ) ;
728
760
let mut section = self . default_section . clone ( ) ;
729
761
let mut current_key: Option < String > = None ;
@@ -740,23 +772,29 @@ impl Ini {
740
772
let mut blank_lines = 0usize ;
741
773
742
774
for ( num, raw_line) in input. lines ( ) . enumerate ( ) {
775
+ let raw_line = raw_line. trim ( ) ;
776
+
777
+ // If the line is _just_ a comment, skip it entirely.
743
778
let line = match raw_line. find ( |c : char | self . comment_symbols . contains ( & c) ) {
744
- Some ( idx ) => & raw_line [ ..idx ] ,
745
- None => raw_line,
779
+ Some ( 0 ) => continue ,
780
+ Some ( _ ) | None => raw_line,
746
781
} ;
747
782
748
- let trimmed = line. trim ( ) ;
783
+ let line = line. trim ( ) ;
749
784
750
785
// Skip empty lines, but keep track of them for multiline values.
751
- if trimmed. is_empty ( ) {
752
- // If a line is _just_ a comment (regardless of whether it's preceded by
753
- // whitespace), ignore it.
754
- if line == raw_line {
755
- blank_lines += 1 ;
756
- }
786
+ if line. is_empty ( ) {
787
+ blank_lines += 1 ;
757
788
continue ;
758
789
}
759
790
791
+ let line = match line. find ( |c : char | inline_comment_symbols. contains ( & c) ) {
792
+ Some ( idx) => & raw_line[ ..idx] ,
793
+ None => raw_line,
794
+ } ;
795
+
796
+ let trimmed = line. trim ( ) ;
797
+
760
798
match ( trimmed. find ( '[' ) , trimmed. rfind ( ']' ) ) {
761
799
( Some ( 0 ) , Some ( end) ) => {
762
800
section = caser ( trimmed[ 1 ..end] . trim ( ) ) ;
0 commit comments