From 732c8a58f049a81367187d209a8a2a1ba4df0479 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 14 Dec 2021 13:10:46 -0600 Subject: [PATCH] fix!: Make `Key::from_str` only accept `Display`s output I remember wanting to remove this before and had problems. It looks like those have since been addressed (I think with changes to repr). Before, we were taking any string and trying to infer what its repr would be. Now we only parse actualy dotted keys. This will hopefully help with rust-lang/cargo#10176 BREAKING CHANGE: `Key::from_str` will accept fewer values and will return slightly different errors. --- src/key.rs | 4 ---- tests/test_parse.rs | 22 ++++++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/key.rs b/src/key.rs index 0ffb37de..c9993499 100644 --- a/src/key.rs +++ b/src/key.rs @@ -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 { - let basic = format!("\"{}\"", s); - let literal = format!("'{}'", s); Key::try_parse(s) - .or_else(|_| Key::try_parse(&basic)) - .or_else(|_| Key::try_parse(&literal)) } } diff --git a/tests/test_parse.rs b/tests/test_parse.rs index 7d03b0e3..231d3feb 100644 --- a/tests/test_parse.rs +++ b/tests/test_parse.rs @@ -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() }}; } @@ -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, ""); }}; } @@ -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() + ); }}; } @@ -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] @@ -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]