Skip to content

Commit

Permalink
Merge pull request #576 from marcospb19/toml-edit-add-retain-method-t…
Browse files Browse the repository at this point in the history
…o-collections

toml_edit: add `retain` to `Array`, `Table` and `InlineTable`
  • Loading branch information
epage committed Jul 5, 2023
2 parents bb108a8 + 4b2d3c9 commit c2a36b0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions crates/toml_edit/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ impl Array {
}
}

/// Retains only the values specified by the `keep` predicate.
///
/// In other words, remove all values for which `keep(&value)` returns `false`.
///
/// This method operates in place, visiting each element exactly once in the
/// original order, and preserves the order of the retained elements.
pub fn retain<F>(&mut self, mut keep: F)
where
F: FnMut(&Value) -> bool,
{
self.values
.retain(|item| item.as_value().map(&mut keep).unwrap_or(false));
}

fn value_op<T>(
&mut self,
v: Value,
Expand Down
14 changes: 14 additions & 0 deletions crates/toml_edit/src/array_of_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ impl ArrayOfTables {
pub fn remove(&mut self, index: usize) {
self.values.remove(index);
}

/// Retains only the elements specified by the `keep` predicate.
///
/// In other words, remove all tables for which `keep(&table)` returns `false`.
///
/// This method operates in place, visiting each element exactly once in the
/// original order, and preserves the order of the retained elements.
pub fn retain<F>(&mut self, mut keep: F)
where
F: FnMut(&Table) -> bool,
{
self.values
.retain(|item| item.as_table().map(&mut keep).unwrap_or(false));
}
}

/// An iterator type over `ArrayOfTables`'s values.
Expand Down
18 changes: 18 additions & 0 deletions crates/toml_edit/src/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ impl InlineTable {
kv.value.into_value().ok().map(|value| (key, value))
})
}

/// Retains only the elements specified by the `keep` predicate.
///
/// In other words, remove all pairs `(key, value)` for which
/// `keep(&key, &mut value)` returns `false`.
///
/// The elements are visited in iteration order.
pub fn retain<F>(&mut self, mut keep: F)
where
F: FnMut(&str, &mut Value) -> bool,
{
self.items.retain(|key, item| {
item.value
.as_value_mut()
.map(|value| keep(key, value))
.unwrap_or(false)
});
}
}

impl std::fmt::Display for InlineTable {
Expand Down
14 changes: 14 additions & 0 deletions crates/toml_edit/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,20 @@ impl Table {
pub fn remove_entry(&mut self, key: &str) -> Option<(Key, Item)> {
self.items.shift_remove(key).map(|kv| (kv.key, kv.value))
}

/// Retains only the elements specified by the `keep` predicate.
///
/// In other words, remove all pairs `(key, item)` for which
/// `keep(&key, &mut item)` returns `false`.
///
/// The elements are visited in iteration order.
pub fn retain<F>(&mut self, mut keep: F)
where
F: FnMut(&str, &mut Item) -> bool,
{
self.items
.retain(|key, key_value| keep(key, &mut key_value.value));
}
}

impl std::fmt::Display for Table {
Expand Down

0 comments on commit c2a36b0

Please sign in to comment.