Crates.io | serde-email |
lib.rs | serde-email |
version | 3.1.0 |
source | src |
created_at | 2022-11-20 19:37:17.216416 |
updated_at | 2024-09-09 23:43:30.706361 |
description | A validating email type that can be serialised using Serde |
homepage | |
repository | https://github.com/JosephLenton/serde-email |
max_upload_size | |
id | 719348 |
size | 26,656 |
This crate is for creating Email
objects.
let emails : Vec<Email> = vec![]
.Email
type guarantees to be validated. Once it is created, you can be confident it's safe to use as an email.Email
type can also be used as strings. This allows interoptability with lots of connector functions which will take a String.(Note this library will not check if the Email address exists. It only validates that it looks correct.)
serde
Default - Enables serde serialisation and deserialisation.sea-orm
- Enables Sea Orm use with DB entities.use ::serde_email::Email;
let email = Email::from_str("test@example.com").expect("A valid email address");
use ::serde_email::is_valid_email;
if is_valid_email(&"test@example.com") {
// do something
}
use ::serde_email::Email;
use ::serde_json;
struct Person {
name: String,
email: Email,
}
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r#"
{
"name": "John Doe",
"email": "john@example.com"
}"#;
// Parse the string of data into serde_json::Value.
let person: Person = serde_json::from_str(data).unwrap();
// Access parts of the data by indexing with square brackets.
println!("Hello {} I'll email you are {}", person.name, person.email);
You can use the Email
type with Sea Orm, including using it to save data to the DB.
Underneath it will serialise to a Text
type within the DB.
use ::sea_orm::entity::prelude::*;
use ::serde::Deserialize;
use ::serde::Serialize;
use ::serde_email::Email;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "user")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub email: Email, // use as an email field
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
The validation is all done by the email_address crate.