mail_parser/core/
body.rs

1/*
2 * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use crate::{AttachmentIterator, BodyPartIterator, Message, MessagePart, MessagePartId, PartType};
8
9impl PartType<'_> {
10    #[allow(clippy::len_without_is_empty)]
11    pub fn len(&self) -> usize {
12        match self {
13            PartType::Text(v) | PartType::Html(v) => v.len(),
14            PartType::Binary(v) | PartType::InlineBinary(v) => v.len(),
15            PartType::Message(v) => v.raw_message.len(),
16            PartType::Multipart(_) => 0,
17        }
18    }
19}
20
21impl<'x> BodyPartIterator<'x> {
22    pub(crate) fn new(message: &'x Message<'x>, list: &'x [MessagePartId]) -> BodyPartIterator<'x> {
23        BodyPartIterator {
24            message,
25            list,
26            pos: -1,
27        }
28    }
29}
30
31impl<'x> Iterator for BodyPartIterator<'x> {
32    type Item = &'x MessagePart<'x>;
33
34    fn next(&mut self) -> Option<Self::Item> {
35        self.pos += 1;
36        self.message
37            .parts
38            .get(*self.list.get(self.pos as usize)? as usize)
39    }
40}
41
42impl<'x> AttachmentIterator<'x> {
43    pub(crate) fn new(message: &'x Message<'x>) -> AttachmentIterator<'x> {
44        AttachmentIterator { message, pos: -1 }
45    }
46}
47
48impl<'x> Iterator for AttachmentIterator<'x> {
49    type Item = &'x MessagePart<'x>;
50
51    fn next(&mut self) -> Option<Self::Item> {
52        self.pos += 1;
53        self.message.attachment(self.pos as u32)
54    }
55}