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

View file

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