diff --git a/README.md b/README.md index fb2810e..1568232 100644 --- a/README.md +++ b/README.md @@ -188,14 +188,17 @@ You can activate it by adding it as a feature like this: configparser = { version = "3.2.0", features = ["tokio"] } ``` -## Override Defaults +## Override Options -You can change the default configuration options like this. See the API for more verbose documentation. +You can change the default configuration options like this. +See the API for more verbose documentation. ``` -let mut default = IniDefault::default(); -default.multiline = true; -let mut config = Ini::new_from_defaults(default); +let mut parser_options = IniDefault::default(); +parser_options.multiline = true; +parser_options.enable_inline_comments = false; + +let mut config = Ini::new_from_defaults(parser_options); ``` ## 📜 License @@ -224,7 +227,7 @@ Old changelogs are visible in the commit history. - Adds support for multi-line key-value pairs. - Adds `async-std` feature for asynchronous file operations. - Some performance optimizations. -- 3.0.3 +- 3.0.3 - Add default empty line on empty strings. - Feature to append to existing `Ini` objects. - Minor lint fixes. diff --git a/src/ini.rs b/src/ini.rs index 1f9b41e..4b2afbd 100644 --- a/src/ini.rs +++ b/src/ini.rs @@ -35,6 +35,7 @@ pub struct Ini { boolean_values: HashMap>, case_sensitive: bool, multiline: bool, + enable_inline_comments: bool, } #[cfg(all(feature = "serde", not(feature = "indexmap")))] @@ -163,6 +164,16 @@ pub struct IniDefault { ///assert_eq!(default.multiline, false); ///``` pub multiline: bool, + ///Denotes if the `Ini` object recognizes inline comments. + ///## Example + ///```rust + ///use configparser::ini::Ini; + /// + ///let mut config = Ini::new(); + ///let default = config.defaults(); + ///assert_eq!(default.enable_inline_comments, true); + ///``` + pub enable_inline_comments: bool, } impl Default for IniDefault { @@ -193,6 +204,7 @@ impl Default for IniDefault { .cloned() .collect(), case_sensitive: false, + enable_inline_comments: true, // retain compatibility with previous versions } } } @@ -352,6 +364,7 @@ impl Ini { boolean_values: defaults.boolean_values, case_sensitive: defaults.case_sensitive, multiline: defaults.multiline, + enable_inline_comments: defaults.enable_inline_comments, } } @@ -373,6 +386,7 @@ impl Ini { boolean_values: self.boolean_values.to_owned(), case_sensitive: self.case_sensitive, multiline: self.multiline, + enable_inline_comments: self.enable_inline_comments, } } @@ -834,9 +848,13 @@ impl Ini { continue; } - let line = match line.find(|c: char| inline_comment_symbols.contains(&c)) { - Some(idx) => &line[..idx], - None => line, + let line = if self.enable_inline_comments { + match line.find(|c: char| inline_comment_symbols.contains(&c)) { + Some(idx) => &line[..idx], + None => line, + } + } else { + line }; let trimmed = line.trim(); diff --git a/tests/test.rs b/tests/test.rs index d367247..30d9a73 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -255,7 +255,7 @@ basic_option=basic_value } #[test] -fn inline_comment_symbols() -> Result<(), Box> { +fn inline_comment_symbols_enabled() -> Result<(), Box> { const FILE_CONTENTS: &str = " [basic_section] ; basic comment @@ -329,6 +329,60 @@ empty_option= Ok(()) } +#[test] +fn inline_comment_symbols_disabled() -> Result<(), Box> { + use configparser::ini::IniDefault; + + const FILE_CONTENTS: &str = " +[basic_section] +; basic comment + ; comment with space +basic_option=value +basic_with_comment=value ; Simple comment +basic_with_comment_hash=value # Simple comment +basic_with_extra_inline=value ! comment +empty_option= +"; + + let mut parser_options = IniDefault::default(); + // true tested in inline_comment_symbols_enabled() + parser_options.enable_inline_comments = false; + + let mut config = Ini::new_from_defaults(parser_options); + config.read(FILE_CONTENTS.to_owned())?; + + assert_eq!( + config.get("basic_section", "basic_with_comment"), + Some(String::from("value ; Simple comment")) + ); + assert_eq!( + config.get("basic_section", "basic_with_comment_hash"), + Some(String::from("value # Simple comment")) + ); + assert_eq!( + config.get("basic_section", "basic_with_extra_inline"), + Some(String::from("value ! comment")) + ); + assert_eq!( + config.get("basic_section", "empty_option"), + Some(String::from("")) + ); + + config.set_inline_comment_symbols(Some(&['!'])); + config.read(FILE_CONTENTS.to_owned())?; + + assert_eq!( + config.get("basic_section", "basic_with_comment"), + Some(String::from("value ; Simple comment")) + ); + assert_eq!( + config.get("basic_section", "basic_with_extra_inline"), + Some(String::from("value ! comment")) + ); + + Ok(()) +} + #[test] #[cfg(feature = "indexmap")] fn sort_on_write() -> Result<(), Box> {