From 42f7a1b3e92baf371c5e46c42fa48203870572d1 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 3 Jun 2024 10:58:20 -0500 Subject: [PATCH 1/2] test(encode): Show existing quote behavior --- crates/toml_edit/tests/testsuite/edit.rs | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/toml_edit/tests/testsuite/edit.rs b/crates/toml_edit/tests/testsuite/edit.rs index f31119af..f17a0a55 100644 --- a/crates/toml_edit/tests/testsuite/edit.rs +++ b/crates/toml_edit/tests/testsuite/edit.rs @@ -200,6 +200,36 @@ key3 = 8.1415926 "#]]); } +#[test] +fn test_insert_key_with_quotes() { + given( + r#" + [package] + name = "foo" + + [target] + "#, + ) + .running(|root| { + root["target"]["cfg(target_os = \"linux\")"] = table(); + root["target"]["cfg(target_os = \"linux\")"]["dependencies"] = table(); + root["target"]["cfg(target_os = \"linux\")"]["dependencies"]["name"] = value("dep"); + }) + .produces_display(str![[r#" + + [package] + name = "foo" + + [target] + +[target."cfg(target_os = \"linux\")"] + +[target."cfg(target_os = \"linux\")".dependencies] +name = "dep" + +"#]]); +} + // removal #[test] From c9e36e7a0fe7fb3f7c25a6f217eeb0d1182eecc5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 3 Jun 2024 10:59:32 -0500 Subject: [PATCH 2/2] fix(encode): Prefer literals over escaping double-quotes Part of rust-lang/cargo#14002 --- crates/toml/tests/testsuite/display.rs | 2 +- crates/toml_edit/src/encode.rs | 3 +++ crates/toml_edit/src/key.rs | 8 ++------ crates/toml_edit/tests/testsuite/edit.rs | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/crates/toml/tests/testsuite/display.rs b/crates/toml/tests/testsuite/display.rs index c38486e9..a0e0cff2 100644 --- a/crates/toml/tests/testsuite/display.rs +++ b/crates/toml/tests/testsuite/display.rs @@ -75,7 +75,7 @@ fn table() { } .to_string(), "\"foo.bar\" = 2\n\ - \"foo\\\"bar\" = 2\n" + 'foo\"bar' = 2\n" ); assert_eq!( map! { diff --git a/crates/toml_edit/src/encode.rs b/crates/toml_edit/src/encode.rs index b8c1c048..a8ccaa2a 100644 --- a/crates/toml_edit/src/encode.rs +++ b/crates/toml_edit/src/encode.rs @@ -439,6 +439,9 @@ fn infer_style(value: &str) -> (StringStyle, bool) { } match ch { '\t' => {} + '"' => { + prefer_literal = true; + } '\\' => { prefer_literal = true; } diff --git a/crates/toml_edit/src/key.rs b/crates/toml_edit/src/key.rs index 0fc793a0..15fdd4de 100644 --- a/crates/toml_edit/src/key.rs +++ b/crates/toml_edit/src/key.rs @@ -289,17 +289,13 @@ fn to_key_repr(key: &str) -> Repr { crate::encode::to_string_repr( key, Some(crate::encode::StringStyle::OnelineSingle), - Some(false), + None, ) } } #[cfg(not(feature = "parse"))] { - crate::encode::to_string_repr( - key, - Some(crate::encode::StringStyle::OnelineSingle), - Some(false), - ) + crate::encode::to_string_repr(key, Some(crate::encode::StringStyle::OnelineSingle), None) } } diff --git a/crates/toml_edit/tests/testsuite/edit.rs b/crates/toml_edit/tests/testsuite/edit.rs index f17a0a55..3b6a21a2 100644 --- a/crates/toml_edit/tests/testsuite/edit.rs +++ b/crates/toml_edit/tests/testsuite/edit.rs @@ -222,9 +222,9 @@ fn test_insert_key_with_quotes() { [target] -[target."cfg(target_os = \"linux\")"] +[target.'cfg(target_os = "linux")'] -[target."cfg(target_os = \"linux\")".dependencies] +[target.'cfg(target_os = "linux")'.dependencies] name = "dep" "#]]);