From 806013fdf3a8321cc84597a37c9ef991a4b5b8e0 Mon Sep 17 00:00:00 2001 From: Gringo Suave Date: Sun, 24 Aug 2025 14:38:30 -0700 Subject: [PATCH 1/5] add option field --- src/ini.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ini.rs b/src/ini.rs index 1f9b41e..ef08aa0 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, // compatibility w/ 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, } } From ce578af2164f753ade01d0a3af324ebad2b4dd60 Mon Sep 17 00:00:00 2001 From: Gringo Suave Date: Sun, 24 Aug 2025 14:48:10 -0700 Subject: [PATCH 2/5] impl option --- src/ini.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ini.rs b/src/ini.rs index ef08aa0..1f5e9b9 100644 --- a/src/ini.rs +++ b/src/ini.rs @@ -848,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(); From fc097a85552f37892f435b602d220d042c828bc0 Mon Sep 17 00:00:00 2001 From: Gringo Suave Date: Sun, 24 Aug 2025 15:07:10 -0700 Subject: [PATCH 3/5] impl disabled option test --- tests/test.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) 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> { From 0e18b9e8757be6a0076e5b159fb2d13261f0e9e6 Mon Sep 17 00:00:00 2001 From: Gringo Suave Date: Sun, 24 Aug 2025 15:14:40 -0700 Subject: [PATCH 4/5] update readme --- README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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. From fb0c98a64788243f9e9b390e3fb23c8901a1c7f1 Mon Sep 17 00:00:00 2001 From: Gringo Suave Date: Mon, 25 Aug 2025 10:42:31 -0700 Subject: [PATCH 5/5] update comment --- src/ini.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ini.rs b/src/ini.rs index 1f5e9b9..4b2afbd 100644 --- a/src/ini.rs +++ b/src/ini.rs @@ -204,7 +204,7 @@ impl Default for IniDefault { .cloned() .collect(), case_sensitive: false, - enable_inline_comments: true, // compatibility w/ previous versions + enable_inline_comments: true, // retain compatibility with previous versions } } }