use abnf_core::streaming::SP;
use imap_types::{address::Address, core::NString, envelope::Envelope};
use nom::{
branch::alt,
bytes::streaming::tag,
combinator::map,
multi::many1,
sequence::{delimited, tuple},
IResult,
};
use crate::rfc3501::{
address::address,
core::{nil, nstring},
};
pub fn envelope(input: &[u8]) -> IResult<&[u8], Envelope> {
let mut parser = delimited(
tag(b"("),
tuple((
env_date,
SP,
env_subject,
SP,
env_from,
SP,
env_sender,
SP,
env_reply_to,
SP,
env_to,
SP,
env_cc,
SP,
env_bcc,
SP,
env_in_reply_to,
SP,
env_message_id,
)),
tag(b")"),
);
let (
remaining,
(
date,
_,
subject,
_,
from,
_,
sender,
_,
reply_to,
_,
to,
_,
cc,
_,
bcc,
_,
in_reply_to,
_,
message_id,
),
) = parser(input)?;
Ok((
remaining,
Envelope {
date,
subject,
from,
sender,
reply_to,
to,
cc,
bcc,
in_reply_to,
message_id,
},
))
}
#[inline]
pub fn env_date(input: &[u8]) -> IResult<&[u8], NString> {
nstring(input)
}
#[inline]
pub fn env_subject(input: &[u8]) -> IResult<&[u8], NString> {
nstring(input)
}
pub fn env_from(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
alt((
delimited(tag(b"("), many1(address), tag(b")")),
map(nil, |_| Vec::new()),
))(input)
}
pub fn env_sender(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
alt((
delimited(tag(b"("), many1(address), tag(b")")),
map(nil, |_| Vec::new()),
))(input)
}
pub fn env_reply_to(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
alt((
delimited(tag(b"("), many1(address), tag(b")")),
map(nil, |_| Vec::new()),
))(input)
}
pub fn env_to(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
alt((
delimited(tag(b"("), many1(address), tag(b")")),
map(nil, |_| Vec::new()),
))(input)
}
pub fn env_cc(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
alt((
delimited(tag(b"("), many1(address), tag(b")")),
map(nil, |_| Vec::new()),
))(input)
}
pub fn env_bcc(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
alt((
delimited(tag(b"("), many1(address), tag(b")")),
map(nil, |_| Vec::new()),
))(input)
}
#[inline]
pub fn env_in_reply_to(input: &[u8]) -> IResult<&[u8], NString> {
nstring(input)
}
#[inline]
pub fn env_message_id(input: &[u8]) -> IResult<&[u8], NString> {
nstring(input)
}