email-address-parser 2.0.2

An RFC 5322, and RFC 6532 compliant email address parser.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#[cfg(target_arch = "wasm32")]
extern crate console_error_panic_hook;
extern crate pest;
extern crate pest_derive;
use pest::{iterators::Pairs, Parser};
use std::fmt;
use std::hash::Hash;
use std::str::FromStr;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

/// Options for parsing.
///
/// There is only one available option so far `is_lax` which can be set to
/// `true` or `false` to  enable/disable obsolete parts parsing.
/// The default is `false`.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Debug,Clone)]
pub struct ParsingOptions {
    pub is_lax: bool,
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl ParsingOptions {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
    pub fn new(is_lax: bool) -> ParsingOptions {
        ParsingOptions { is_lax }
    }
}

impl Default for ParsingOptions {
    fn default() -> Self {
        ParsingOptions::new(false)
    }
}

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

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

#[derive(Parser)]
#[grammar = "rfc5322.pest"]
struct RFC5322;

/// 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");
/// ```
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct EmailAddress {
    local_part: String,
    domain: String,
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl EmailAddress {
    #![warn(missing_docs)]
    #![warn(rustdoc::missing_doc_code_examples)]

    /// This is a WASM wrapper over EmailAddress::new that panics.
    /// If you are using this lib from Rust then consider using EmailAddress::new.
    ///
    /// # Examples
    /// ```
    /// use email_address_parser::EmailAddress;
    ///
    /// let email = EmailAddress::_new("foo", "bar.com", None);
    /// ```
    ///
    /// # Panics
    ///
    /// This method panics if the local part or domain is invalid.
    ///
    /// ```rust,should_panic,ignore-wasm32
    /// use email_address_parser::EmailAddress;
    ///
    /// EmailAddress::_new("foo", "-bar.com", None);
    /// ```
    #[doc(hidden)]
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
    pub fn _new(local_part: &str, domain: &str, options: Option<ParsingOptions>) -> EmailAddress {
        #[cfg(target_arch = "wasm32")]
        console_error_panic_hook::set_once();
        match EmailAddress::new(local_part, domain, options) {
            Ok(instance) => instance,
            Err(message) => panic!("{}", message),
        }
    }

    /// 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());
    /// ```
    pub fn parse(input: &str, options: Option<ParsingOptions>) -> Option<EmailAddress> {
        let instantiate = |mut parsed: pest::iterators::Pairs<Rule>| {
            let mut parsed = parsed
                .next()
                .unwrap()
                .into_inner()
                .next()
                .unwrap()
                .into_inner();
            Some(EmailAddress {
                local_part: String::from(parsed.next().unwrap().as_str()),
                domain: String::from(parsed.next().unwrap().as_str()),
            })
        };
        match EmailAddress::parse_core(input, options) {
            Some(parsed) => instantiate(parsed),
            None => None,
        }
    }
    /// 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))));
    /// ```
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = "isValid"))]
    pub fn is_valid(input: &str, options: Option<ParsingOptions>) -> bool {
        EmailAddress::parse_core(input, options).is_some()
    }

    /// Returns the local part of the email address.
    ///
    /// Note that if you are using this library from rust, then consider using the `get_local_part` method instead.
    /// This returns a cloned copy of the local part string, instead of a borrowed `&str`, and exists purely for WASM interoperability.
    ///
    /// # Examples
    /// ```
    /// use email_address_parser::EmailAddress;
    ///
    /// let email = EmailAddress::new("foo", "bar.com", None).unwrap();
    /// assert_eq!(email.localPart(), "foo");
    ///
    /// let email = EmailAddress::parse("foo@bar.com", None).unwrap();
    /// assert_eq!(email.localPart(), "foo");
    /// ```
    #[doc(hidden)]
    #[allow(non_snake_case)]
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn localPart(&self) -> String {
        self.local_part.clone()
    }

    /// Returns the domain of the email address.
    ///
    /// Note that if you are using this library from rust, then consider using the `get_domain` method instead.
    /// This returns a cloned copy of the domain string, instead of a borrowed `&str`, and exists purely for WASM interoperability.
    ///
    /// # Examples
    /// ```
    /// use email_address_parser::EmailAddress;
    ///
    /// let email = EmailAddress::new("foo", "bar.com", None).unwrap();
    /// assert_eq!(email.domain(), "bar.com");
    ///
    /// let email = EmailAddress::parse("foo@bar.com", None).unwrap();
    /// assert_eq!(email.domain(), "bar.com");
    /// ```
    #[doc(hidden)]
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn domain(&self) -> String {
        self.domain.clone()
    }

    /// Returns the formatted EmailAddress.
    /// This exists purely for WASM interoperability.
    #[doc(hidden)]
    #[allow(non_snake_case)]
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip_typescript))]
    pub fn toString(&self) -> String {
        format!("{}@{}", self.local_part, self.domain)
    }

    fn parse_core<'i>(input: &'i str, options: Option<ParsingOptions>) -> Option<Pairs<'i, Rule>> {
        let options = options.unwrap_or_default();
        let is_strict = !options.is_lax;
        match RFC5322::parse(Rule::address_single, input) {
            Ok(parsed) => Some(parsed),
            Err(_) => {
                if is_strict {
                    None
                } else {
                    match RFC5322::parse(Rule::address_single_obs, input) {
                        Ok(parsed) => Some(parsed),
                        Err(_) => None,
                    }
                }
            }
        }
    }
}

impl EmailAddress {
    #![warn(missing_docs)]
    #![warn(rustdoc::missing_doc_code_examples)]

    /// 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);
    /// ```
    pub fn new(
        local_part: &str,
        domain: &str,
        options: Option<ParsingOptions>,
    ) -> Result<EmailAddress, String> {
        match EmailAddress::parse(&format!("{}@{}", local_part, domain), options.clone()) {
            Some(email_address) => Ok(email_address),
            None => {
                if !options.unwrap_or_default().is_lax {
                    return Err(format!("Invalid local part '{}'.", local_part));
                }
                Ok(EmailAddress {
                    local_part: String::from(local_part),
                    domain: String::from(domain),
                })
            }
        }
    }

    /// 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");
    /// ```
    pub fn get_local_part(&self) -> &str {
        self.local_part.as_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");
    /// ```
    pub fn get_domain(&self) -> &str {
        self.domain.as_str()
    }
}

impl fmt::Display for EmailAddress {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        formatter.write_fmt(format_args!("{}@{}", self.local_part, self.domain))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn email_address_instantiation_works() {
        let address = EmailAddress::new("foo", "bar.com", None).unwrap();
        assert_eq!(address.get_local_part(), "foo");
        assert_eq!(address.get_domain(), "bar.com");
        assert_eq!(format!("{}", address), "foo@bar.com");
    }

    #[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");
        println!("{:#?}", address);
        assert_eq!(address.is_err(), true);
    }

    #[test]
    fn domain_rule_does_not_parse_dash_google_dot_com_obs() {
        let address = RFC5322::parse(Rule::domain_obs, "-google.com");
        println!("{:#?}", address);
        assert_eq!(address.is_err(), true);
    }

    #[test]
    fn domain_rule_does_not_parse_dash_google_dash_dot_com() {
        let address = RFC5322::parse(Rule::domain_complete, "-google-.com");
        println!("{:#?}", address);
        assert_eq!(address.is_err(), true);
    }

    #[test]
    fn domain_rule_parses_google_dash_dot_com() {
        let address = RFC5322::parse(Rule::domain_complete, "google-.com");
        println!("{:#?}", address);
        assert_eq!(address.is_err(), true);
    }

    #[test]
    fn domain_complete_punycode_domain() {
        let actual = RFC5322::parse(Rule::domain_complete, "xn--masekowski-d0b.pl");
        println!("{:#?}", actual);
        assert_eq!(actual.is_err(), false);
    }

    #[test]
    fn can_parse_deprecated_local_part() {
        let actual = RFC5322::parse(Rule::local_part_obs, "\"test\".\"test\"");
        println!("{:#?}", actual);
        assert_eq!(actual.is_err(), false);
    }

    #[test]
    fn can_parse_email_with_deprecated_local_part() {
        let actual = RFC5322::parse(Rule::address_single_obs, "\"test\".\"test\"@iana.org");
        println!("{:#?}", actual);
        assert_eq!(actual.is_err(), false);
    }

    #[test]
    fn can_parse_domain_with_space() {
        println!("{:#?}", RFC5322::parse(Rule::domain_obs, " iana .com"));
        let actual = EmailAddress::parse("test@ iana .com", Some(ParsingOptions::new(true)));
        println!("{:#?}", actual);
        assert_eq!(actual.is_some(), true, "test@ iana .com");
    }

    #[test]
    fn can_parse_email_with_cfws_near_at() {
        let email = " test @iana.org";
        let actual = EmailAddress::parse(&email, None);
        println!("{:#?}", actual);
        assert_eq!(format!("{}", actual.unwrap()), email);
    }

    #[test]
    fn can_parse_email_with_crlf() {
        let email = "\u{0d}\u{0a} test@iana.org";
        let actual = EmailAddress::parse(&email, Some(ParsingOptions::new(true)));
        println!("{:#?}", actual);
        assert_eq!(format!("{}", actual.unwrap()), email);
    }

    #[test]
    fn can_parse_local_part_with_space() {
        let actual = RFC5322::parse(Rule::address_single_obs, "test . test@iana.org");
        println!("{:#?}", actual);
        assert_eq!(actual.is_err(), false);
    }

    #[test]
    fn can_parse_domain_with_bel() {
        let actual = RFC5322::parse(Rule::domain_literal, "[RFC-5322-\u{07}-domain-literal]");
        println!("{:#?}", actual);
        assert_eq!(actual.is_err(), false);
    }

    #[test]
    fn can_parse_local_part_with_space_and_quote() {
        let actual = RFC5322::parse(Rule::local_part_complete, "\"test test\"");
        println!("{:#?}", actual);
        assert_eq!(actual.is_err(), false);
    }

    #[test]
    fn can_parse_idn() {
        let actual = RFC5322::parse(Rule::domain_complete, "bücher.com");
        println!("{:#?}", actual);
        assert_eq!(actual.is_err(), false);
    }

    #[test]
    fn parsing_empty_local_part_and_domain() {
        let actual = EmailAddress::parse("@", Some(ParsingOptions::new(true)));
        assert_eq!(actual.is_none(), true, "expected none");
        let actual = EmailAddress::new("", "", Some(ParsingOptions::new(false)));
        assert_eq!(actual.is_err(), true, "expected error");
        let actual = EmailAddress::new("", "", Some(ParsingOptions::new(true)));
        assert_eq!(actual.is_ok(), true, "expected ok");
        let actual = actual.unwrap();
        assert_eq!(actual.domain, "");
        assert_eq!(actual.local_part, "");
    }
}