mailidator 0.1.0

A lightweight Rust library for checking email address misspellings
Documentation
//! Basic usage example for the email spell checker library

use mailidator::{Config, Mailidator};

fn main() {
    println!("Email Spell Checker - Basic Usage Examples\n");

    // Create a default spell checker
    let checker = Mailidator::default();

    // Example 1: Common Gmail typos
    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");
            }
        }
    }

    // Example 2: Yahoo and other provider typos
    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!");
        }
    }

    // Example 3: Valid emails (should not suggest corrections)
    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)");
            }
        }
    }

    // Example 4: Custom configuration
    println!("\n=== Example 4: Custom Configuration ===");

    let mut config = Config::default();
    // Add custom domains for a specific organization
    config.domains.extend(vec![
        "company.org".to_string(),
        "mycompany.biz".to_string(),
        "internal.local".to_string(),
    ]);
    // Make matching stricter
    config.threshold = 0.3;

    let custom_checker = Mailidator::new(config);

    let custom_emails = vec![
        "employee@compny.org", // Should suggest company.org
        "user@mycompay.biz",   // Should suggest mycompany.biz
        "admin@internl.local", // Should suggest internal.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}");
        }
    }

    // Example 5: Practical usage pattern
    println!("\n=== Example 5: Practical Usage Pattern ===");

    fn validate_email_with_suggestion(email: &str) -> Result<String, String> {
        let checker = Mailidator::default();

        // Basic format validation
        if !email.contains('@') || email.starts_with('@') || email.ends_with('@') {
            return Err("Invalid email format".to_string());
        }

        // Check for typos
        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", // Valid
        "user@gmaik.com", // Typo
        "invalid-email",  // Invalid format
        "user@yahoo.com", // Valid
        "user@yaho.com",  // Typo
    ];

    for email in test_emails {
        match validate_email_with_suggestion(email) {
            Ok(valid_email) => {
                println!("✓ '{valid_email}' is valid");
            }
            Err(error) => {
                println!("✗ '{email}': {error}");
            }
        }
    }
}