Skip to content

Commit

Permalink
fix!: Make Key::from_str only accept Displays output
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
epage committed Dec 14, 2021
1 parent 3899970 commit dc7bc2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
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
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

0 comments on commit dc7bc2a

Please sign in to comment.