use serde::Deserialize;
use crate::rule_set::rule::when::request::rule_op::RuleOp;
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum HeaderOperator {
Equal,
NotEqual,
StartsWith,
NotStartsWith,
EndsWith,
NotEndsWith,
Contains,
NotContains,
WildCard,
Regex,
NotRegex,
Exists,
Absent,
}
impl Default for HeaderOperator {
fn default() -> Self {
Self::Equal
}
}
impl std::fmt::Display for HeaderOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Equal => write!(f, " == "),
Self::NotEqual => write!(f, " != "),
Self::StartsWith => write!(f, " starts with "),
Self::NotStartsWith => write!(f, " does not start with "),
Self::EndsWith => write!(f, " ends with "),
Self::NotEndsWith => write!(f, " does not end with "),
Self::Contains => write!(f, " contains "),
Self::NotContains => write!(f, " does not contain "),
Self::WildCard => write!(f, " wild card matches "),
Self::Regex => write!(f, " matches regex "),
Self::NotRegex => write!(f, " does not match regex "),
Self::Exists => write!(f, " exists"),
Self::Absent => write!(f, " absent"),
}
}
}
impl HeaderOperator {
pub fn as_str(&self) -> &'static str {
match self {
Self::Equal => "equal",
Self::NotEqual => "not_equal",
Self::StartsWith => "starts_with",
Self::NotStartsWith => "not_starts_with",
Self::EndsWith => "ends_with",
Self::NotEndsWith => "not_ends_with",
Self::Contains => "contains",
Self::NotContains => "not_contains",
Self::WildCard => "wild_card",
Self::Regex => "regex",
Self::NotRegex => "not_regex",
Self::Exists => "exists",
Self::Absent => "absent",
}
}
pub fn to_rule_op(&self) -> RuleOp {
match self {
Self::Equal => RuleOp::Equal,
Self::NotEqual => RuleOp::NotEqual,
Self::StartsWith => RuleOp::StartsWith,
Self::NotStartsWith => RuleOp::NotStartsWith,
Self::EndsWith => RuleOp::EndsWith,
Self::NotEndsWith => RuleOp::NotEndsWith,
Self::Contains => RuleOp::Contains,
Self::NotContains => RuleOp::NotContains,
Self::WildCard => RuleOp::WildCard,
Self::Regex => RuleOp::Regex,
Self::NotRegex => RuleOp::NotRegex,
Self::Exists | Self::Absent => {
panic!("HeaderOperator::to_rule_op called on presence operator {:?}", self)
}
}
}
}