Skip to content

Commit 0af6deb

Browse files
authored
Merge pull request #740 from epage/key-quotes
fix(encode): Prefer literals over escaping double-quotes
2 parents 9e6290f + c9e36e7 commit 0af6deb

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

crates/toml/tests/testsuite/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn table() {
7575
}
7676
.to_string(),
7777
"\"foo.bar\" = 2\n\
78-
\"foo\\\"bar\" = 2\n"
78+
'foo\"bar' = 2\n"
7979
);
8080
assert_eq!(
8181
map! {

crates/toml_edit/src/encode.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ fn infer_style(value: &str) -> (StringStyle, bool) {
439439
}
440440
match ch {
441441
'\t' => {}
442+
'"' => {
443+
prefer_literal = true;
444+
}
442445
'\\' => {
443446
prefer_literal = true;
444447
}

crates/toml_edit/src/key.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,13 @@ fn to_key_repr(key: &str) -> Repr {
289289
crate::encode::to_string_repr(
290290
key,
291291
Some(crate::encode::StringStyle::OnelineSingle),
292-
Some(false),
292+
None,
293293
)
294294
}
295295
}
296296
#[cfg(not(feature = "parse"))]
297297
{
298-
crate::encode::to_string_repr(
299-
key,
300-
Some(crate::encode::StringStyle::OnelineSingle),
301-
Some(false),
302-
)
298+
crate::encode::to_string_repr(key, Some(crate::encode::StringStyle::OnelineSingle), None)
303299
}
304300
}
305301

crates/toml_edit/tests/testsuite/edit.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@ key3 = 8.1415926
200200
"#]]);
201201
}
202202

203+
#[test]
204+
fn test_insert_key_with_quotes() {
205+
given(
206+
r#"
207+
[package]
208+
name = "foo"
209+
210+
[target]
211+
"#,
212+
)
213+
.running(|root| {
214+
root["target"]["cfg(target_os = \"linux\")"] = table();
215+
root["target"]["cfg(target_os = \"linux\")"]["dependencies"] = table();
216+
root["target"]["cfg(target_os = \"linux\")"]["dependencies"]["name"] = value("dep");
217+
})
218+
.produces_display(str![[r#"
219+
220+
[package]
221+
name = "foo"
222+
223+
[target]
224+
225+
[target.'cfg(target_os = "linux")']
226+
227+
[target.'cfg(target_os = "linux")'.dependencies]
228+
name = "dep"
229+
230+
"#]]);
231+
}
232+
203233
// removal
204234

205235
#[test]

0 commit comments

Comments
 (0)