Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(edit): Preserve new key's formatting when inserting #788

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions crates/toml_edit/src/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,37 @@ impl InlineTable {

/// Inserts a key-value pair into the map.
pub fn insert(&mut self, key: impl Into<InternalString>, value: Value) -> Option<Value> {
let key = Key::new(key.into());
use indexmap::map::MutableEntryKey;
let key = Key::new(key);
let value = Item::Value(value);
self.items
.insert(key, value)
.and_then(|old| old.into_value().ok())
match self.items.entry(key.clone()) {
indexmap::map::Entry::Occupied(mut entry) => {
entry.key_mut().fmt();
let old = std::mem::replace(entry.get_mut(), value);
old.into_value().ok()
}
indexmap::map::Entry::Vacant(entry) => {
entry.insert(value);
None
}
}
}

/// Inserts a key-value pair into the map.
pub fn insert_formatted(&mut self, key: &Key, value: Value) -> Option<Value> {
let key = key.to_owned();
use indexmap::map::MutableEntryKey;
let value = Item::Value(value);
self.items
.insert(key, value)
.and_then(|old| old.into_value().ok())
match self.items.entry(key.clone()) {
indexmap::map::Entry::Occupied(mut entry) => {
*entry.key_mut() = key.clone();
let old = std::mem::replace(entry.get_mut(), value);
old.into_value().ok()
}
indexmap::map::Entry::Vacant(entry) => {
entry.insert(value);
None
}
}
}

/// Removes an item given the key.
Expand Down
27 changes: 24 additions & 3 deletions crates/toml_edit/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,35 @@ impl Table {

/// Inserts a key-value pair into the map.
pub fn insert(&mut self, key: &str, item: Item) -> Option<Item> {
use indexmap::map::MutableEntryKey;
let key = Key::new(key);
self.items.insert(key, item)
match self.items.entry(key.clone()) {
indexmap::map::Entry::Occupied(mut entry) => {
entry.key_mut().fmt();
let old = std::mem::replace(entry.get_mut(), item);
Some(old)
}
indexmap::map::Entry::Vacant(entry) => {
entry.insert(item);
None
}
}
}

/// Inserts a key-value pair into the map.
pub fn insert_formatted(&mut self, key: &Key, item: Item) -> Option<Item> {
let key = key.to_owned();
self.items.insert(key, item)
use indexmap::map::MutableEntryKey;
match self.items.entry(key.clone()) {
indexmap::map::Entry::Occupied(mut entry) => {
*entry.key_mut() = key.clone();
let old = std::mem::replace(entry.get_mut(), item);
Some(old)
}
indexmap::map::Entry::Vacant(entry) => {
entry.insert(item);
None
}
}
}

/// Removes an item given the key.
Expand Down
46 changes: 46 additions & 0 deletions crates/toml_edit/tests/testsuite/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,3 +980,49 @@ fn sorting_with_references() {
let mut array = toml_edit::Array::from_iter(values);
array.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
}

#[test]
fn table_str_key_whitespace() {
let mut document = "bookmark = 1010".parse::<DocumentMut>().unwrap();

let key: &str = "bookmark";

document.insert(key, array());
let table = document[key].as_array_of_tables_mut().unwrap();

let mut bookmark_table = Table::new();
bookmark_table["name"] = value("test.swf".to_owned());
table.push(bookmark_table);

assert_data_eq!(
document.to_string(),
str![[r#"
[[bookmark]]
name = "test.swf"

"#]]
);
}

#[test]
fn table_key_decor_whitespace() {
let mut document = "bookmark = 1010".parse::<DocumentMut>().unwrap();

let key = Key::parse(" bookmark ").unwrap().remove(0);

document.insert_formatted(&key, array());
let table = document[&key].as_array_of_tables_mut().unwrap();

let mut bookmark_table = Table::new();
bookmark_table["name"] = value("test.swf".to_owned());
table.push(bookmark_table);

assert_data_eq!(
document.to_string(),
str![[r#"
[[ bookmark ]]
name = "test.swf"

"#]]
);
}
Loading