Skip to content

Commit dc7bc2a

Browse files
committed
fix!: Make Key::from_str only accept Displays 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.
1 parent 3899970 commit dc7bc2a

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/key.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ impl FromStr for Key {
158158
/// if fails, tries as basic quoted key (surrounds with "")
159159
/// and then literal quoted key (surrounds with '')
160160
fn from_str(s: &str) -> Result<Self, Self::Err> {
161-
let basic = format!("\"{}\"", s);
162-
let literal = format!("'{}'", s);
163161
Key::try_parse(s)
164-
.or_else(|_| Key::try_parse(&basic))
165-
.or_else(|_| Key::try_parse(&literal))
166162
}
167163
}
168164

tests/test_parse.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use toml_edit::{Key, Value};
33
macro_rules! parse {
44
($s:expr, $ty:ty) => {{
55
let v = $s.parse::<$ty>();
6-
assert!(v.is_ok());
6+
assert!(v.is_ok(), "Failed with {}", v.unwrap_err());
77
v.unwrap()
88
}};
99
}
@@ -17,7 +17,7 @@ macro_rules! parse_value {
1717
macro_rules! test_key {
1818
($s:expr, $expected:expr) => {{
1919
let key = parse!($s, Key);
20-
assert_eq!(key.get(), $expected);
20+
assert_eq!(key.get(), $expected, "");
2121
}};
2222
}
2323

@@ -26,7 +26,11 @@ macro_rules! parse_error {
2626
let res = $input.parse::<$ty>();
2727
assert!(res.is_err());
2828
let err = res.unwrap_err();
29-
assert!(err.to_string().find($err_msg).is_some());
29+
assert!(
30+
err.to_string().find($err_msg).is_some(),
31+
"Error was: `{:?}`",
32+
err.to_string()
33+
);
3034
}};
3135
}
3236

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

38-
parse_error!("'\"", Key, "Could not parse the line");
42+
parse_error!("'\"", Key, "Expected `'`");
3943
}
4044

4145
#[test]
@@ -46,10 +50,12 @@ fn test_key_from_str() {
4650
r#""Jos\u00E9\U000A0000\n\t\r\f\b\"""#,
4751
"Jos\u{00E9}\u{A0000}\n\t\r\u{c}\u{8}\""
4852
);
49-
test_key!("", "");
50-
test_key!("'hello key'bla", "'hello key'bla");
51-
let wp = "C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\cargo-edit-test.YizxPxxElXn9";
52-
test_key!(wp, wp);
53+
test_key!("\"\"", "");
54+
test_key!("\"'hello key'bla\"", "'hello key'bla");
55+
test_key!(
56+
"'C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\cargo-edit-test.YizxPxxElXn9'",
57+
"C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\cargo-edit-test.YizxPxxElXn9"
58+
);
5359
}
5460

5561
#[test]

0 commit comments

Comments
 (0)