mailidator

Crates.iomailidator
lib.rsmailidator
version0.1.0
created_at2025-08-16 03:50:30.594887+00
updated_at2025-08-16 03:50:30.594887+00
descriptionA lightweight Rust library for checking email address misspellings
homepage
repositoryhttps://github.com/yourusername/mailidator
max_upload_size
id1797897
size57,502
Yuki Okushi (JohnTitor)

documentation

README

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"

# If you need Serde support
mailidator = { version = "0.1.0", features = ["serde"] }

Basic Usage

use mailidator::Mailidator;

fn main() {
    let checker = Mailidator::default();
    
    // Common Gmail typos
    if let Some(suggestion) = checker.check("user@gmaik.com") {
        println!("Did you mean: {}?", suggestion.full());
        println!("User: {}", suggestion.address());
        println!("Domain: {}", suggestion.domain());
        // Output: Did you mean: user@gmail.com?
    }
    
    // No suggestion for correct email addresses
    assert!(checker.check("user@gmail.com").is_none());
}

Custom Configuration

use mailidator::{Mailidator, Config};

fn main() {
    let mut config = Config::default();
    
    // Add organization-specific domains
    config.domains.extend(vec![
        "company.org".to_string(),
        "internal.local".to_string(),
    ]);
    
    // Stricter matching settings
    config.threshold = 0.3;
    
    let checker = Mailidator::new(config);
    
    if let Some(suggestion) = checker.check("user@compny.org") {
        println!("Suggestion: {}", suggestion.full());
        // Output: Suggestion: user@company.org
    }
}

Practical Example

use mailidator::Mailidator;

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())
        }
    }
}

fn main() {
    let test_emails = vec![
        "user@gmail.com",       // Valid
        "user@gmaik.com",       // Typo
        "invalid-email",        // Invalid format
    ];

    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 {
    // Create checker with default settings
    pub fn default() -> Self
    
    // Create checker with custom settings
    pub fn new(config: Config) -> Self
    
    // Check email address and return suggestion if available
    pub fn check(&self, email: &str) -> Option<Suggestion>
}

Suggestion

impl Suggestion {
    // Get the local part (before @)
    pub fn address(&self) -> &str
    
    // Get the suggested domain
    pub fn domain(&self) -> &str
    
    // Get the complete corrected email address
    pub fn full(&self) -> String
}

Config

pub struct Config {
    pub domains: Vec<String>,              // Popular domains list
    pub second_level_domains: Vec<String>, // Second-level domains
    pub top_level_domains: Vec<String>,    // Top-level domains
    pub threshold: f64,                    // String distance threshold (0.0-1.0)
}

License

Apache-2.0 License

Contributing

Bug reports, feature requests, and pull requests are welcome!

References

Commit count: 0

cargo fmt