use automatic serialization, thanks @link2xt for the tip

This commit is contained in:
holger krekel
2019-12-08 19:56:55 +01:00
parent 84bcc81fda
commit 2d9bb929ad
3 changed files with 11 additions and 72 deletions

View File

@@ -1533,7 +1533,7 @@ fn is_known_rfc724_mid(context: &Context, rfc724_mid: &mailparse::MailAddr) -> b
/// - checks also if any of the referenced IDs are send by a messenger /// - checks also if any of the referenced IDs are send by a messenger
/// - it is okay, if the referenced messages are moved to trash here /// - it is okay, if the referenced messages are moved to trash here
/// - no check for the Chat-* headers (function is only called if it is no messenger message itself) /// - no check for the Chat-* headers (function is only called if it is no messenger message itself)
fn dc_is_reply_to_messenger_message(context: &Context, mime_parser: &MimeParser) -> bool { fn is_reply_to_messenger_message(context: &Context, mime_parser: &MimeParser) -> bool {
if let Some(value) = mime_parser.get(HeaderDef::InReplyTo) { if let Some(value) = mime_parser.get(HeaderDef::InReplyTo) {
if is_msgrmsg_rfc724_mid_in_list(context, &value) { if is_msgrmsg_rfc724_mid_in_list(context, &value) {
return true; return true;

View File

@@ -1,104 +1,43 @@
use crate::strum::EnumProperty; #[derive(Debug, Display, Clone, PartialEq, Eq, EnumVariantNames)]
#[strum(serialize_all = "kebab_case")]
#[derive(Debug, Clone, PartialEq, Eq, EnumProperty)] #[allow(dead_code)]
pub enum HeaderDef { pub enum HeaderDef {
#[strum(props(header = "message-id"))]
MessageId, MessageId,
#[strum(props(header = "subject"))]
Subject, Subject,
#[strum(props(header = "date"))]
Date, Date,
#[strum(props(header = "from"))]
From_, From_,
#[strum(props(header = "to"))]
To, To,
#[strum(props(header = "cc"))]
Cc, Cc,
#[strum(props(header = "disposition"))]
Disposition, Disposition,
#[strum(props(header = "original-message-id"))]
OriginalMessageId, OriginalMessageId,
#[strum(props(header = "list-id"))]
ListId, ListId,
#[strum(props(header = "references"))]
References, References,
#[strum(props(header = "in-reply-to"))]
InReplyTo, InReplyTo,
#[strum(props(header = "precedence"))]
Precedence, Precedence,
#[strum(props(header = "chat-version"))]
ChatVersion, ChatVersion,
#[strum(props(header = "chat-group-id"))]
ChatGroupId, ChatGroupId,
#[strum(props(header = "chat-group-name"))]
ChatGroupName, ChatGroupName,
#[strum(props(header = "chat-group-name-changed"))]
ChatGroupNameChanged, ChatGroupNameChanged,
#[strum(props(header = "chat-verified"))]
ChatVerified, ChatVerified,
#[strum(props(header = "chat-group-image"))]
ChatGroupImage, ChatGroupImage,
#[strum(props(header = "chat-voice-message"))]
ChatVoiceMessage, ChatVoiceMessage,
#[strum(props(header = "chat-group-member-removed"))]
ChatGroupMemberRemoved, ChatGroupMemberRemoved,
#[strum(props(header = "chat-group-member-added"))]
ChatGroupMemberAdded, ChatGroupMemberAdded,
#[strum(props(header = "chat-content"))]
ChatContent, ChatContent,
#[strum(props(header = "chat-duration"))]
ChatDuration, ChatDuration,
#[strum(props(header = "chat-disposition-notification-to"))]
ChatDispositionNotificationTo, ChatDispositionNotificationTo,
#[strum(props(header = "autocrypt-setup-message"))]
AutocryptSetupMessage, AutocryptSetupMessage,
#[strum(props(header = "secure-join"))]
SecureJoin, SecureJoin,
#[strum(props(header = "secure-join-group"))]
SecureJoinGroup, SecureJoinGroup,
#[strum(props(header = "secure-join-fingerprint"))]
SecureJoinFingerprint, SecureJoinFingerprint,
#[strum(props(header = "secure-join-invitenumber"))]
SecureJoinInvitenumber, SecureJoinInvitenumber,
#[strum(props(header = "secure-join-group"))]
SecureJoinAuth, SecureJoinAuth,
#[strum(props(header = "test-header"))]
_TestHeader, _TestHeader,
} }
impl HeaderDef { impl HeaderDef {
/// Returns the corresponding Event id. /// Returns the corresponding Event id.
pub fn get_headername(&self) -> &str { pub fn get_headername(&self) -> String {
self.get_str("header").expect("missing header definition") self.to_string()
} }
} }

View File

@@ -350,7 +350,7 @@ impl<'a> MimeParser<'a> {
} }
pub fn get(&self, headerdef: HeaderDef) -> Option<&String> { pub fn get(&self, headerdef: HeaderDef) -> Option<&String> {
self.header.get(headerdef.get_headername()) self.header.get(&headerdef.get_headername())
} }
fn parse_mime_recursive(&mut self, mail: &mailparse::ParsedMail<'_>) -> Result<bool> { fn parse_mime_recursive(&mut self, mail: &mailparse::ParsedMail<'_>) -> Result<bool> {
@@ -729,9 +729,9 @@ impl<'a> MimeParser<'a> {
// must be present // must be present
let disp = HeaderDef::Disposition.get_headername(); let disp = HeaderDef::Disposition.get_headername();
if let Some(_disposition) = report_fields.get_first_value(disp).ok().flatten() { if let Some(_disposition) = report_fields.get_first_value(&disp).ok().flatten() {
if let Some(original_message_id) = report_fields if let Some(original_message_id) = report_fields
.get_first_value(HeaderDef::OriginalMessageId.get_headername()) .get_first_value(&HeaderDef::OriginalMessageId.get_headername())
.ok() .ok()
.flatten() .flatten()
.and_then(|v| parse_message_id(&v)) .and_then(|v| parse_message_id(&v))
@@ -1126,9 +1126,9 @@ mod tests {
\n\ \n\
\x00"; \x00";
let mimeparser = MimeParser::from_bytes(&context.ctx, &raw[..]).unwrap(); let mimeparser = MimeParser::from_bytes(&context.ctx, &raw[..]).unwrap();
// test that we treat Subject as a protected header that can // test that we treat Subject as a protected header that can
// bubble upwards // bubble upwards
assert_eq!(mimeparser.get_subject(), Some("inner-subject".into())); assert_eq!(mimeparser.get_subject(), Some("inner-subject".into()));
let of = mimeparser.get(HeaderDef::SecureJoinGroup).unwrap(); let of = mimeparser.get(HeaderDef::SecureJoinGroup).unwrap();