eml-codec/src/rfc5322/identification.rs

88 lines
2.1 KiB
Rust
Raw Normal View History

2023-06-13 15:12:16 +02:00
use nom::{
branch::alt,
2023-06-22 15:08:50 +02:00
bytes::complete::{tag, take_while},
2023-06-13 15:12:16 +02:00
combinator::opt,
sequence::{delimited, pair, tuple},
2023-06-22 15:08:50 +02:00
IResult,
2023-06-13 15:12:16 +02:00
};
2023-07-19 18:30:43 +02:00
use crate::rfc5322::mailbox::is_dtext;
use crate::text::whitespace::cfws;
use crate::text::words::dot_atom_text;
2023-06-20 15:56:06 +02:00
2023-07-18 23:25:10 +02:00
#[derive(Debug, PartialEq)]
pub struct MessageId<'a> {
2023-07-19 18:30:43 +02:00
pub left: &'a [u8],
pub right: &'a [u8],
2023-07-18 23:25:10 +02:00
}
pub type MessageIdList<'a> = Vec<MessageId<'a>>;
2023-07-19 18:30:43 +02:00
/*
2023-06-22 10:48:07 +02:00
impl<'a> TryFrom<&'a lazy::Identifier<'a>> for MessageId<'a> {
2023-06-20 15:56:06 +02:00
type Error = IMFError<'a>;
2023-06-22 10:48:07 +02:00
fn try_from(id: &'a lazy::Identifier<'a>) -> Result<Self, Self::Error> {
2023-06-20 15:56:06 +02:00
msg_id(id.0)
.map(|(_, i)| i)
.map_err(|e| IMFError::MessageID(e))
}
}
2023-06-22 10:48:07 +02:00
impl<'a> TryFrom<&'a lazy::IdentifierList<'a>> for MessageIdList<'a> {
2023-06-20 15:56:06 +02:00
type Error = IMFError<'a>;
2023-06-22 10:48:07 +02:00
fn try_from(id: &'a lazy::IdentifierList<'a>) -> Result<Self, Self::Error> {
2023-06-20 15:56:06 +02:00
many1(msg_id)(id.0)
.map(|(_, i)| i)
.map_err(|e| IMFError::MessageIDList(e))
}
2023-07-19 18:30:43 +02:00
}*/
2023-06-13 15:12:16 +02:00
/// Message identifier
///
/// ```abnf
/// msg-id = [CFWS] "<" id-left "@" id-right ">" [CFWS]
/// ```
2023-07-19 18:30:43 +02:00
pub fn msg_id(input: &[u8]) -> IResult<&[u8], MessageId> {
2023-06-13 15:12:16 +02:00
let (input, (left, _, right)) = delimited(
pair(opt(cfws), tag("<")),
tuple((id_left, tag("@"), id_right)),
pair(tag(">"), opt(cfws)),
)(input)?;
2023-06-22 15:08:50 +02:00
Ok((input, MessageId { left, right }))
2023-06-13 15:12:16 +02:00
}
2023-07-19 18:30:43 +02:00
// @FIXME Missing obsolete
fn id_left(input: &[u8]) -> IResult<&[u8], &[u8]> {
2023-06-22 15:08:50 +02:00
dot_atom_text(input)
2023-06-13 15:12:16 +02:00
}
2023-07-19 18:30:43 +02:00
// @FIXME Missing obsolete
fn id_right(input: &[u8]) -> IResult<&[u8], &[u8]> {
2023-06-13 15:12:16 +02:00
alt((dot_atom_text, no_fold_litteral))(input)
}
2023-07-19 18:30:43 +02:00
fn no_fold_litteral(input: &[u8]) -> IResult<&[u8], &[u8]> {
2023-06-13 15:12:16 +02:00
delimited(tag("["), take_while(is_dtext), tag("]"))(input)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_msg_id() {
assert_eq!(
2023-07-19 18:30:43 +02:00
msg_id(b"<5678.21-Nov-1997@example.com>"),
2023-06-22 15:08:50 +02:00
Ok((
2023-07-19 18:30:43 +02:00
&b""[..],
2023-06-22 15:08:50 +02:00
MessageId {
2023-07-19 18:30:43 +02:00
left: &b"5678.21-Nov-1997"[..],
right: &b"example.com"[..],
2023-06-22 15:08:50 +02:00
}
)),
2023-06-13 15:12:16 +02:00
);
}
}