pub struct RuleList {
    pub rules: Vec<MatchingRule>,
    pub rule_logic: RuleLogic,
    pub cascaded: bool,
}
Expand description

Data structure for representing a list of rules and the logic needed to combine them

Fields§

§rules: Vec<MatchingRule>

List of rules to apply

§rule_logic: RuleLogic

Rule logic to use to evaluate multiple rules

§cascaded: bool

If this rule list has matched the exact path or if it has cascaded (i.e. is a parent)

Implementations§

Creates a new empty rule list

Examples found in repository?
src/matchingrules/mod.rs (line 886)
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  fn default() -> Self {
    RuleList::empty(RuleLogic::And)
  }
}

/// Category that the matching rule is applied to
#[derive(PartialEq, Debug, Clone, Eq, Hash, PartialOrd, Ord)]
pub enum Category {
  /// Request Method
  METHOD,
  /// Request Path
  PATH,
  /// Request/Response Header
  HEADER,
  /// Request Query Parameter
  QUERY,
  /// Body
  BODY,
  /// Response Status
  STATUS,
  /// Message contents (body)
  CONTENTS,
  /// Message metadata
  METADATA
}

impl FromStr for Category {
  type Err = String;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    match s.to_lowercase().as_str() {
      "method" => Ok(Category::METHOD),
      "path" => Ok(Category::PATH),
      "header" => Ok(Category::HEADER),
      "query" => Ok(Category::QUERY),
      "body" => Ok(Category::BODY),
      "status" => Ok(Category::STATUS),
      "contents" => Ok(Category::CONTENTS),
      "metadata" => Ok(Category::METADATA),
      _ => Err(format!("'{}' is not a valid Category", s))
    }
  }
}

impl <'a> Into<&'a str> for Category {
  fn into(self) -> &'a str {
    match self {
      Category::METHOD => "method",
      Category::PATH => "path",
      Category::HEADER => "header",
      Category::QUERY => "query",
      Category::BODY => "body",
      Category::STATUS => "status",
      Category::CONTENTS => "contents",
      Category::METADATA => "metadata"
    }
  }
}

impl Into<String> for Category {
  fn into(self) -> String {
    self.to_string()
  }
}

impl <'a> From<&'a str> for Category {
  fn from(s: &'a str) -> Self {
    Category::from_str(s).unwrap_or_default()
  }
}

impl From<String> for Category {
  fn from(s: String) -> Self {
    Category::from_str(&s).unwrap_or_default()
  }
}

impl Default for Category {
  fn default() -> Self {
    Category::BODY
  }
}

impl Display for Category {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    let s: &str = self.clone().into();
    write!(f, "{}", s)
  }
}

/// Data structure for representing a category of matching rules
#[derive(Debug, Clone, Eq, Default)]
pub struct MatchingRuleCategory {
  /// Name of the category
  pub name: Category,
  /// Matching rules for this category
  pub rules: HashMap<DocPath, RuleList>
}

impl MatchingRuleCategory {
  /// Creates an empty category
  pub fn empty<S>(name: S) -> MatchingRuleCategory
    where S: Into<Category>
  {
    MatchingRuleCategory {
      name: name.into(),
      rules: hashmap! {},
    }
  }

  /// Creates a default category
  pub fn equality<S>(name: S) -> MatchingRuleCategory
    where S: Into<Category>
  {
    MatchingRuleCategory {
      name: name.into(),
      rules: hashmap! {
        DocPath::empty() => RuleList::equality()
      }
    }
  }

  /// If the matching rules in the category are empty
  pub fn is_empty(&self) -> bool {
    self.rules.is_empty()
  }

  /// If the matching rules in the category are not empty
  pub fn is_not_empty(&self) -> bool {
    !self.rules.is_empty()
  }

  /// Adds a rule from the Value representation
  pub fn rule_from_json(
    &mut self,
    key: DocPath,
    matcher_json: &Value,
    rule_logic: RuleLogic,
  ) -> anyhow::Result<()> {
    let matching_rule = MatchingRule::from_json(matcher_json)
      .with_context(|| format!("Could not parse matcher JSON {:?}", matcher_json))?;

    let rules = self.rules.entry(key)
      .or_insert_with(|| RuleList::empty(rule_logic));
    rules.rules.push(matching_rule);
    Ok(())
  }

  /// Adds a rule to this category
  pub fn add_rule(
    &mut self,
    key: DocPath,
    matcher: MatchingRule,
    rule_logic: RuleLogic,
  ) {
    let rules = self.rules.entry(key).or_insert_with(|| RuleList::empty(rule_logic));
    rules.rules.push(matcher);
  }

Creates a default rule list with an equality matcher

Examples found in repository?
src/matchingrules/mod.rs (line 1002)
996
997
998
999
1000
1001
1002
1003
1004
1005
  pub fn equality<S>(name: S) -> MatchingRuleCategory
    where S: Into<Category>
  {
    MatchingRuleCategory {
      name: name.into(),
      rules: hashmap! {
        DocPath::empty() => RuleList::equality()
      }
    }
  }

Creates a new rule list with the single matching rule

If the rule list is empty (has no matchers)

If there is a type matcher defined for the rule list

Examples found in repository?
src/matchingrules/mod.rs (line 1091)
1090
1091
1092
  pub fn type_matcher_defined(&self) -> bool {
    self.rules.values().any(|rule_list| rule_list.type_matcher_defined())
  }

If the values matcher is defined for the rule list

Examples found in repository?
src/matchingrules/mod.rs (line 1096)
1095
1096
1097
  pub fn values_matcher_defined(&self) -> bool {
    self.rules.values().any(|rule_list| rule_list.values_matcher_defined())
  }

Add a matching rule to the rule list

Examples found in repository?
src/matchingrules/mod.rs (line 863)
861
862
863
864
865
  pub fn add_rules(&mut self, rules: &RuleList) {
    for rule in &rules.rules {
      self.add_rule(rule);
    }
  }

If this rule list has matched the exact path or if it has cascaded (i.e. is a parent)

Examples found in repository?
src/matchingrules/mod.rs (line 1058)
1054
1055
1056
1057
1058
1059
1060
  fn max_by_path(&self, path: &[&str]) -> RuleList {
    self.rules.iter().map(|(k, v)| (k, v, k.path_weight(path)))
      .filter(|&(_, _, (w, _))| w > 0)
      .max_by_key(|&(_, _, (w, t))| w * t)
      .map(|(_, v, (_, t))| v.as_cascaded(t != path.len()))
      .unwrap_or_default()
  }

Add all the rules from the list to this list

Examples found in repository?
src/matchingrules/mod.rs (line 1206)
1203
1204
1205
1206
1207
1208
1209
1210
1211
  pub fn add_rules(&mut self, category: MatchingRuleCategory) {
    for (path, rules) in &category.rules {
      if self.rules.contains_key(path) {
        self.rules.get_mut(path).unwrap().add_rules(rules)
      } else {
        self.rules.insert(path.clone(), rules.clone());
      }
    }
  }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more