From d292b77253b7845ff4bf919fd3ad6c1e346cc9df Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 13 Jun 2023 17:06:10 +0800 Subject: [PATCH 1/2] return error when value is missing --- src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ab745d6..cb1ce65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1356,7 +1356,12 @@ impl<'a> Parser<'a> { } fn parse_key(&mut self) -> Result { - self.parse_str_until(&[Some('='), Some(':')], false) + let key = self.parse_str_until(&[Some('='), Some(':'), Some('\n')], false)?; + if self.ch == Some('\n') { + return self.error("Value is missing"); + } else { + Ok(key) + } } fn parse_val(&mut self) -> Result { @@ -1561,6 +1566,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"; From 8d52fa4a9ab6d5b472402d05bf92cff105e4f855 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 13 Jun 2023 17:57:29 +0800 Subject: [PATCH 2/2] add dont_allow_no_value option --- src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cb1ce65..b9e5b70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -215,6 +215,17 @@ 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 { @@ -222,6 +233,7 @@ impl Default for ParseOption { ParseOption { enabled_quote: true, enabled_escape: true, + dont_allow_no_value: true, } } } @@ -1357,7 +1369,7 @@ impl<'a> Parser<'a> { fn parse_key(&mut self) -> Result { let key = self.parse_str_until(&[Some('='), Some(':'), Some('\n')], false)?; - if self.ch == Some('\n') { + if self.opt.dont_allow_no_value && self.ch == Some('\n') { return self.error("Value is missing"); } else { Ok(key)