Skip to content

Commit

Permalink
Create feature flag allow-empty-values (#7)
Browse files Browse the repository at this point in the history
This PR address #8, introducing our first feature flag to allow for
parsing lines that have a key but no value.

We also enable running the tests with all features enabled as well as
the default.

Signed-off-by: Kyle Rader <[email protected]>
Co-authored-by: Kyle Rader <[email protected]>
  • Loading branch information
kyle-rader-msft and Kyle Rader authored Mar 7, 2024
1 parent d469e3e commit ca62ab4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
- name: Unit Tests
- name: Unit Tests (default features)
uses: actions-rs/cargo@v1
with:
command: test
- name: "Unit Tests (all features)"
uses: actions-rs/cargo@v1
with:
command: test
args: "--all-features"
quality:
name: Code Quality
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ edition = "2021"

authors = ["TheDaemoness"]
include = ["/src", "/README.md", "/DOC.md"]
rust-version = "1.56" # 2021 edition
rust-version = "1.56" # 2021 edition
version = "1.0.2"

[workspace]
members = ["tools"]

[features]
allow-empty-values = []
17 changes: 13 additions & 4 deletions src/linereader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ pub fn parse_line(line: &str) -> LineReadResult<'_> {
} else if let Some((key_raw, val_raw)) = l.split_once('=') {
let key = key_raw.trim_end();
let val = val_raw.trim_start();
if key.is_empty() || val.is_empty() {
Err(ParseError::InvalidLine)
} else {
Ok(Line::Pair(key.trim_end(), val.trim_start()))
match (key.is_empty(), val.is_empty()) {
(true, _) => Err(ParseError::InvalidLine),
(false, true) => {
#[cfg(feature = "allow-empty-values")]
{
Ok(Line::Pair(key.trim_end(), val))
}
#[cfg(not(feature = "allow-empty-values"))]
{
Err(ParseError::InvalidLine)
}
}
(false, false) => Ok(Line::Pair(key.trim_end(), val.trim_start())),
}
} else {
Err(ParseError::InvalidLine)
Expand Down
7 changes: 6 additions & 1 deletion src/tests/linereader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fn valid_props() {
(" foo = bar = baz ", Pair("foo", "bar = baz")),
("foo = bar #baz", Pair("foo", "bar #baz")),
("foo = [bar]", Pair("foo", "[bar]")),
#[cfg(feature = "allow-empty-values")]
("foo =", Pair("foo", "")),
#[cfg(feature = "allow-empty-values")]
("foo = ", Pair("foo", "")),
])
}

Expand Down Expand Up @@ -64,8 +68,9 @@ fn invalid() {
"][",
"nonproperty",
"=",
"noval = ",
" = nokey",
#[cfg(not(feature = "allow-empty-values"))]
"noval = ",
];
for line in lines {
assert!(matches!(
Expand Down

0 comments on commit ca62ab4

Please sign in to comment.