forked from stalwartlabs/mail-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbody.rs
More file actions
56 lines (48 loc) · 1.49 KB
/
body.rs
File metadata and controls
56 lines (48 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::{AttachmentIterator, BodyPartIterator, Message, MessagePart, MessagePartId, PartType};
impl PartType<'_> {
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
match self {
PartType::Text(v) => v.len(),
PartType::Html(v) => v.potentially_wrong_charset().len(),
PartType::Binary(v) | PartType::InlineBinary(v) => v.len(),
PartType::Message(v) => v.raw_message.len(),
PartType::Multipart(_) => 0,
}
}
}
impl<'x> BodyPartIterator<'x> {
pub(crate) fn new(message: &'x Message<'x>, list: &'x [MessagePartId]) -> BodyPartIterator<'x> {
BodyPartIterator {
message,
list,
pos: -1,
}
}
}
impl<'x> Iterator for BodyPartIterator<'x> {
type Item = &'x MessagePart<'x>;
fn next(&mut self) -> Option<Self::Item> {
self.pos += 1;
self.message
.parts
.get(*self.list.get(self.pos as usize)? as usize)
}
}
impl<'x> AttachmentIterator<'x> {
pub(crate) fn new(message: &'x Message<'x>) -> AttachmentIterator<'x> {
AttachmentIterator { message, pos: -1 }
}
}
impl<'x> Iterator for AttachmentIterator<'x> {
type Item = &'x MessagePart<'x>;
fn next(&mut self) -> Option<Self::Item> {
self.pos += 1;
self.message.attachment(self.pos as u32)
}
}