fn validate()

in src/models/filter.rs [144:204]


  fn validate(&self) -> Result<(), String> {
    // Only allow users to set either contains or not_contains, but not both
    if *self.contains() != default_contains_query()
      && *self.not_contains() != default_not_contains_queries()
    {
      return Err(
        "Invalid Filter Argument. `contains` and `not_contains` cannot be set at the same time !!! Please use two filters instead."
          .to_string(),
      );
    }

    if self.at_least > self.at_most {
      return Err(
        "Invalid Filter Argument. `at_least` should be less than or equal to `at_most` !!!"
          .to_string(),
      );
    }

    // If the user set `at_least` or `at_most`, then the contains query cannot be empty
    if (self.at_least != default_contains_at_least() || self.at_most != default_contains_at_most())
      && self.contains().pattern().is_empty()
    {
      return Err(
        "Invalid Filter Argument. `at_least` or `at_most` is set, but `contains` is empty !!!"
          .to_string(),
      );
    }

    if *self.enclosing_node() != default_enclosing_node() {
      self.enclosing_node().validate()?
    }

    if *self.outermost_enclosing_node() != default_enclosing_node() {
      self.outermost_enclosing_node().validate()?
    }

    if *self.not_enclosing_node() != default_not_enclosing_node() {
      self.not_enclosing_node().validate()?
    }

    if *self.contains() != default_contains_query() {
      self.contains().validate()?
    }

    if *self.not_contains() != default_not_contains_queries() {
      self.not_contains().iter().try_for_each(|x| x.validate())?
    }

    if (*self.child_count() != default_child_count()
      || *self.sibling_count() != default_sibling_count())
      && (*self.enclosing_node() != default_enclosing_node()
        || *self.not_enclosing_node() != default_not_enclosing_node()
        || *self.outermost_enclosing_node() != default_enclosing_node()
        || *self.contains() != default_contains_query()
        || *self.not_contains() != default_not_contains_queries())
    {
      return Err("The child/sibling count operator is not compatible with (not) enclosing node and (not) contains operator".to_string());
    }

    Ok(())
  }