From 35553136ee856a2485246b9a60f2d8915530c076 Mon Sep 17 00:00:00 2001 From: lxl66566 Date: Sun, 17 Dec 2023 16:26:35 +0800 Subject: [PATCH] feat: support string comment symbols fix ci --- src/ini.rs | 32 +++++++++++++++++++------------- tests/test.rs | 4 ++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/ini.rs b/src/ini.rs index 204df3f..3d45b70 100644 --- a/src/ini.rs +++ b/src/ini.rs @@ -26,7 +26,7 @@ use std::path::Path; pub struct Ini { map: Map>>, default_section: std::string::String, - comment_symbols: Vec, + comment_symbols: Vec, delimiters: Vec, boolean_values: HashMap>, case_sensitive: bool, @@ -63,9 +63,9 @@ pub struct IniDefault { /// ///let mut config = Ini::new(); ///let default = config.defaults(); - ///assert_eq!(default.comment_symbols, vec![';', '#']); + ///assert_eq!(default.comment_symbols, vec![";".to_string(), "#".to_string()]); ///``` - pub comment_symbols: Vec, + pub comment_symbols: Vec, ///Denotes the set delimiters for the key-value pairs. ///## Example ///```rust @@ -103,7 +103,7 @@ impl Default for IniDefault { fn default() -> Self { Self { default_section: "default".to_owned(), - comment_symbols: vec![';', '#'], + comment_symbols: vec![";".to_string(), "#".to_string()], delimiters: vec!['=', ':'], multiline: false, boolean_values: [ @@ -267,7 +267,7 @@ impl Ini { ///use configparser::ini::IniDefault; /// ///let mut default = IniDefault::default(); - ///default.comment_symbols = vec![';']; + ///default.comment_symbols = vec![";".to_string()]; ///default.delimiters = vec!['=']; ///let mut config = Ini::new_from_defaults(default.clone()); ///// Now, load as usual with new defaults: @@ -346,7 +346,7 @@ impl Ini { self.default_section = section.to_owned(); } - ///Sets the default comment symbols to the defined character slice (the defaults are `;` and `#`). + ///Sets the default comment symbols to the defined character slice (the defaults are `;` and `#`). Strings are also accepted. ///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. ///## Example ///```rust @@ -354,13 +354,17 @@ impl Ini { /// ///let mut config = Ini::new(); ///config.set_comment_symbols(&['!', '#']); + ///config.set_comment_symbols(&["!", "#", "//"]); // also allowed ///let map = config.load("tests/test.ini").unwrap(); ///``` ///Returns nothing. - pub fn set_comment_symbols(&mut self, symlist: &[char]) { - self.comment_symbols = symlist.to_vec(); + pub fn set_comment_symbols(&mut self, symlist: T) + where + T: IntoIterator, + T::Item: std::string::ToString, + { + self.comment_symbols = symlist.into_iter().map(|c| c.to_string()).collect(); } - ///Sets multiline string support. ///It must be set before `load()` or `read()` is called in order to take effect. ///## Example @@ -730,10 +734,12 @@ impl Ini { }; for (num, raw_line) in input.lines().enumerate() { - let line = match raw_line.find(|c: char| self.comment_symbols.contains(&c)) { - Some(idx) => &raw_line[..idx], - None => raw_line, - }; + let mut line = raw_line; + for comment_symbol in &self.comment_symbols { + if let Some(idx) = line.find(comment_symbol) { + line = &line[..idx] + } + } let trimmed = line.trim(); diff --git a/tests/test.rs b/tests/test.rs index 4313aa1..baaed39 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -6,7 +6,7 @@ use std::error::Error; fn non_cs() -> Result<(), Box> { let mut config = Ini::new(); let map = config.load("tests/test.ini")?; - config.set_comment_symbols(&[';', '#', '!']); + config.set_comment_symbols([";", "#", "!", "//"]); let inpstring = config.read( "defaultvalues=defaultvalues [topsecret] @@ -16,7 +16,7 @@ fn non_cs() -> Result<(), Box> { None string Password=[in-brackets] [ spacing ] - indented=indented + indented=indented // String comment not indented = not indented ;testcomment !modified comment [values]#another comment