Skip to content

Commit

Permalink
add comment with working example
Browse files Browse the repository at this point in the history
  • Loading branch information
techhazard committed May 31, 2023
1 parent 0b85d56 commit 322225e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion rust-lib/src/email_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,27 @@ impl Default for ParsingOptions {
}
}

/// 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 = "[email protected]";
///
/// 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);
/// ```
impl FromStr for EmailAddress {
type Err = fmt::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(email) = EmailAddress::parse(s, None) {
let opts = ParsingOptions::default();
if let Some(email) = EmailAddress::parse(s, Some(opts)) {
Ok(email)
} else {
Err(fmt::Error)
Expand Down

0 comments on commit 322225e

Please sign in to comment.