From 705e2d54ef66baff1cac950d07492aebfe94d194 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 3 Jun 2024 10:59:32 -0500 Subject: [PATCH] fix(encode): Prefer literals over escaping double-quotes Part of rust-lang/cargo#14002 --- crates/toml_edit/src/encode.rs | 3 +++ crates/toml_edit/src/key.rs | 8 ++------ crates/toml_edit/tests/testsuite/edit.rs | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) 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" "#]]);