Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to return error when value is missing #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,25 @@ pub struct ParseOption {
///
/// If `enabled_escape` is true, then the value of `Key` will become `C:Windows` (`\W` equals to `W`).
pub enabled_escape: bool,

/// Don't allow a key which contains no value
/// For example
/// ```ini
/// [Section]
/// Key1
/// Key2=C:\Windows
/// ```
///
/// If `dont_allow_no_value` is true, then the given ini file is not valid.
pub dont_allow_no_value: bool,
}

impl Default for ParseOption {
fn default() -> ParseOption {
ParseOption {
enabled_quote: true,
enabled_escape: true,
dont_allow_no_value: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be false by default.

}
}
}
Expand Down Expand Up @@ -1356,7 +1368,12 @@ impl<'a> Parser<'a> {
}

fn parse_key(&mut self) -> Result<String, ParseError> {
self.parse_str_until(&[Some('='), Some(':')], false)
let key = self.parse_str_until(&[Some('='), Some(':'), Some('\n')], false)?;
if self.opt.dont_allow_no_value && self.ch == Some('\n') {
return self.error("Value is missing");
} else {
Ok(key)
}
}

fn parse_val(&mut self) -> Result<String, ParseError> {
Expand Down Expand Up @@ -1561,6 +1578,13 @@ mod test {
assert!(opt.is_ok());
}

#[test]
fn parse_when_value_missing() {
let invalid_input = "[sec1]\nkey1\nkey2=377";
let ini = Ini::load_from_str(invalid_input);
assert!(ini.is_err())
}

#[test]
fn parse_error_numbers() {
let invalid_input = "\n\\x";
Expand Down