Skip to content

Logic simplification, closing distance to #68 and #71 #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
55 changes: 17 additions & 38 deletions src/parsers/fields/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

use std::borrow::Cow;
use std::mem;

use crate::{parsers::MessageStream, Addr, Address, Group, HeaderValue};

Expand Down Expand Up @@ -138,15 +139,10 @@ impl<'x> AddressParser<'x> {
}

if !self.mail_tokens.is_empty() {
if self.group_name.is_none() {
self.group_name = concat_tokens(&mut self.mail_tokens).into();
if let Some(group_name) = &mut self.group_name {
*group_name = mem::take(group_name) + " " + concat_tokens(&mut self.mail_tokens);
} else {
self.group_name = Some(
(self.group_name.as_ref().unwrap().as_ref().to_owned()
+ " "
+ concat_tokens(&mut self.mail_tokens).as_ref())
.into(),
);
self.group_name = concat_tokens(&mut self.mail_tokens).into();
}
}
}
Expand All @@ -156,6 +152,10 @@ impl<'x> AddressParser<'x> {
let has_comment = self.group_comment.is_some();
let has_addresses = !self.addresses.is_empty();

if !has_name && !has_addresses {
return;
}

self.result
.push(if has_name && has_addresses && has_comment {
Group {
Expand All @@ -169,23 +169,16 @@ impl<'x> AddressParser<'x> {
),
addresses: std::mem::take(&mut self.addresses),
}
} else if has_addresses && has_name {
} else if has_name {
Group {
name: self.group_name.take(),
addresses: std::mem::take(&mut self.addresses),
}
} else if has_addresses {
} else {
Group {
name: self.group_comment.take(),
addresses: std::mem::take(&mut self.addresses),
}
} else if has_name {
Group {
name: self.group_name.take(),
addresses: Vec::new(),
}
} else {
return;
});
}
}
Expand Down Expand Up @@ -218,9 +211,7 @@ impl<'x> MessageStream<'x> {
b'\n' => {
parser.add_token(self, false);
if self.try_next_is_space() {
if !parser.is_token_start {
parser.is_token_start = true;
}
parser.is_token_start = true;
continue;
} else {
break;
Expand Down Expand Up @@ -286,19 +277,13 @@ impl<'x> MessageStream<'x> {
self.restore();
}
b' ' | b'\t' => {
if !parser.is_token_start {
parser.is_token_start = true;
}
if parser.is_escaped {
parser.is_escaped = false;
}
parser.is_token_start = true;
parser.is_escaped = false;
if parser.state == AddressState::Quote {
if parser.token_start == 0 {
parser.token_start = self.offset();
parser.token_end = parser.token_start;
} else {
parser.token_end = self.offset();
}
parser.token_end = self.offset();
}
continue;
}
Expand Down Expand Up @@ -334,20 +319,14 @@ impl<'x> MessageStream<'x> {
_ => (),
}

if parser.is_escaped {
parser.is_escaped = false;
}
parser.is_escaped = false;

if parser.is_token_start {
parser.is_token_start = false;
}
parser.is_token_start = false;

if parser.token_start == 0 {
parser.token_start = self.offset();
parser.token_end = parser.token_start;
} else {
parser.token_end = self.offset();
}
parser.token_end = self.offset();
}

parser.add_address();
Expand Down
24 changes: 10 additions & 14 deletions src/parsers/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl MessageParser {
_ => (false, false),
}
} else if is_inline {
if state.in_alternative && (state.need_text_body || state.need_html_body) {
if state.in_alternative {
match mime_type {
MimeType::TextHtml => {
state.need_text_body = false;
Expand All @@ -314,6 +314,12 @@ impl MessageParser {
message.text_body.push(message.parts.len());
}

let is_html = mime_type == MimeType::TextHtml;

if !is_text || !add_to_html && is_html || !add_to_text && !is_html {
message.attachments.push(message.parts.len());
}

if is_text {
let text = match (
bytes,
Expand All @@ -332,25 +338,15 @@ impl MessageParser {
(Cow::Borrowed(bytes), None) => String::from_utf8_lossy(bytes),
};

let is_html = mime_type == MimeType::TextHtml;

if !add_to_html && is_html || !add_to_text && !is_html {
message.attachments.push(message.parts.len());
}

if is_html {
PartType::Html(text)
} else {
PartType::Text(text)
}
} else if is_inline {
PartType::InlineBinary(bytes)
} else {
message.attachments.push(message.parts.len());

if !is_inline {
PartType::Binary(bytes)
} else {
PartType::InlineBinary(bytes)
}
PartType::Binary(bytes)
}
} else {
message.attachments.push(message.parts.len());
Expand Down