Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 21 additions & 3 deletions src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Ini {
boolean_values: HashMap<bool, Vec<String>>,
case_sensitive: bool,
multiline: bool,
enable_inline_comments: bool,
}

#[cfg(all(feature = "serde", not(feature = "indexmap")))]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -193,6 +204,7 @@ impl Default for IniDefault {
.cloned()
.collect(),
case_sensitive: false,
enable_inline_comments: true, // retain compatibility with previous versions
}
}
}
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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();
Expand Down
56 changes: 55 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ basic_option=basic_value
}

#[test]
fn inline_comment_symbols() -> Result<(), Box<dyn Error>> {
fn inline_comment_symbols_enabled() -> Result<(), Box<dyn Error>> {
const FILE_CONTENTS: &str = "
[basic_section]
; basic comment
Expand Down Expand Up @@ -329,6 +329,60 @@ empty_option=
Ok(())
}

#[test]
fn inline_comment_symbols_disabled() -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
Expand Down
Loading