Skip to content

Commit

Permalink
Merge pull request #740 from epage/key-quotes
Browse files Browse the repository at this point in the history
fix(encode): Prefer literals over escaping double-quotes
  • Loading branch information
epage authored Jun 3, 2024
2 parents 9e6290f + c9e36e7 commit 0af6deb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/toml/tests/testsuite/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn table() {
}
.to_string(),
"\"foo.bar\" = 2\n\
\"foo\\\"bar\" = 2\n"
'foo\"bar' = 2\n"
);
assert_eq!(
map! {
Expand Down
3 changes: 3 additions & 0 deletions crates/toml_edit/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ fn infer_style(value: &str) -> (StringStyle, bool) {
}
match ch {
'\t' => {}
'"' => {
prefer_literal = true;
}
'\\' => {
prefer_literal = true;
}
Expand Down
8 changes: 2 additions & 6 deletions crates/toml_edit/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
30 changes: 30 additions & 0 deletions crates/toml_edit/tests/testsuite/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 0af6deb

Please sign in to comment.