#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MatcherTier {
Fixed,
Byte,
Literal,
LiteralSet,
Field,
Regex,
Backtracking,
}
impl MatcherTier {
pub const ALL: [Self; 7] = [
Self::Fixed,
Self::Byte,
Self::Literal,
Self::LiteralSet,
Self::Field,
Self::Regex,
Self::Backtracking,
];
#[must_use]
pub const fn index(self) -> usize {
match self {
Self::Fixed => 0,
Self::Byte => 1,
Self::Literal => 2,
Self::LiteralSet => 3,
Self::Field => 4,
Self::Regex => 5,
Self::Backtracking => 6,
}
}
#[must_use]
pub const fn class(self) -> u8 {
match self {
Self::Fixed => 0,
Self::Byte | Self::Literal | Self::LiteralSet => 1,
Self::Field => 2,
Self::Regex => 3,
Self::Backtracking => 4,
}
}
#[must_use]
pub const fn rank(self) -> u8 {
match self {
Self::Fixed => 7,
Self::Byte => 6,
Self::Literal => 5,
Self::LiteralSet => 4,
Self::Field => 3,
Self::Regex => 2,
Self::Backtracking => 1,
}
}
#[must_use]
pub const fn typical_budget_ns(self) -> Option<u64> {
match self {
Self::Byte => Some(30),
Self::Literal => Some(200),
Self::LiteralSet => Some(500),
Self::Fixed | Self::Field | Self::Regex | Self::Backtracking => None,
}
}
}
impl std::fmt::Display for MatcherTier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Fixed => "Fixed",
Self::Byte => "Byte",
Self::Literal => "Literal",
Self::LiteralSet => "LiteralSet",
Self::Field => "Field",
Self::Regex => "Regex",
Self::Backtracking => "Backtracking",
})
}
}
#[cfg(test)]
mod tests {
use super::MatcherTier;
#[test]
fn all_is_in_index_order_and_ranks_fall() {
for (i, tier) in MatcherTier::ALL.iter().enumerate() {
assert_eq!(tier.index(), i, "{tier}");
}
for pair in MatcherTier::ALL.windows(2) {
assert!(
pair[0].rank() > pair[1].rank(),
"{} vs {}",
pair[0],
pair[1]
);
assert!(
pair[0].class() <= pair[1].class(),
"{} vs {}",
pair[0],
pair[1]
);
}
assert_eq!(MatcherTier::Fixed.rank(), 7);
assert_eq!(MatcherTier::Backtracking.rank(), 1);
}
#[test]
fn classes_match_the_design_table() {
let classes: Vec<u8> = MatcherTier::ALL.iter().map(|t| t.class()).collect();
assert_eq!(classes, [0, 1, 1, 1, 2, 3, 4]);
}
}