adds iter_(read/write)_variable_identifiers

seperate iterator returns for read and write variable identifiers.
this is useful in our project and i suspect it might be of use for
others too.
This commit is contained in:
Tobias Schmitt 2022-07-21 21:12:26 +02:00
parent a4f49de71a
commit 532e2ed770
2 changed files with 53 additions and 1 deletions

View File

@ -99,6 +99,48 @@ impl Node {
}) })
} }
/// Returns an iterator over all read variable identifiers in this expression.
/// Each occurrence of a variable identifier is returned separately.
///
/// # Examples
///
/// ```rust
/// use evalexpr::*;
///
/// let tree = build_operator_tree("d=a + f(b + c)").unwrap(); // Do proper error handling here
/// let mut iter = tree.iter_read_variable_identifiers();
/// assert_eq!(iter.next(), Some("a"));
/// assert_eq!(iter.next(), Some("b"));
/// assert_eq!(iter.next(), Some("c"));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter_read_variable_identifiers(&self) -> impl Iterator<Item = &str> {
self.iter().filter_map(|node| match node.operator() {
Operator::VariableIdentifierRead { identifier } => Some(identifier.as_str()),
_ => None,
})
}
/// Returns an iterator over all write variable identifiers in this expression.
/// Each occurrence of a variable identifier is returned separately.
///
/// # Examples
///
/// ```rust
/// use evalexpr::*;
///
/// let tree = build_operator_tree("d = a + f(b + c)").unwrap(); // Do proper error handling here
/// let mut iter = tree.iter_write_variable_identifiers();
/// assert_eq!(iter.next(), Some("d"));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter_write_variable_identifiers(&self) -> impl Iterator<Item = &str> {
self.iter().filter_map(|node| match node.operator() {
Operator::VariableIdentifierWrite { identifier } => Some(identifier.as_str()),
_ => None,
})
}
/// Returns an iterator over all function identifiers in this expression. /// Returns an iterator over all function identifiers in this expression.
/// Each occurrence of a function identifier is returned separately. /// Each occurrence of a function identifier is returned separately.
/// ///

View File

@ -1616,16 +1616,26 @@ fn test_error_constructors() {
#[test] #[test]
fn test_iterators() { fn test_iterators() {
let tree = build_operator_tree("5 + 3 + fun(4) + var").unwrap(); let tree = build_operator_tree("writevar = 5 + 3 + fun(4) + var").unwrap();
let mut iter = tree.iter_identifiers(); let mut iter = tree.iter_identifiers();
assert_eq!(iter.next(), Some("writevar"));
assert_eq!(iter.next(), Some("fun")); assert_eq!(iter.next(), Some("fun"));
assert_eq!(iter.next(), Some("var")); assert_eq!(iter.next(), Some("var"));
assert_eq!(iter.next(), None); assert_eq!(iter.next(), None);
let mut iter = tree.iter_variable_identifiers(); let mut iter = tree.iter_variable_identifiers();
assert_eq!(iter.next(), Some("writevar"));
assert_eq!(iter.next(), Some("var")); assert_eq!(iter.next(), Some("var"));
assert_eq!(iter.next(), None); assert_eq!(iter.next(), None);
let mut iter = tree.iter_read_variable_identifiers();
assert_eq!(iter.next(), Some("var"));
assert_eq!(iter.next(), None);
let mut iter = tree.iter_write_variable_identifiers();
assert_eq!(iter.next(), Some("writevar"));
assert_eq!(iter.next(), None);
let mut iter = tree.iter_function_identifiers(); let mut iter = tree.iter_function_identifiers();
assert_eq!(iter.next(), Some("fun")); assert_eq!(iter.next(), Some("fun"));
assert_eq!(iter.next(), None); assert_eq!(iter.next(), None);