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!: Make Key::from_str only accept Displays output #273

Merged
merged 2 commits into from
Dec 14, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ impl FromStr for Key {
/// if fails, tries as basic quoted key (surrounds with "")
/// and then literal quoted key (surrounds with '')
fn from_str(s: &str) -> Result<Self, Self::Err> {
let basic = format!("\"{}\"", s);
let literal = format!("'{}'", s);
Key::try_parse(s)
.or_else(|_| Key::try_parse(&basic))
.or_else(|_| Key::try_parse(&literal))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/parser/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ARRAY_TABLE_CLOSE: &[u8] = b"]]";
toml_parser!(std_table, parser, {
(
between(byte(STD_TABLE_OPEN), byte(STD_TABLE_CLOSE), key()),
line_trailing().and_then(|t| std::str::from_utf8(t)),
line_trailing().and_then(std::str::from_utf8),
)
.and_then(|(h, t)| parser.borrow_mut().deref_mut().on_std_header(h, t))
});
Expand All @@ -42,7 +42,7 @@ toml_parser!(std_table, parser, {
toml_parser!(array_table, parser, {
(
between(range(ARRAY_TABLE_OPEN), range(ARRAY_TABLE_CLOSE), key()),
line_trailing().and_then(|t| std::str::from_utf8(t)),
line_trailing().and_then(std::str::from_utf8),
)
.and_then(|(h, t)| parser.borrow_mut().deref_mut().on_array_header(h, t))
});
Expand Down
2 changes: 1 addition & 1 deletion tests/easy_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ fn from_table(
fn from_array(
decoded: &[toml_test_harness::Decoded],
) -> Result<toml_edit::easy::value::Array, toml_test_harness::Error> {
decoded.iter().map(|v| from_decoded(v)).collect()
decoded.iter().map(from_decoded).collect()
}
2 changes: 1 addition & 1 deletion tests/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ fn from_table(
fn from_array(
decoded: &[toml_test_harness::Decoded],
) -> Result<toml_edit::Array, toml_test_harness::Error> {
decoded.iter().map(|v| from_decoded(v)).collect()
decoded.iter().map(from_decoded).collect()
}
22 changes: 14 additions & 8 deletions tests/test_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use toml_edit::{Key, Value};
macro_rules! parse {
($s:expr, $ty:ty) => {{
let v = $s.parse::<$ty>();
assert!(v.is_ok());
assert!(v.is_ok(), "Failed with {}", v.unwrap_err());
v.unwrap()
}};
}
Expand All @@ -17,7 +17,7 @@ macro_rules! parse_value {
macro_rules! test_key {
($s:expr, $expected:expr) => {{
let key = parse!($s, Key);
assert_eq!(key.get(), $expected);
assert_eq!(key.get(), $expected, "");
}};
}

Expand All @@ -26,7 +26,11 @@ macro_rules! parse_error {
let res = $input.parse::<$ty>();
assert!(res.is_err());
let err = res.unwrap_err();
assert!(err.to_string().find($err_msg).is_some());
assert!(
err.to_string().find($err_msg).is_some(),
"Error was: `{:?}`",
err.to_string()
);
}};
}

Expand All @@ -35,7 +39,7 @@ fn test_parse_error() {
parse_error!("'hello'bla", Value, "Could not parse the line");
parse_error!(r#"{a = 2"#, Value, "Expected `}`");

parse_error!("'\"", Key, "Could not parse the line");
parse_error!("'\"", Key, "Expected `'`");
}

#[test]
Expand All @@ -46,10 +50,12 @@ fn test_key_from_str() {
r#""Jos\u00E9\U000A0000\n\t\r\f\b\"""#,
"Jos\u{00E9}\u{A0000}\n\t\r\u{c}\u{8}\""
);
test_key!("", "");
test_key!("'hello key'bla", "'hello key'bla");
let wp = "C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\cargo-edit-test.YizxPxxElXn9";
test_key!(wp, wp);
test_key!("\"\"", "");
test_key!("\"'hello key'bla\"", "'hello key'bla");
test_key!(
"'C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\cargo-edit-test.YizxPxxElXn9'",
"C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\cargo-edit-test.YizxPxxElXn9"
);
}

#[test]
Expand Down