Skip to content

Commit

Permalink
Merge pull request #418 from epage/dot
Browse files Browse the repository at this point in the history
fix(parser): Catch more dotted key interactions
  • Loading branch information
epage committed Jan 3, 2023
2 parents 26465fb + 4c776a4 commit 4ee4d04
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
13 changes: 11 additions & 2 deletions crates/toml_edit/src/parser/state.rs
Expand Up @@ -116,9 +116,10 @@ impl ParseState {
let key = &path[path.len() - 1];
if let Some(entry) = parent_table.remove(key.get()) {
match entry {
Item::Table(t) if t.implicit => {
Item::Table(t) if t.implicit && !t.is_dotted() => {
self.current_table = t;
}
// Since tables cannot be defined more than once, redefining such tables using a [table] header is not allowed. Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed.
_ => return Err(CustomError::duplicate_key(&path, path.len() - 1)),
}
}
Expand Down Expand Up @@ -202,7 +203,15 @@ impl ParseState {
table = last_child;
}
Item::Table(ref mut sweet_child_of_mine) => {
// "Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed"
// Since tables cannot be defined more than once, redefining such tables using a
// [table] header is not allowed. Likewise, using dotted keys to redefine tables
// already defined in [table] form is not allowed.
if sweet_child_of_mine.is_dotted() && !dotted {
return Err(CustomError::DuplicateKey {
key: key.get().into(),
table: None,
});
}
if dotted && !sweet_child_of_mine.is_implicit() {
return Err(CustomError::DuplicateKey {
key: key.get().into(),
Expand Down
8 changes: 1 addition & 7 deletions crates/toml_edit/tests/decoder_compliance.rs
Expand Up @@ -3,12 +3,6 @@ mod decoder;
fn main() {
let decoder = decoder::Decoder;
let mut harness = toml_test_harness::DecoderHarness::new(decoder);
harness
.ignore([
"valid/string/escape-esc.toml",
"invalid/table/duplicate-key-dotted-table.toml",
"invalid/table/duplicate-key-dotted-table2.toml",
])
.unwrap();
harness.ignore(["valid/string/escape-esc.toml"]).unwrap();
harness.test();
}
8 changes: 1 addition & 7 deletions crates/toml_edit/tests/easy_decoder_compliance.rs
Expand Up @@ -5,13 +5,7 @@ mod easy_decoder;
fn main() {
let decoder = easy_decoder::Decoder;
let mut harness = toml_test_harness::DecoderHarness::new(decoder);
harness
.ignore([
"valid/string/escape-esc.toml",
"invalid/table/duplicate-key-dotted-table.toml",
"invalid/table/duplicate-key-dotted-table2.toml",
])
.unwrap();
harness.ignore(["valid/string/escape-esc.toml"]).unwrap();
harness.test();
}

Expand Down
@@ -0,0 +1,6 @@
TOML parse error at line 4, column 1
|
4 | [fruit.apple] # INVALID
| ^
Invalid table header
Duplicate key `apple` in table `fruit`
@@ -0,0 +1,6 @@
TOML parse error at line 4, column 1
|
4 | [fruit.apple.taste] # INVALID
| ^
Invalid table header
Duplicate key `apple`

0 comments on commit 4ee4d04

Please sign in to comment.