From 6d3505a960076fa5b31d1c2a57ee4438a9e10646 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Thu, 12 May 2022 18:38:08 -0400 Subject: [PATCH] Clean up use of generics on Session --- src/fetch.rs | 19 +++++-------------- src/inbox.rs | 15 ++++++--------- src/inbox/idle.rs | 9 ++++----- src/lib.rs | 18 ++++++++++-------- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/fetch.rs b/src/fetch.rs index 301050e..aca4f08 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -1,27 +1,18 @@ use std::error::Error; -use std::{fmt::Debug, time::Duration}; - -use async_imap::{ - extensions::idle::{Handle, IdleResponse}, - imap_proto::{MailboxDatum, Response}, - Session, -}; -use futures::{AsyncRead, AsyncWrite, StreamExt}; use crate::inbox::SequenceNumber; +use crate::IMAPSession; +use futures::StreamExt; /// `fetch_email` will consume the current session and put it into an `Idle` state, until a new email is received /// /// # Errors /// If, for any reason, the email fails to be fetched, one of `async_imap` error's will be returned. #[allow(clippy::module_name_repetitions)] -pub async fn fetch_email( - session: &mut Session, +pub async fn fetch_email( + session: &mut IMAPSession, sequence_number: SequenceNumber, -) -> Result> -where - T: AsyncRead + AsyncWrite + Unpin + Debug + Send, -{ +) -> Result> { let message = session .fetch(format!("{}", sequence_number.value()), "RFC822") .await? diff --git a/src/inbox.rs b/src/inbox.rs index eaf6455..c1eb1e5 100644 --- a/src/inbox.rs +++ b/src/inbox.rs @@ -4,12 +4,13 @@ use async_imap::{ error::{Error as IMAPError, Result as IMAPResult}, extensions::idle::Handle, imap_proto::{MailboxDatum, Response}, - Session, }; use futures::{AsyncRead, AsyncWrite}; use thiserror::Error; use tokio::sync::broadcast::{self, error::SendError}; +use crate::{IMAPClient, IMAPSession, IMAPTransportStream}; + mod idle; #[derive(Clone, Copy, Debug)] @@ -58,10 +59,7 @@ impl Watcher { /// Watch for the arrival of a single email. While the `Watcher` is watching, the caller must relinquish ownership /// of the `Session`, but it will be returned to it upon succesful completion of the - pub async fn watch_for_email(&self, session: Session) -> Result, WatchError> - where - T: AsyncRead + AsyncWrite + Unpin + Debug + Send, - { + pub async fn watch_for_email(&self, session: IMAPSession) -> Result { let mut idle_handle = session.idle(); let sequence_number = Self::idle_for_email(&mut idle_handle).await?; self.sender.send(sequence_number)?; @@ -70,10 +68,9 @@ impl Watcher { Ok(reclaimed_session) } - async fn idle_for_email(idle_handle: &mut Handle) -> IMAPResult - where - T: AsyncRead + AsyncWrite + Unpin + Debug + Send, - { + async fn idle_for_email( + idle_handle: &mut Handle, + ) -> IMAPResult { loop { idle_handle.init().await?; let idle_res = idle::wait_for_data(idle_handle).await; diff --git a/src/inbox/idle.rs b/src/inbox/idle.rs index 5d13aa5..59c9080 100644 --- a/src/inbox/idle.rs +++ b/src/inbox/idle.rs @@ -2,12 +2,14 @@ use std::{fmt::Debug, time::Duration}; use thiserror::Error; use async_imap::{ - error::{Error as IMAPError, Result as IMAPResult}, + error::Error as IMAPError, extensions::idle::{Handle, IdleResponse}, imap_proto::Response as IMAPResponse, }; use futures::{AsyncRead, AsyncWrite}; +use crate::IMAPTransportStream; + // 29 minutes, as per RFC2177 which specifies we should re-issue our idle every 29 minutes const WAIT_TIMEOUT: Duration = Duration::from_secs(29 * 60); @@ -55,10 +57,7 @@ impl Data { } } -pub async fn wait_for_data(idle_handle: &mut Handle) -> Result -where - T: AsyncRead + AsyncWrite + Unpin + Debug + Send, -{ +pub async fn wait_for_data(idle_handle: &mut Handle) -> Result { println!("starting timeout wait..."); let (idle_response_future, _stop) = idle_handle.wait_with_timeout(WAIT_TIMEOUT); let idle_response = idle_response_future.await?; diff --git a/src/lib.rs b/src/lib.rs index 337c2d5..c268085 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,8 +7,9 @@ extern crate log; use std::fmt::Debug; -use async_imap::{self, Client, Session}; -use async_native_tls::TlsConnector; +use async_imap; +use async_native_tls::{TlsConnector, TlsStream}; +use async_std::net::TcpStream; pub use config::{Config, IMAP as IMAPConfig}; use futures::{AsyncRead, AsyncWrite}; @@ -20,9 +21,12 @@ mod inbox; pub use fetch::fetch_email; pub use inbox::{SequenceNumber, Watcher}; -pub async fn setup_session( - cfg: &Config, -) -> async_imap::error::Result> { +type IMAPTransportStream = TlsStream; +// TODO: Probably doesn't need to be pub +type IMAPClient = async_imap::Client; +pub type IMAPSession = async_imap::Session; + +pub async fn setup_session(cfg: &Config) -> async_imap::error::Result { let imap_cfg = cfg.imap(); println!("Logging in..."); let client = build_imap_client(imap_cfg).await?; @@ -33,9 +37,7 @@ pub async fn setup_session( .map_err(|err| err.0) } -async fn build_imap_client( - imap_cfg: &IMAPConfig, -) -> async_imap::error::Result> { +async fn build_imap_client(imap_cfg: &IMAPConfig) -> async_imap::error::Result { let tls_connector = TlsConnector::new(); async_imap::connect( (imap_cfg.domain(), imap_cfg.port()),