email/email/message/
imap.rs

1use imap_client::imap_next::imap_types::fetch::{
2    MacroOrMessageDataItemNames, MessageDataItem, MessageDataItemName,
3};
4use once_cell::sync::Lazy;
5
6use super::Message;
7use crate::email::{Error, Result};
8
9/// The IMAP fetch items needed to retrieve everything we need to
10/// build an envelope: UID, flags and envelope (Message-ID, From, To,
11/// Subject, Date).
12pub static FETCH_MESSAGES: Lazy<MacroOrMessageDataItemNames<'static>> = Lazy::new(|| {
13    MacroOrMessageDataItemNames::MessageDataItemNames(vec![MessageDataItemName::BodyExt {
14        section: None,
15        partial: None,
16        peek: false,
17    }])
18});
19
20/// Same as [`FETCH_MESSAGES`], but with peek set a `true`.
21pub static PEEK_MESSAGES: Lazy<MacroOrMessageDataItemNames<'static>> = Lazy::new(|| {
22    MacroOrMessageDataItemNames::MessageDataItemNames(vec![MessageDataItemName::BodyExt {
23        section: None,
24        partial: None,
25        peek: true,
26    }])
27});
28
29impl<'a> TryFrom<&'a [MessageDataItem<'_>]> for Message<'a> {
30    type Error = Error;
31
32    fn try_from(items: &'a [MessageDataItem]) -> Result<Self> {
33        for item in items {
34            if let MessageDataItem::BodyExt { data, .. } = item {
35                if let Some(data) = data.0.as_ref() {
36                    return Ok(Message::from(data.as_ref()));
37                }
38            }
39        }
40
41        Err(Error::ParseEmailEmptyRawError)
42    }
43}