Skip to content

Commit

Permalink
toml_edit: add retain to arrays and tables
Browse files Browse the repository at this point in the history
added to: `Table`, `InlineTable`, `Array` and `ArrayOfTables`
  • Loading branch information
marcospb19 committed Jun 28, 2023
1 parent 22fb58e commit b8ab23f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
14 changes: 14 additions & 0 deletions crates/toml_edit/src/array.rs
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
16 changes: 15 additions & 1 deletion crates/toml_edit/src/array_of_tables.rs
@@ -1,6 +1,6 @@
use std::iter::FromIterator;

use crate::{Array, Item, Table};
use crate::{Array, Item, Table, Value};

/// Type representing a TOML array of tables
#[derive(Clone, Debug, Default)]
Expand Down 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 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));
}
}

/// An iterator type over `ArrayOfTables`'s values.
Expand Down
18 changes: 18 additions & 0 deletions crates/toml_edit/src/inline_table.rs
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
18 changes: 18 additions & 0 deletions crates/toml_edit/src/table.rs
Expand Up @@ -397,6 +397,24 @@ 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, 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 Table {
Expand Down

0 comments on commit b8ab23f

Please sign in to comment.