Expand description
§match-domain: Rapid checker for prefix and suffix matching of domain names
This crate provides a high-performance domain name matching library using double-array trie data structures. It enables you to efficiently check if a given domain name matches against a collection of prefix or suffix patterns.
§Key Features
- Fast matching: Uses double-array trie (Cedar) for O(m) lookup performance, where m is the length of the given domain to be matched, independent of the number of patterns
- Flexible patterns: Supports both prefix (
www.example.*) and suffix (*.example.com) matching - Multiple match types: Simple boolean matching, all matches, and longest match queries
- Memory efficient: Compact representation using trie data structures
- Thread safe: All operations are read-only after initialization
§Supported Pattern Types
- Exact domain:
example.commatches exactlyexample.com - Suffix wildcard:
*.example.commatchesapi.example.com,www.example.com, etc. - Prefix wildcard:
www.example.*matcheswww.example.com,www.example.org, etc.
§Quick Start
use match_domain::DomainMatchingRule;
// Create a domain matching rule from a list of patterns
let rule = DomainMatchingRule::try_from(vec![
"*.google.com", // Suffix pattern
"www.example.*", // Prefix pattern
"exact.domain.net", // Exact match
]).unwrap();
// Check if a domain matches any pattern
assert!(rule.is_matched("api.google.com")); // Matches *.google.com
assert!(rule.is_matched("www.example.org")); // Matches www.example.*
assert!(rule.is_matched("exact.domain.net")); // Exact match
assert!(!rule.is_matched("unmatched.domain.com")); // No match§Advanced Usage
§Finding All Matches
use match_domain::DomainMatchingRule;
let rule = DomainMatchingRule::try_from(vec![
"google.com",
"*.google.com",
"com",
]).unwrap();
// Get all matching suffixes (returns reversed strings)
let matches = rule.find_suffix_match_all("api.google.com");
assert_eq!(matches.len(), 2); // Matches both "google.com" and "com"§Finding Longest Match
use match_domain::DomainMatchingRule;
let rule = DomainMatchingRule::try_from(vec![
"www.example.*",
"www.*",
]).unwrap();
// Get the most specific (longest) matching prefix
let longest = rule.find_prefix_match_longest("www.example.com");
assert_eq!(longest, Some("www.example".to_string())); // More specific than "www"§Important Notes
- Domain names must be provided in lowercase
- Domain names should not contain leading dots
- Suffix matching returns reversed strings for internal efficiency
- The crate is thread-safe after initialization
Structs§
- Domain
Matching Rule - A struct representing a prefix-or-suffix matching rule. This struct checks if a domain is contained in a list of prefixes or suffixes with longest match rule.
Enums§
- Error
- Describes things that can go wrong in the match-domain
Constants§
- REGEXP_
DOMAIN_ OR_ PREFIX - Regular expression for domain or prefix