2 stable releases
1.0.1 | Feb 26, 2023 |
---|
#341 in Email
8KB
121 lines
Email type
Email type for approach suggested by Alexis King - "Parse, don't validate".
How to use:
Add dependency:
email-type-rs = { git = "https://github.com/lebe-dev/email-type-rs", version = "1.0.0", features = ["utils"] }
Use:
let email = Email::from_str("lexi.lambda@gmail.com")?;
let email = "lexi.lambda@gmail.com".parse()?;
// fn some_func(value: &str)
some_func(email.as_str());
some_func(&email);
Useful for REST API Endpoints, i.e. /api/register
accepts:
#[derive(Deserialize)]
struct UserRegistrationRequest {
pub email: Email,
...
}
Incoming JSON with invalid value in email
field will raise deserialization error (Serde).
Util functions
Add to Cargo.toml
:
[dev-dependencies]
email-type-rs = { git = "https://github.com/lebe-dev/email-type-rs", version = "1.0.0", features = ["utils"] }
Functions:
get_random_email()
- return randomEmail
. Useful for tests.
Thanks
- Alexis King, article - Parse, don't validate
- Justin Wernick, article - The Newtype Pattern In Rust
Dependencies
~5.5MB
~115K SLoC