# `match-domain`: Rapid checker for the prefix and suffix matching of domain names, written in Rust
[](https://crates.io/crates/match-domain)
[](https://docs.rs/match-domain)
[](LICENSE)

Double-array trie based domain matcher, written in Rust.
This enables you to check if the given domain name matches the prefix or suffix of the domain name in the trie.
## Usage
### Basic Domain Matching
```rust
use match_domain::DomainMatchingRule;
let domain_matching_rule = DomainMatchingRule::try_from(vec![
"www.google.com".to_string(),
"*.google.com".to_string(),
"yahoo.co.*".to_string(),
])
.unwrap();
assert!(domain_matching_rule.is_matched("wwxx.google.com"));
assert!(domain_matching_rule.is_matched("yahoo.co.jp"));
assert!(!domain_matching_rule.is_matched("www.yahoo.com"));
assert!(!domain_matching_rule.is_matched("www.yahoo.co.jp"));
```
### Advanced Matching APIs
The library provides several APIs for more granular matching:
#### Suffix Matching
```rust
use match_domain::DomainMatchingRule;
let rule = DomainMatchingRule::try_from(vec![
"google.com".to_string(),
"*.google.com".to_string(),
"com".to_string(),
])
.unwrap();
// Check if any suffix matches
assert!(rule.find_suffix_match("api.google.com"));
// Get all matching suffixes
let all_matches = rule.find_suffix_match_all("api.google.com");
// Returns reversed strings: ["moc.elgoog", "moc"]
// Get the longest matching suffix
let longest_match = rule.find_suffix_match_longest("api.google.com");
assert_eq!(longest_match, Some("moc.elgoog".to_string())); // "google.com" is longer than "com"
```
#### Prefix Matching
```rust
use match_domain::DomainMatchingRule;
let rule = DomainMatchingRule::try_from(vec![
"www.example.*".to_string(),
"www.*".to_string(),
"api.service.*".to_string(),
])
.unwrap();
// Check if any prefix matches
assert!(rule.find_prefix_match("www.example.com"));
// Get all matching prefixes
let all_matches = rule.find_prefix_match_all("www.example.com");
// Returns: ["www.example", "www"]
// Get the longest matching prefix
let longest_match = rule.find_prefix_match_longest("www.example.com");
assert_eq!(longest_match, Some("www.example".to_string())); // "www.example" is longer than "www"
```
#### Comprehensive Example
```rust
use match_domain::DomainMatchingRule;
let rule = DomainMatchingRule::try_from(vec![
"*.google.com".to_string(), // Suffix pattern
"www.example.*".to_string(), // Prefix pattern
"exact.domain.net".to_string(), // Exact match
])
.unwrap();
// Test different domain matching scenarios
assert!(rule.is_matched("api.google.com")); // Matches suffix pattern
assert!(rule.is_matched("www.example.org")); // Matches prefix pattern
assert!(rule.is_matched("exact.domain.net")); // Exact match
// Get detailed matching information
let suffix_matches = rule.find_suffix_match_all("mail.google.com");
let prefix_matches = rule.find_prefix_match_all("www.example.co.uk");
// Find longest matches for better specificity
let longest_suffix = rule.find_suffix_match_longest("subdomain.google.com");
let longest_prefix = rule.find_prefix_match_longest("www.example.multiple.tlds");
```
### Important Notes
For all matching methods:
- The argument `domain_name` should be in lowercase
- The argument `domain_name` should not contain a leading dot
- Suffix matching returns reversed strings (for internal implementation efficiency)
- Longest match functions return the most specific match when multiple patterns apply