Crates.io | mailidator |
lib.rs | mailidator |
version | 0.1.0 |
created_at | 2025-08-16 03:50:30.594887+00 |
updated_at | 2025-08-16 03:50:30.594887+00 |
description | A lightweight Rust library for checking email address misspellings |
homepage | |
repository | https://github.com/yourusername/mailidator |
max_upload_size | |
id | 1797897 |
size | 57,502 |
A lightweight Rust library for checking email address misspellings and suggesting corrections.
Add this to your Cargo.toml
:
[dependencies]
mailidator = "0.1.0"
# If you need Serde support
mailidator = { version = "0.1.0", features = ["serde"] }
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());
}
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
}
}
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);
}
}
}
}
This library supports the following popular domains:
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)
}
Apache-2.0 License
Bug reports, feature requests, and pull requests are welcome!