use std::iter::FromIterator;
use crate::{nodes::Identifier, process::NodeProcessor};
pub struct FindVariables<'a> {
variables: Vec<&'a str>,
usage_found: bool,
}
impl<'a> FindVariables<'a> {
pub fn new(variable: &'a str) -> Self {
Self {
variables: vec![variable],
usage_found: false,
}
}
#[inline]
pub fn has_found_usage(&self) -> bool {
self.usage_found
}
}
impl<'a> FromIterator<&'a str> for FindVariables<'a> {
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
Self {
variables: iter.into_iter().collect(),
usage_found: false,
}
}
}
impl NodeProcessor for FindVariables<'_> {
fn process_variable_expression(&mut self, variable: &mut Identifier) {
if !self.usage_found {
let name = variable.get_name();
self.usage_found = self.variables.iter().any(|v| *v == name)
}
}
}