mailidator 0.1.0

A lightweight Rust library for checking email address misspellings
Documentation
  • Coverage
  • 100%
    21 out of 21 items documented2 out of 11 items with examples
  • Size
  • Source code size: 57.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.85 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 20s Average build duration of successful builds.
  • all releases: 20s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • JohnTitor

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