Repository: toml-rs/toml
Pull request #740 (2024-06-03 15:59:59)

fix(encode): Prefer literals over escaping double-quotes 
Part of rust-lang/cargo#14002


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index ddf2bed8..411c9084 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -549,9 +549,9 @@ dependencies = [
 
 [[package]]
 name = "serde"
-version = "1.0.203"
+version = "1.0.202"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
+checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
 dependencies = [
  "serde_derive",
 ]
@@ -569,9 +569,9 @@ dependencies = [
 
 [[package]]
 name = "serde_derive"
-version = "1.0.203"
+version = "1.0.202"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
+checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -611,9 +611,9 @@ checksum = "fa42c91313f1d05da9b26f267f931cf178d4aba455b4c4622dd7355eb80c6640"
 
 [[package]]
 name = "snapbox"
-version = "0.6.7"
+version = "0.6.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "94204b12a4d3550420babdb4148c6639692e4e3e61060866929c5107f208aeb6"
+checksum = "9ad8c7be18cc9ec7f4d7948ad6b9df0e04fc649663e3c0ed59f304ed17ca69e9"
 dependencies = [
  "anstream 0.6.14",
  "anstyle",
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)
     }
 }
 


Test patch:
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/tests/testsuite/edit.rs b/crates/toml_edit/tests/testsuite/edit.rs
index f31119af..3b6a21a2 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]