pub struct EmailAddress { /* private fields */ }
Expand description

Email address struct.

Examples

use email_address_parser::EmailAddress;

assert!(EmailAddress::parse("foo@-bar.com", None).is_none());
let email = EmailAddress::parse("foo@bar.com", None);
assert!(email.is_some());
let email = email.unwrap();
assert_eq!(email.get_local_part(), "foo");
assert_eq!(email.get_domain(), "bar.com");
assert_eq!(format!("{}", email), "foo@bar.com");

Implementations§

source§

impl EmailAddress

source

pub fn parse( input: &str, options: Option<ParsingOptions> ) -> Option<EmailAddress>

Parses a given string as an email address.

Accessible from WASM.

Returns Some(EmailAddress) if the parsing is successful, else None.

Examples
use email_address_parser::*;

// strict parsing
let email = EmailAddress::parse("foo@bar.com", None);
assert!(email.is_some());
let email = email.unwrap();
assert_eq!(email.get_local_part(), "foo");
assert_eq!(email.get_domain(), "bar.com");

// non-strict parsing
let email = EmailAddress::parse("\u{0d}\u{0a} \u{0d}\u{0a} test@iana.org", Some(ParsingOptions::new(true)));
assert!(email.is_some());

// parsing invalid address
let email = EmailAddress::parse("test@-iana.org", Some(ParsingOptions::new(true)));
assert!(email.is_none());
let email = EmailAddress::parse("test@-iana.org", Some(ParsingOptions::new(true)));
assert!(email.is_none());
let email = EmailAddress::parse("test", Some(ParsingOptions::new(true)));
assert!(email.is_none());
let email = EmailAddress::parse("test", Some(ParsingOptions::new(true)));
assert!(email.is_none());
source

pub fn is_valid(input: &str, options: Option<ParsingOptions>) -> bool

Validates if the given input string is an email address or not.

Returns true if the input is valid, false otherwise. Unlike the parse method, it does not instantiate an EmailAddress.

Examples
use email_address_parser::*;

// strict validation
assert!(EmailAddress::is_valid("foo@bar.com", None));

// non-strict validation
assert!(EmailAddress::is_valid("\u{0d}\u{0a} \u{0d}\u{0a} test@iana.org", Some(ParsingOptions::new(true))));

// invalid address
assert!(!EmailAddress::is_valid("test@-iana.org", Some(ParsingOptions::new(true))));
assert!(!EmailAddress::is_valid("test@-iana.org", Some(ParsingOptions::new(true))));
assert!(!EmailAddress::is_valid("test", Some(ParsingOptions::new(true))));
assert!(!EmailAddress::is_valid("test", Some(ParsingOptions::new(true))));
source§

impl EmailAddress

source

pub fn new( local_part: &str, domain: &str, options: Option<ParsingOptions> ) -> Result<EmailAddress, String>

Instantiates a new Some(EmailAddress) for a valid local part and domain. Returns Err otherwise.

Examples
use email_address_parser::EmailAddress;

let email = EmailAddress::new("foo", "bar.com", None).unwrap();

assert_eq!(EmailAddress::new("foo", "-bar.com", None).is_err(), true);
source

pub fn get_local_part(&self) -> &str

Returns the local part of the email address.

Not accessible from WASM.

Examples
use email_address_parser::EmailAddress;

let email = EmailAddress::new("foo", "bar.com", None).unwrap();
assert_eq!(email.get_local_part(), "foo");

let email = EmailAddress::parse("foo@bar.com", None).unwrap();
assert_eq!(email.get_local_part(), "foo");
source

pub fn get_domain(&self) -> &str

Returns the domain of the email address.

Not accessible from WASM.

Examples
use email_address_parser::EmailAddress;

let email = EmailAddress::new("foo", "bar.com", None).unwrap();
assert_eq!(email.get_domain(), "bar.com");

let email = EmailAddress::parse("foo@bar.com", None).unwrap();
assert_eq!(email.get_domain(), "bar.com");

Trait Implementations§

source§

impl Clone for EmailAddress

source§

fn clone(&self) -> EmailAddress

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for EmailAddress

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for EmailAddress

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl FromStr for EmailAddress

Allows conversion from string slices (&str) to EmailAddress using the FromStr trait. This wraps around EmailAddress::parse using the default ParsingOptions.

Examples

use email_address_parser::EmailAddress;
use std::str::FromStr;

const input_address : &str = "string@slice.com";

let myaddr : EmailAddress = input_address.parse().expect("could not parse str into EmailAddress");
let myotheraddr = EmailAddress::from_str(input_address).expect("could create EmailAddress from str");

assert_eq!(myaddr, myotheraddr);
§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for EmailAddress

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<EmailAddress> for EmailAddress

source§

fn eq(&self, other: &EmailAddress) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for EmailAddress

source§

impl StructuralEq for EmailAddress

source§

impl StructuralPartialEq for EmailAddress

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.