Skip to content

Commit

Permalink
Derive Eq, PartialEq, and Hash for EmailAddress
Browse files Browse the repository at this point in the history
What?
=====

This enables EmailAddress equality to be checked as the current
structure only wraps two String values.

This also derives Hash, allowing it to be used as a key in dictionary
structures (e.g. BTreeMap).
  • Loading branch information
joshuaclayton committed Apr 15, 2021
1 parent f001cba commit c643037
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion rust-lib/src/email_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate pest;
extern crate pest_derive;
use pest::{iterators::Pairs, Parser};
use std::fmt;
use std::hash::Hash;
use wasm_bindgen::prelude::*;

/// Options for parsing.
Expand Down Expand Up @@ -49,7 +50,7 @@ struct RFC5322;
/// assert_eq!(format!("{}", email), "[email protected]");
/// ```
#[wasm_bindgen]
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct EmailAddress {
local_part: String,
domain: String,
Expand Down Expand Up @@ -328,6 +329,18 @@ mod tests {
assert_eq!(format!("{}", address), "[email protected]");
}

#[test]
fn email_address_supports_equality_checking() {
let foo_at_bar_dot_com = EmailAddress::new("foo", "bar.com", None).unwrap();
let foo_at_bar_dot_com_2 = EmailAddress::new("foo", "bar.com", None).unwrap();
let foob_at_ar_dot_com = EmailAddress::new("foob", "ar.com", None).unwrap();

assert_eq!(foo_at_bar_dot_com, foo_at_bar_dot_com);
assert_eq!(foo_at_bar_dot_com, foo_at_bar_dot_com_2);
assert_ne!(foo_at_bar_dot_com, foob_at_ar_dot_com);
assert_ne!(foo_at_bar_dot_com_2, foob_at_ar_dot_com);
}

#[test]
fn domain_rule_does_not_parse_dash_google_dot_com() {
let address = RFC5322::parse(Rule::domain_complete, "-google.com");
Expand Down

0 comments on commit c643037

Please sign in to comment.