Skip to content

Commit fe5ee76

Browse files
authored
Allow defining subtables of tables created via dotted keys (#14)
* Fix the `invalid!` macro * Rewrite `deserialize_table` Previously, defining subtables of tables created via dotted keys was not allowed, even though it is valid according to the spec. For example, the following valid TOML was incorrectly rejected: a.b = 0 [a.c]
1 parent ee7004f commit fe5ee76

16 files changed

+520
-210
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[fruit]
2+
apple.color = "red"
3+
4+
[[fruit.apple.seeds]]
5+
size = 2
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[fruit]
2+
apple.color = "red"
3+
apple.taste.sweet = true
4+
5+
# [fruit.apple] # INVALID
6+
[fruit.apple.taste] # INVALID
7+
8+
[fruit.apple.texture] # you can add sub-tables
9+
smooth = true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [fruit.apple] # INVALID
2+
[fruit.apple.taste] # INVALID
3+
4+
[fruit]
5+
apple.color = "red"
6+
apple.taste.sweet = true
7+
8+
[fruit.apple.texture] # you can add sub-tables
9+
smooth = true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[fruit.apple.texture] # you can add sub-tables
2+
smooth = true
3+
4+
[fruit]
5+
apple.color = "red"
6+
apple.taste.sweet = true
7+
8+
# [fruit.apple] # INVALID
9+
# [fruit.apple.taste] # INVALID
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[fruit]
2+
apple.color = "red"
3+
apple.taste.sweet = true
4+
5+
# [fruit.apple] # INVALID
6+
# [fruit.apple.taste] # INVALID
7+
8+
[fruit.apple.texture] # you can add sub-tables
9+
smooth = true

integ-tests/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ macro_rules! invalid {
221221
let toml_str =
222222
std::fs::read_to_string(dbg!(concat!("data/", stringify!($name), ".toml")))
223223
.expect(concat!("failed to load ", stringify!($name), ".toml"));
224-
let error = toml_span::parse(toml_str).unwrap_err();
225-
$crate::error_snapshot!($name, error, &toml_str);
224+
let error = toml_span::parse(&toml_str).unwrap_err();
225+
$crate::error_snapshot!($name, Some(error.to_diagnostic(())), &toml_str);
226226
}
227227
};
228228
($name:ident, $toml:expr) => {

integ-tests/tests/parser.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ name = "Born in the USA"
4949
"#
5050
);
5151

52+
valid!(table_9_valid);
53+
valid!(table_9_reverse_valid);
54+
invalid!(table_9_invalid);
55+
invalid!(table_9_reverse_invalid);
56+
valid!(array_within_dotted);
57+
5258
mod stray_cr {
5359
use super::invalid;
5460

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
source: integ-tests/tests/parser.rs
3+
expression: spans
4+
---
5+
note[string]: root_fruit_apple_color
6+
┌─ array_within_dotted:2:16
7+
8+
2apple.color = "red"
9+
^^^
10+
11+
note[integer]: root_fruit_apple_seeds_0_size
12+
┌─ array_within_dotted:5:8
13+
14+
5size = 2
15+
^
16+
17+
note[table]: root_fruit_apple_seeds_0
18+
┌─ array_within_dotted:4:1
19+
20+
4 │ ╭ [[fruit.apple.seeds]]
21+
5 │ │ size = 2
22+
│ ╰────────^
23+
24+
note[array]: root_fruit_apple_seeds
25+
┌─ array_within_dotted:4:1
26+
27+
4 │ ╭ [[fruit.apple.seeds]]
28+
5 │ │ size = 2
29+
│ ╰────────^
30+
31+
note[table]: root_fruit_apple
32+
┌─ array_within_dotted:4:1
33+
34+
4 │ ╭ [[fruit.apple.seeds]]
35+
5 │ │ size = 2
36+
│ ╰────────^
37+
38+
note[table]: root_fruit
39+
┌─ array_within_dotted:1:1
40+
41+
1 │ ╭ [fruit]
42+
2 │ │ apple.color = "red"
43+
│ ╰───────────────────^
44+
45+
note[table]: root
46+
┌─ array_within_dotted:1:1
47+
48+
1 │ ╭ [fruit]
49+
2 │ │ apple.color = "red"
50+
3 │ │
51+
4 │ │ [[fruit.apple.seeds]]
52+
5 │ │ size = 2
53+
6 │ │
54+
│ ╰^
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: integ-tests/tests/parser.rs
3+
expression: valid_toml
4+
---
5+
{
6+
"fruit": {
7+
"apple": {
8+
"color": "red",
9+
"seeds": [
10+
{
11+
"size": 2
12+
}
13+
]
14+
}
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: integ-tests/tests/parser.rs
3+
expression: error
4+
---
5+
error[duplicate-key]:
6+
┌─ table_9_invalid:6:14
7+
8+
3apple.taste.sweet = true
9+
----- first key instance
10+
·
11+
6 │ [fruit.apple.taste] # INVALID
12+
^^^^^ duplicate key

0 commit comments

Comments
 (0)