From e2ac5dadfb21ea089322beec9f2a39f9e8a2e3ab Mon Sep 17 00:00:00 2001 From: Arnaud de Bossoreille Date: Thu, 12 Sep 2024 17:26:43 +0200 Subject: [PATCH] Fix parsing Mailbox with spaces (#986) --- src/message/mailbox/parsers/rfc2822.rs | 4 +++- src/message/mailbox/types.rs | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/message/mailbox/parsers/rfc2822.rs b/src/message/mailbox/parsers/rfc2822.rs index 1f756d7..15eee5d 100644 --- a/src/message/mailbox/parsers/rfc2822.rs +++ b/src/message/mailbox/parsers/rfc2822.rs @@ -170,7 +170,9 @@ fn phrase() -> impl Parser, Error = Cheap> { // mailbox = name-addr / addr-spec pub(crate) fn mailbox() -> impl Parser, (String, String)), Error = Cheap> { - choice((name_addr(), addr_spec().map(|addr| (None, addr)))).then_ignore(end()) + choice((name_addr(), addr_spec().map(|addr| (None, addr)))) + .padded() + .then_ignore(end()) } // name-addr = [display-name] angle-addr diff --git a/src/message/mailbox/types.rs b/src/message/mailbox/types.rs index b6d7f6f..8ea8e17 100644 --- a/src/message/mailbox/types.rs +++ b/src/message/mailbox/types.rs @@ -556,6 +556,14 @@ mod test { ); } + #[test] + fn parse_address_only_trim() { + assert_eq!( + " kayo@example.com ".parse(), + Ok(Mailbox::new(None, "kayo@example.com".parse().unwrap())) + ); + } + #[test] fn parse_address_with_name() { assert_eq!( @@ -567,6 +575,17 @@ mod test { ); } + #[test] + fn parse_address_with_name_trim() { + assert_eq!( + " K. ".parse(), + Ok(Mailbox::new( + Some("K.".into()), + "kayo@example.com".parse().unwrap() + )) + ); + } + #[test] fn parse_address_with_empty_name() { assert_eq!( @@ -578,7 +597,7 @@ mod test { #[test] fn parse_address_with_empty_name_trim() { assert_eq!( - " ".parse(), + " ".parse(), Ok(Mailbox::new(None, "kayo@example.com".parse().unwrap())) ); }