use mailidator::{Config, Mailidator};
fn main() {
println!("Email Spell Checker - Basic Usage Examples\n");
let checker = Mailidator::default();
println!("=== Example 1: Gmail Typos ===");
let gmail_typos = vec![
"user@gmaik.com",
"john.doe@gmial.com",
"test@gnail.com",
"admin@gmail.co",
];
for email in gmail_typos {
match checker.check(email) {
Some(suggestion) => {
println!("Input: {} -> Suggestion: {}", email, suggestion.full());
println!(
" Address: {}, Domain: {}",
suggestion.address(),
suggestion.domain()
);
}
None => {
println!("Input: {email} -> No suggestion needed");
}
}
}
println!("\n=== Example 2: Other Provider Typos ===");
let other_typos = vec![
"user@yaho.com",
"test@outlok.com",
"admin@hotmial.com",
"contact@icluod.com",
];
for email in other_typos {
if let Some(suggestion) = checker.check(email) {
println!("Did you mean {} instead of {}?", suggestion.full(), email);
} else {
println!("{email} looks correct!");
}
}
println!("\n=== Example 3: Valid Emails ===");
let valid_emails = vec![
"user@gmail.com",
"test@yahoo.com",
"admin@outlook.com",
"contact@hotmail.com",
];
for email in valid_emails {
match checker.check(email) {
Some(suggestion) => {
println!("Unexpected suggestion for {}: {}", email, suggestion.full());
}
None => {
println!("{email} ✓ (valid)");
}
}
}
println!("\n=== Example 4: Custom Configuration ===");
let mut config = Config::default();
config.domains.extend(vec![
"company.org".to_string(),
"mycompany.biz".to_string(),
"internal.local".to_string(),
]);
config.threshold = 0.3;
let custom_checker = Mailidator::new(config);
let custom_emails = vec![
"employee@compny.org", "user@mycompay.biz", "admin@internl.local", ];
for email in custom_emails {
if let Some(suggestion) = custom_checker.check(email) {
println!("Custom suggestion: {} -> {}", email, suggestion.full());
} else {
println!("No custom suggestion for: {email}");
}
}
println!("\n=== Example 5: Practical Usage Pattern ===");
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()),
}
}
let test_emails = vec![
"user@gmail.com", "user@gmaik.com", "invalid-email", "user@yahoo.com", "user@yaho.com", ];
for email in test_emails {
match validate_email_with_suggestion(email) {
Ok(valid_email) => {
println!("✓ '{valid_email}' is valid");
}
Err(error) => {
println!("✗ '{email}': {error}");
}
}
}
}