fn _check()

in crates/core/src/models/filter.rs [373:433]


  fn _check(
    &self, filter: Filter, node: Node, rule_store: &mut RuleStore,
    substitutions: &HashMap<String, String>,
  ) -> bool {
    let mut node_to_check = node;
    let instantiated_filter = filter.instantiate(substitutions);

    debug!(
      "Applying filter {:?} against potential match {:?}",
      instantiated_filter,
      node.to_sexp()
    );

    // Check fact_filter: at least one recorded fact must match all key-value pairs
    let fact_filter = instantiated_filter.fact_filter();
    if !fact_filter.is_empty() {
      let any_fact_matches = self.facts().iter().any(|fact| {
        !fact.voided()
          && fact_filter
            .iter()
            .all(|(k, v)| fact.data().get(k).map(String::as_str) == Some(v.as_str()))
      });
      if !any_fact_matches {
        return false;
      }
    }
    if *filter.child_count() != default_child_count() {
      return node.named_child_count() == (*filter.child_count() as usize);
    }

    if *filter.sibling_count() != default_sibling_count() {
      return node.parent().unwrap().named_child_count() == (*filter.sibling_count() as usize);
    }

    // Check if no ancestor matches the query for not_enclosing_node
    if !self._check_not_enclosing_node(rule_store, node_to_check, &instantiated_filter) {
      return false;
    }
    // If an enclosing node is provided
    let query = instantiated_filter.enclosing_node();
    if !query.pattern().is_empty() {
      if let Some(result) = self._match_ancestor(rule_store, node_to_check, query) {
        node_to_check = result;
      } else {
        return false;
      }
    }

    // If an outermost enclosing node is provided
    let query = instantiated_filter.outermost_enclosing_node();
    if !query.pattern().is_empty() {
      if let Some(result) = self._match_outermost_ancestor(rule_store, node_to_check, query) {
        node_to_check = result;
      } else {
        return false;
      }
    }

    self._check_filter_not_contains(&instantiated_filter, rule_store, &node_to_check)
      && self._check_filter_contains(&instantiated_filter, rule_store, &node_to_check)
  }