fix address

This commit is contained in:
Quentin 2023-07-19 15:56:03 +02:00
parent fd0794fe6e
commit 6e7cb5500c
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 44 additions and 44 deletions

View file

@ -8,36 +8,34 @@ use nom::{
}; };
use crate::error::IMFError; use crate::error::IMFError;
use crate::fragments::lazy; use crate::rfc5322::mailbox::{mailbox, MailboxRef};
use crate::fragments::mailbox::mailbox; use crate::text::misc_token::{phrase, Phrase};
use crate::fragments::misc_token::phrase; use crate::text::whitespace::cfws;
//use crate::fragments::model::{AddressList, AddressRef, GroupRef, MailboxList, MailboxRef};
use crate::fragments::whitespace::cfws;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct GroupRef { pub struct GroupRef<'a> {
pub name: String, pub name: Phrase<'a>,
pub participants: Vec<MailboxRef>, pub participants: Vec<MailboxRef<'a>>,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum AddressRef { pub enum AddressRef<'a> {
Single(MailboxRef), Single(MailboxRef<'a>),
Many(GroupRef), Many(GroupRef<'a>),
} }
impl From<MailboxRef> for AddressRef { impl<'a> From<MailboxRef<'a>> for AddressRef<'a> {
fn from(mx: MailboxRef) -> Self { fn from(mx: MailboxRef<'a>) -> Self {
AddressRef::Single(mx) AddressRef::Single(mx)
} }
} }
impl From<GroupRef> for AddressRef { impl<'a> From<GroupRef<'a>> for AddressRef<'a> {
fn from(grp: GroupRef) -> Self { fn from(grp: GroupRef<'a>) -> Self {
AddressRef::Many(grp) AddressRef::Many(grp)
} }
} }
pub type AddressList = Vec<AddressRef>; pub type AddressList<'a> = Vec<AddressRef<'a>>;
impl<'a> TryFrom<&'a lazy::Mailbox<'a>> for MailboxRef { /*impl<'a> TryFrom<&'a lazy::Mailbox<'a>> for MailboxRef {
type Error = IMFError<'a>; type Error = IMFError<'a>;
fn try_from(mx: &'a lazy::Mailbox<'a>) -> Result<Self, Self::Error> { fn try_from(mx: &'a lazy::Mailbox<'a>) -> Result<Self, Self::Error> {
@ -75,14 +73,14 @@ impl<'a> TryFrom<&'a lazy::NullableAddressList<'a>> for AddressList {
.map(|(_, a)| a.unwrap_or(vec![])) .map(|(_, a)| a.unwrap_or(vec![]))
.map_err(|e| IMFError::NullableAddressList(e)) .map_err(|e| IMFError::NullableAddressList(e))
} }
} }*/
/// Address (section 3.4 of RFC5322) /// Address (section 3.4 of RFC5322)
/// ///
/// ```abnf /// ```abnf
/// address = mailbox / group /// address = mailbox / group
/// ``` /// ```
pub fn address(input: &str) -> IResult<&str, AddressRef> { pub fn address(input: &[u8]) -> IResult<&[u8], AddressRef> {
alt((into(mailbox), into(group)))(input) alt((into(mailbox), into(group)))(input)
} }
@ -92,7 +90,7 @@ pub fn address(input: &str) -> IResult<&str, AddressRef> {
/// group = display-name ":" [group-list] ";" [CFWS] /// group = display-name ":" [group-list] ";" [CFWS]
/// display-name = phrase /// display-name = phrase
/// ``` /// ```
pub fn group(input: &str) -> IResult<&str, GroupRef> { pub fn group(input: &[u8]) -> IResult<&[u8], GroupRef> {
let (input, (grp_name, _, grp_list, _, _)) = let (input, (grp_name, _, grp_list, _, _)) =
tuple((phrase, tag(":"), opt(group_list), tag(";"), opt(cfws)))(input)?; tuple((phrase, tag(":"), opt(group_list), tag(";"), opt(cfws)))(input)?;
@ -110,11 +108,11 @@ pub fn group(input: &str) -> IResult<&str, GroupRef> {
/// ```abnf /// ```abnf
/// group-list = mailbox-list / CFWS / obs-group-list /// group-list = mailbox-list / CFWS / obs-group-list
/// ``` /// ```
pub fn group_list(input: &str) -> IResult<&str, Vec<MailboxRef>> { pub fn group_list(input: &[u8]) -> IResult<&[u8], Vec<MailboxRef>> {
alt((mailbox_list, mailbox_cfws))(input) alt((mailbox_list, mailbox_cfws))(input)
} }
fn mailbox_cfws(input: &str) -> IResult<&str, Vec<MailboxRef>> { fn mailbox_cfws(input: &[u8]) -> IResult<&[u8], Vec<MailboxRef>> {
let (input, _) = cfws(input)?; let (input, _) = cfws(input)?;
Ok((input, vec![])) Ok((input, vec![]))
} }
@ -124,7 +122,7 @@ fn mailbox_cfws(input: &str) -> IResult<&str, Vec<MailboxRef>> {
/// ```abnf /// ```abnf
/// mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list /// mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list
/// ``` /// ```
pub fn mailbox_list(input: &str) -> IResult<&str, Vec<MailboxRef>> { pub fn mailbox_list(input: &[u8]) -> IResult<&[u8], Vec<MailboxRef>> {
separated_list1(tag(","), mailbox)(input) separated_list1(tag(","), mailbox)(input)
} }
@ -133,11 +131,11 @@ pub fn mailbox_list(input: &str) -> IResult<&str, Vec<MailboxRef>> {
/// ```abnf /// ```abnf
/// address-list = (address *("," address)) / obs-addr-list /// address-list = (address *("," address)) / obs-addr-list
/// ``` /// ```
pub fn address_list(input: &str) -> IResult<&str, Vec<AddressRef>> { pub fn address_list(input: &[u8]) -> IResult<&[u8], Vec<AddressRef>> {
separated_list1(tag(","), address)(input) separated_list1(tag(","), address)(input)
} }
pub fn address_list_cfws(input: &str) -> IResult<&str, Vec<AddressRef>> { pub fn address_list_cfws(input: &[u8]) -> IResult<&[u8], Vec<AddressRef>> {
let (input, _) = cfws(input)?; let (input, _) = cfws(input)?;
Ok((input, vec![])) Ok((input, vec![]))
} }
@ -145,19 +143,20 @@ pub fn address_list_cfws(input: &str) -> IResult<&str, Vec<AddressRef>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::fragments::model::AddrSpec; use crate::text::misc_token::{Phrase, Word};
use crate::rfc5322::mailbox::{AddrSpec, Domain, LocalPart, LocalPartToken};
#[test] #[test]
fn test_mailbox_list() { fn test_mailbox_list() {
match mailbox_list(r#"Pete(A nice \) chap) <pete(his account)@silly.test(his host)>"#) { match mailbox_list(r#"Pete(A nice \) chap) <pete(his account)@silly.test(his host)>"#.as_bytes()) {
Ok(("", _)) => (), Ok((rest, _)) => assert_eq!(&b""[..], rest),
_ => panic!(), _ => panic!(),
}; };
match mailbox_list( match mailbox_list(
r#"Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>, <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>"#, r#"Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>, <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>"#.as_bytes(),
) { ) {
Ok(("", _)) => (), Ok((rest, _)) => assert_eq!(&b""[..], rest),
_ => panic!(), _ => panic!(),
}; };
} }
@ -166,42 +165,42 @@ mod tests {
fn test_address_list() { fn test_address_list() {
assert_eq!( assert_eq!(
address_list( address_list(
r#"A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;, Mary Smith <mary@x.test>"# r#"A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;, Mary Smith <mary@x.test>"#.as_bytes()
), ),
Ok(( Ok((
"", &b""[..],
vec![ vec![
AddressRef::Many(GroupRef { AddressRef::Many(GroupRef {
name: "A Group".to_string(), name: Phrase(vec![Word::Atom(&b"A"[..]), Word::Atom(&b"Group"[..])]),
participants: vec![ participants: vec![
MailboxRef { MailboxRef {
name: Some("Ed Jones".into()), name: Some(Phrase(vec![Word::Atom(&b"Ed"[..]), Word::Atom(&b"Jones"[..])])),
addrspec: AddrSpec { addrspec: AddrSpec {
local_part: "c".into(), local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"c"[..]))]),
domain: "a.test".into() domain: Domain::Atoms(vec![&b"a"[..], &b"test"[..]]),
}, },
}, },
MailboxRef { MailboxRef {
name: None, name: None,
addrspec: AddrSpec { addrspec: AddrSpec {
local_part: "joe".into(), local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"joe"[..]))]),
domain: "where.test".into() domain: Domain::Atoms(vec![&b"where"[..], &b"test"[..]])
}, },
}, },
MailboxRef { MailboxRef {
name: Some("John".into()), name: Some(Phrase(vec![Word::Atom(&b"John"[..])])),
addrspec: AddrSpec { addrspec: AddrSpec {
local_part: "jdoe".into(), local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"jdoe"[..]))]),
domain: "one.test".into() domain: Domain::Atoms(vec![&b"one"[..], &b"test"[..]])
}, },
}, },
], ],
}), }),
AddressRef::Single(MailboxRef { AddressRef::Single(MailboxRef {
name: Some("Mary Smith".into()), name: Some(Phrase(vec![Word::Atom(&b"Mary"[..]), Word::Atom(&b"Smith"[..])])),
addrspec: AddrSpec { addrspec: AddrSpec {
local_part: "mary".into(), local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"mary"[..]))]),
domain: "x.test".into() domain: Domain::Atoms(vec![&b"x"[..], &b"test"[..]])
}, },
}), }),
] ]

View file

@ -1 +1,2 @@
pub mod mailbox; pub mod mailbox;
pub mod address;