use async_imap::{ error::Result as ImapResult, types::{Capabilities, Fetch, Mailbox, Name}, Session as ImapSession, }; use async_native_tls::TlsStream; use async_std::net::TcpStream; use async_std::prelude::*; #[derive(Debug)] pub(crate) enum Session { Secure(ImapSession>), Insecure(ImapSession), } impl Session { pub async fn capabilities(&mut self) -> ImapResult { let res = match self { Session::Secure(i) => i.capabilities().await?, Session::Insecure(i) => i.capabilities().await?, }; Ok(res) } pub async fn list( &mut self, reference_name: Option<&str>, mailbox_pattern: Option<&str>, ) -> ImapResult> { let res = match self { Session::Secure(i) => { i.list(reference_name, mailbox_pattern) .await? .collect::>() .await? } Session::Insecure(i) => { i.list(reference_name, mailbox_pattern) .await? .collect::>() .await? } }; Ok(res) } pub async fn create>(&mut self, mailbox_name: S) -> ImapResult<()> { match self { Session::Secure(i) => i.create(mailbox_name).await?, Session::Insecure(i) => i.create(mailbox_name).await?, } Ok(()) } pub async fn subscribe>(&mut self, mailbox: S) -> ImapResult<()> { match self { Session::Secure(i) => i.subscribe(mailbox).await?, Session::Insecure(i) => i.subscribe(mailbox).await?, } Ok(()) } pub async fn close(&mut self) -> ImapResult<()> { match self { Session::Secure(i) => i.close().await?, Session::Insecure(i) => i.close().await?, } Ok(()) } pub async fn select>(&mut self, mailbox_name: S) -> ImapResult { let mbox = match self { Session::Secure(i) => i.select(mailbox_name).await?, Session::Insecure(i) => i.select(mailbox_name).await?, }; Ok(mbox) } pub async fn fetch(&mut self, sequence_set: S1, query: S2) -> ImapResult> where S1: AsRef, S2: AsRef, { let res = match self { Session::Secure(i) => { i.fetch(sequence_set, query) .await? .collect::>() .await? } Session::Insecure(i) => { i.fetch(sequence_set, query) .await? .collect::>() .await? } }; Ok(res) } pub async fn uid_fetch(&mut self, uid_set: S1, query: S2) -> ImapResult> where S1: AsRef, S2: AsRef, { let res = match self { Session::Secure(i) => { i.uid_fetch(uid_set, query) .await? .collect::>() .await? } Session::Insecure(i) => { i.uid_fetch(uid_set, query) .await? .collect::>() .await? } }; Ok(res) } pub async fn uid_store(&mut self, uid_set: S1, query: S2) -> ImapResult> where S1: AsRef, S2: AsRef, { let res = match self { Session::Secure(i) => { i.uid_store(uid_set, query) .await? .collect::>() .await? } Session::Insecure(i) => { i.uid_store(uid_set, query) .await? .collect::>() .await? } }; Ok(res) } pub async fn uid_mv, S2: AsRef>( &mut self, uid_set: S1, mailbox_name: S2, ) -> ImapResult<()> { match self { Session::Secure(i) => i.uid_mv(uid_set, mailbox_name).await?, Session::Insecure(i) => i.uid_mv(uid_set, mailbox_name).await?, } Ok(()) } pub async fn uid_copy, S2: AsRef>( &mut self, uid_set: S1, mailbox_name: S2, ) -> ImapResult<()> { match self { Session::Secure(i) => i.uid_copy(uid_set, mailbox_name).await?, Session::Insecure(i) => i.uid_copy(uid_set, mailbox_name).await?, } Ok(()) } }