Skip to content

Commit

Permalink
fix(error): Improve dotted key overwrite error
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 29, 2021
1 parent 1b3ccd6 commit fe79d63
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ pub(crate) enum CustomError {
key: String,
table: Option<Vec<Key>>,
},
DottedKeyExtendWrongType {
key: Vec<Key>,
actual: &'static str,
},
InvalidHexEscape(u32),
UnparsedLine,
OutOfRange,
Expand All @@ -265,6 +269,14 @@ impl Display for CustomError {
writeln!(f, "Duplicate key `{}`", key)
}
}
CustomError::DottedKeyExtendWrongType { key, actual } => {
let path = key.iter().join(".");
writeln!(
f,
"Dotted key `{}` attempted to extend non-table type (`{}`)",
path, actual
)
}
CustomError::InvalidHexEscape(h) => {
writeln!(f, "Invalid hex escape code: {:x} ", h)
}
Expand Down
6 changes: 3 additions & 3 deletions src/parser/inline_table.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::key::Key;
use crate::parser::errors::CustomError;
use crate::parser::key::key;
use crate::parser::table::duplicate_key;
use crate::parser::table::extend_wrong_type;
use crate::parser::trivia::ws;
use crate::parser::value::value;
use crate::table::TableKeyValue;
Expand Down Expand Up @@ -57,8 +57,8 @@ fn descend_path<'a>(
Value::InlineTable(ref mut sweet_child_of_mine) => {
table = sweet_child_of_mine;
}
_ => {
return Err(duplicate_key(path, i));
ref v => {
return Err(extend_wrong_type(path, i, v.type_name()));
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/parser/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ pub(crate) fn duplicate_key(path: &[Key], i: usize) -> CustomError {
}
}

pub(crate) fn extend_wrong_type(path: &[Key], i: usize, actual: &'static str) -> CustomError {
assert!(i < path.len());
CustomError::DottedKeyExtendWrongType {
key: path[..=i].to_vec(),
actual,
}
}

impl TomlParser {
pub(crate) fn descend_path<'t, 'k>(
mut table: &'t mut Table,
Expand All @@ -91,8 +99,8 @@ impl TomlParser {
Item::Table(new_table)
});
match *entry {
Item::Value(..) => {
return Err(duplicate_key(path, i));
Item::Value(ref v) => {
return Err(extend_wrong_type(path, i, v.type_name()));
}
Item::ArrayOfTables(ref mut array) => {
debug_assert!(!array.is_empty());
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/invalid/duplicate-keys-dotted.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
TOML parse error at line 3, column 1
|
3 | ssl-version.min = 'tlsv1.2'
| ^
Dotted key `ssl-version` attempted to extend non-table type (`string`)

4 changes: 4 additions & 0 deletions tests/fixtures/invalid/duplicate-keys-dotted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[http]
ssl-version = 'tlsv1.1'
ssl-version.min = 'tlsv1.2'
ssl-version.max = 'tlsv1.3'

0 comments on commit fe79d63

Please sign in to comment.