Struct pact_models::matchingrules::RuleList
source · 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: RuleLogicRule logic to use to evaluate multiple rules
cascaded: boolIf this rule list has matched the exact path or if it has cascaded (i.e. is a parent)
Implementations§
source§impl RuleList
impl RuleList
sourcepub fn empty(rule_logic: RuleLogic) -> RuleList
pub fn empty(rule_logic: RuleLogic) -> RuleList
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);
}sourcepub fn new(rule: MatchingRule) -> RuleList
pub fn new(rule: MatchingRule) -> RuleList
Creates a new rule list with the single matching rule
sourcepub fn type_matcher_defined(&self) -> bool
pub fn type_matcher_defined(&self) -> bool
If there is a type matcher defined for the rule list
sourcepub fn values_matcher_defined(&self) -> bool
pub fn values_matcher_defined(&self) -> bool
If the values matcher is defined for the rule list
sourcepub fn add_rule(&mut self, rule: &MatchingRule)
pub fn add_rule(&mut self, rule: &MatchingRule)
Add a matching rule to the rule list
sourcepub fn as_cascaded(&self, b: bool) -> RuleList
pub fn as_cascaded(&self, b: bool) -> RuleList
If this rule list has matched the exact path or if it has cascaded (i.e. is a parent)
Trait Implementations§
source§impl PartialEq<RuleList> for RuleList
impl PartialEq<RuleList> for RuleList
impl Eq for RuleList
impl StructuralEq for RuleList
Auto Trait Implementations§
impl RefUnwindSafe for RuleList
impl Send for RuleList
impl Sync for RuleList
impl Unpin for RuleList
impl UnwindSafe for RuleList
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.