Mailidator
A lightweight Rust library for checking email address misspellings and suggesting corrections.
Installation
Add this to your Cargo.toml:
[dependencies]
mailidator = "0.1.0"
mailidator = { version = "0.1.0", features = ["serde"] }
Basic Usage
use mailidator::Mailidator;
fn main() {
let checker = Mailidator::default();
if let Some(suggestion) = checker.check("user@gmaik.com") {
println!("Did you mean: {}?", suggestion.full());
println!("User: {}", suggestion.address());
println!("Domain: {}", suggestion.domain());
}
assert!(checker.check("user@gmail.com").is_none());
}
Custom Configuration
use mailidator::{Mailidator, Config};
fn main() {
let mut config = Config::default();
config.domains.extend(vec![
"company.org".to_string(),
"internal.local".to_string(),
]);
config.threshold = 0.3;
let checker = Mailidator::new(config);
if let Some(suggestion) = checker.check("user@compny.org") {
println!("Suggestion: {}", suggestion.full());
}
}
Practical Example
use mailidator::Mailidator;
fn validate_email_with_suggestion(email: &str) -> Result<String, String> {
let checker = Mailidator::default();
if !email.contains('@') || email.starts_with('@') || email.ends_with('@') {
return Err("Invalid email format".to_string());
}
match checker.check(email) {
Some(suggestion) => {
Err(format!("Did you mean '{}'?", suggestion.full()))
}
None => {
Ok(email.to_string())
}
}
}
fn main() {
let test_emails = vec![
"user@gmail.com", "user@gmaik.com", "invalid-email", ];
for email in test_emails {
match validate_email_with_suggestion(email) {
Ok(valid_email) => {
println!("✓ '{}' is valid", valid_email);
}
Err(error) => {
println!("✗ '{}': {}", email, error);
}
}
}
}
Supported Domains
This library supports the following popular domains:
Major Providers
- Gmail (gmail.com)
- Yahoo (yahoo.com, yahoo.co.jp, yahoo.co.uk, etc.)
- Outlook/Hotmail (outlook.com, hotmail.com)
- iCloud (icloud.com)
- AOL (aol.com)
Other Providers
- ProtonMail, Zoho, Fastmail, Tutanota
- Regional variations (.co.uk, .co.jp, etc.)
- Corporate and educational domains
API Reference
Mailidator
impl Mailidator {
pub fn default() -> Self
pub fn new(config: Config) -> Self
pub fn check(&self, email: &str) -> Option<Suggestion>
}
Suggestion
impl Suggestion {
pub fn address(&self) -> &str
pub fn domain(&self) -> &str
pub fn full(&self) -> String
}
Config
pub struct Config {
pub domains: Vec<String>, pub second_level_domains: Vec<String>, pub top_level_domains: Vec<String>, pub threshold: f64, }
License
Apache-2.0 License
Contributing
Bug reports, feature requests, and pull requests are welcome!
References