Skip to content

Commit

Permalink
feat: support string comment symbols
Browse files Browse the repository at this point in the history
fix ci
  • Loading branch information
lxl66566 committed Dec 19, 2023
1 parent 4658659 commit 3555313
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
32 changes: 19 additions & 13 deletions src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::path::Path;
pub struct Ini {
map: Map<String, Map<String, Option<String>>>,
default_section: std::string::String,
comment_symbols: Vec<char>,
comment_symbols: Vec<String>,
delimiters: Vec<char>,
boolean_values: HashMap<bool, Vec<String>>,
case_sensitive: bool,
Expand Down Expand Up @@ -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<char>,
pub comment_symbols: Vec<String>,
///Denotes the set delimiters for the key-value pairs.
///## Example
///```rust
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -346,21 +346,25 @@ 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
///use configparser::ini::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<T>(&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
Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::error::Error;
fn non_cs() -> Result<(), Box<dyn Error>> {
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]
Expand All @@ -16,7 +16,7 @@ fn non_cs() -> Result<(), Box<dyn Error>> {
None string
Password=[in-brackets]
[ spacing ]
indented=indented
indented=indented // String comment
not indented = not indented ;testcomment
!modified comment
[values]#another comment
Expand Down

0 comments on commit 3555313

Please sign in to comment.