Skip to content

Commit 4fe30a6

Browse files
committed
replace PartType::Html(Cow<'x, str>) with PartType::Html(Html<'x>)
1 parent f3d1475 commit 4fe30a6

File tree

8 files changed

+34
-20
lines changed

8 files changed

+34
-20
lines changed

examples/message_parse.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
8989

9090
// HTML and text body parts are returned conforming to RFC8621, Section 4.1.4
9191
assert_eq!(
92-
message.body_html(0).unwrap(),
92+
message.body_html(0).unwrap().potentially_wrong_charset(),
9393
concat!(
9494
"<html><p>I was thinking about quitting the &ldquo;exporting&rdquo; to ",
9595
"focus just on the &ldquo;importing&rdquo;,</p><p>but then I thought,",
@@ -120,7 +120,10 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
120120
"ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!"
121121
);
122122
assert_eq!(
123-
nested_message.body_html(0).unwrap(),
123+
nested_message
124+
.body_html(0)
125+
.unwrap()
126+
.potentially_wrong_charset(),
124127
"<html><body>ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!</body></html>"
125128
);
126129

src/core/body.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ impl PartType<'_> {
1010
#[allow(clippy::len_without_is_empty)]
1111
pub fn len(&self) -> usize {
1212
match self {
13-
PartType::Text(v) | PartType::Html(v) => v.len(),
13+
PartType::Text(v) => v.len(),
14+
PartType::Html(v) => v.potentially_wrong_charset().len(),
1415
PartType::Binary(v) | PartType::InlineBinary(v) => v.len(),
1516
PartType::Message(v) => v.raw_message.len(),
1617
PartType::Multipart(_) => 0,

src/core/header.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,8 @@ impl<'x> MessagePart<'x> {
649649
/// Returns the body part's contents as a `u8` slice
650650
pub fn contents(&self) -> &[u8] {
651651
match &self.body {
652-
PartType::Text(text) | PartType::Html(text) => text.as_bytes(),
652+
PartType::Text(text) => text.as_bytes(),
653+
PartType::Html(text) => text.potentially_wrong_charset().as_bytes(),
653654
PartType::Binary(bin) | PartType::InlineBinary(bin) => bin.as_ref(),
654655
PartType::Message(message) => message.raw_message(),
655656
PartType::Multipart(_) => b"",
@@ -659,7 +660,8 @@ impl<'x> MessagePart<'x> {
659660
/// Returns the body part's contents as a `str`
660661
pub fn text_contents(&self) -> Option<&str> {
661662
match &self.body {
662-
PartType::Text(text) | PartType::Html(text) => text.as_ref().into(),
663+
PartType::Text(text) => text.as_ref().into(),
664+
PartType::Html(text) => text.potentially_wrong_charset().as_ref().into(),
663665
PartType::Binary(bin) | PartType::InlineBinary(bin) => {
664666
std::str::from_utf8(bin.as_ref()).ok()
665667
}
@@ -689,7 +691,8 @@ impl<'x> MessagePart<'x> {
689691
/// Returns the body part's length
690692
pub fn len(&self) -> usize {
691693
match &self.body {
692-
PartType::Text(text) | PartType::Html(text) => text.len(),
694+
PartType::Text(text) => text.len(),
695+
PartType::Html(text) => text.potentially_wrong_charset().len(),
693696
PartType::Binary(bin) | PartType::InlineBinary(bin) => bin.len(),
694697
PartType::Message(message) => message.raw_message().len(),
695698
PartType::Multipart(_) => 0,
@@ -758,7 +761,7 @@ impl<'x> MessagePart<'x> {
758761
is_encoding_problem: self.is_encoding_problem,
759762
body: match self.body {
760763
PartType::Text(v) => PartType::Text(v.into_owned().into()),
761-
PartType::Html(v) => PartType::Html(v.into_owned().into()),
764+
PartType::Html(v) => PartType::Html(v.make_owned()),
762765
PartType::Binary(v) => PartType::Binary(v.into_owned().into()),
763766
PartType::InlineBinary(v) => PartType::InlineBinary(v.into_owned().into()),
764767
PartType::Message(v) => PartType::Message(v.into_owned()),

src/core/message.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
MessageStream,
1515
},
1616
Address, AttachmentIterator, BodyPartIterator, DateTime, GetHeader, Header, HeaderForm,
17-
HeaderName, HeaderValue, Message, MessageParser, MessagePart, PartType, Received,
17+
HeaderName, HeaderValue, Html, Message, MessageParser, MessagePart, PartType, Received,
1818
};
1919

2020
impl<'x> Message<'x> {
@@ -391,11 +391,11 @@ impl<'x> Message<'x> {
391391
}
392392

393393
/// Returns a message body part as text/plain
394-
pub fn body_html(&'x self, pos: usize) -> Option<Cow<'x, str>> {
394+
pub fn body_html(&'x self, pos: usize) -> Option<Html<'x>> {
395395
let part = self.parts.get(*self.html_body.get(pos)? as usize)?;
396396
match &part.body {
397-
PartType::Html(html) => Some(html.as_ref().into()),
398-
PartType::Text(text) => Some(text_to_html(text.as_ref()).into()),
397+
PartType::Html(html) => Some(html.to_owned()),
398+
PartType::Text(text) => Some(Html::new(text_to_html(text.as_ref()).into())),
399399
_ => None,
400400
}
401401
}
@@ -405,7 +405,7 @@ impl<'x> Message<'x> {
405405
let part = self.parts.get(*self.text_body.get(pos)? as usize)?;
406406
match &part.body {
407407
PartType::Text(text) => Some(text.as_ref().into()),
408-
PartType::Html(html) => Some(html_to_text(html.as_ref()).into()),
408+
PartType::Html(html) => Some(html_to_text(html.potentially_wrong_charset()).into()),
409409
_ => None,
410410
}
411411
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub enum PartType<'x> {
134134
Text(#[cfg_attr(feature = "rkyv", rkyv(with = rkyv::with::AsOwned))] Cow<'x, str>),
135135

136136
/// A text/html part
137-
Html(#[cfg_attr(feature = "rkyv", rkyv(with = rkyv::with::AsOwned))] Cow<'x, str>),
137+
Html(#[cfg_attr(feature = "rkyv", rkyv(with = rkyv::with::AsOwned))] Html<'x>),
138138

139139
/// Any other part type that is not text.
140140
Binary(#[cfg_attr(feature = "rkyv", rkyv(with = rkyv::with::AsOwned))] Cow<'x, [u8]>),

src/parsers/message.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::borrow::Cow;
88

99
use crate::{
1010
decoders::{charsets::map::charset_decoder, DecodeFnc},
11-
ContentType, Encoding, GetHeader, HeaderName, HeaderValue, Message, MessageParser, MessagePart,
12-
MessagePartId, PartType,
11+
ContentType, Encoding, GetHeader, HeaderName, HeaderValue, Html, Message, MessageParser,
12+
MessagePart, MessagePartId, PartType,
1313
};
1414

1515
use super::MessageStream;
@@ -337,7 +337,7 @@ impl MessageParser {
337337
}
338338

339339
if is_html {
340-
PartType::Html(text)
340+
PartType::Html(Html::new(text))
341341
} else {
342342
PartType::Text(text)
343343
}

src/parsers/preview.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
use std::borrow::Cow;
88

99
use crate::decoders::html::html_to_text;
10+
use crate::Html;
1011

11-
pub fn preview_html<'x>(html: Cow<'_, str>, max_len: usize) -> Cow<'x, str> {
12-
preview_text(html_to_text(html.as_ref()).into(), max_len)
12+
pub fn preview_html<'x>(html: Html<'_>, max_len: usize) -> Cow<'x, str> {
13+
preview_text(
14+
html_to_text(html.potentially_wrong_charset()).into(),
15+
max_len,
16+
)
1317
}
1418

1519
pub fn preview_text<'x>(text: Cow<'_, str>, mut max_len: usize) -> Cow<'x, str> {

tests/integration_test.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
108108
);
109109

110110
assert_eq!(
111-
message.body_html(0).unwrap(),
111+
message.body_html(0).unwrap().potentially_wrong_charset(),
112112
concat!(
113113
"<html><p>I was thinking about quitting the &ldquo;exporting&rdquo; to ",
114114
"focus just on the &ldquo;importing&rdquo;,</p><p>but then I thought,",
@@ -137,7 +137,10 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
137137
);
138138

139139
assert_eq!(
140-
nested_message.body_html(0).unwrap(),
140+
nested_message
141+
.body_html(0)
142+
.unwrap()
143+
.potentially_wrong_charset(),
141144
"<html><body>ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!</body></html>"
142145
);
143146

0 commit comments

Comments
 (0)