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

fix(parser): Catch more dotted key interactions #418

Merged
merged 1 commit into from Jan 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`