Use bool instead of CallbackAction.

This commit is contained in:
Todd Mortimer 2021-04-07 19:45:04 -04:00
parent 5942553e7d
commit 7eb2cfde74
2 changed files with 17 additions and 26 deletions

View file

@ -1,4 +1,3 @@
use imap::extensions::idle;
use native_tls::TlsConnector; use native_tls::TlsConnector;
use structopt::StructOpt; use structopt::StructOpt;
@ -68,9 +67,11 @@ fn main() {
num_responses += 1; num_responses += 1;
println!("IDLE response #{}: {:?}", num_responses, response); println!("IDLE response #{}: {:?}", num_responses, response);
if num_responses >= max_responses { if num_responses >= max_responses {
idle::CallbackAction::Stop // Stop IDLE
false
} else { } else {
idle::CallbackAction::Continue // Continue IDLE
true
} }
}); });

View file

@ -19,8 +19,8 @@ use std::time::Duration;
/// ///
/// Each of the `wait` functions takes a callback function which receives any responses /// Each of the `wait` functions takes a callback function which receives any responses
/// that arrive on the channel while IDLE. The callback function implements whatever /// that arrive on the channel while IDLE. The callback function implements whatever
/// logic is needed to handle the IDLE response, and then returns a [`CallbackAction`] /// logic is needed to handle the IDLE response, and then returns a boolean
/// to `Continue` or `Stop` listening on the channel. /// to continue idling (`true`) or stop (`false`).
/// For users that want the IDLE to exit on any change (the behavior proior to version 3.0), /// For users that want the IDLE to exit on any change (the behavior proior to version 3.0),
/// a convenience callback function [`stop_on_any`] is provided. /// a convenience callback function [`stop_on_any`] is provided.
/// ///
@ -65,19 +65,9 @@ pub enum WaitOutcome {
MailboxChanged, MailboxChanged,
} }
/// Return type for IDLE response callbacks. Tells the IDLE connection
/// if it should continue monitoring the connection or not.
#[derive(Debug, PartialEq, Eq)]
pub enum CallbackAction {
/// Continue receiving responses from the IDLE connection.
Continue,
/// Stop receiving responses, and exit the IDLE wait.
Stop,
}
/// A convenience function to always cause the IDLE handler to exit on any change. /// A convenience function to always cause the IDLE handler to exit on any change.
pub fn stop_on_any(_response: UnsolicitedResponse) -> CallbackAction { pub fn stop_on_any(_response: UnsolicitedResponse) -> bool {
CallbackAction::Stop false
} }
/// Must be implemented for a transport in order for a `Session` using that transport to support /// Must be implemented for a transport in order for a `Session` using that transport to support
@ -142,7 +132,7 @@ impl<'a, T: Read + Write + 'a> Handle<'a, T> {
/// This is necessary so that we can keep using the inner `Session` in `wait_keepalive`. /// This is necessary so that we can keep using the inner `Session` in `wait_keepalive`.
fn wait_inner<F>(&mut self, reconnect: bool, mut callback: F) -> Result<WaitOutcome> fn wait_inner<F>(&mut self, reconnect: bool, mut callback: F) -> Result<WaitOutcome>
where where
F: FnMut(UnsolicitedResponse) -> CallbackAction, F: FnMut(UnsolicitedResponse) -> bool,
{ {
let mut v = Vec::new(); let mut v = Vec::new();
let result = loop { let result = loop {
@ -162,7 +152,7 @@ impl<'a, T: Read + Write + 'a> Handle<'a, T> {
match parse_idle(&v) { match parse_idle(&v) {
(_rest, Some(Err(r))) => break Err(r), (_rest, Some(Err(r))) => break Err(r),
(rest, Some(Ok(response))) => { (rest, Some(Ok(response))) => {
if let CallbackAction::Stop = callback(response) { if !callback(response) {
break Ok(WaitOutcome::MailboxChanged); break Ok(WaitOutcome::MailboxChanged);
} }
rest rest
@ -201,11 +191,11 @@ impl<'a, T: Read + Write + 'a> Handle<'a, T> {
} }
} }
/// Block until the given callback returns `Stop`, or until a response /// Block until the given callback returns `false`, or until a response
/// arrives that is not explicitly handled by [`UnsolicitedResponse`]. /// arrives that is not explicitly handled by [`UnsolicitedResponse`].
pub fn wait<F>(mut self, callback: F) -> Result<()> pub fn wait<F>(mut self, callback: F) -> Result<()>
where where
F: FnMut(UnsolicitedResponse) -> CallbackAction, F: FnMut(UnsolicitedResponse) -> bool,
{ {
self.wait_inner(true, callback).map(|_| ()) self.wait_inner(true, callback).map(|_| ())
} }
@ -219,7 +209,7 @@ impl<'a, T: SetReadTimeout + Read + Write + 'a> Handle<'a, T> {
self.keepalive = interval; self.keepalive = interval;
} }
/// Block until the given callback returns `Stop`, or until a response /// Block until the given callback returns `false`, or until a response
/// arrives that is not explicitly handled by [`UnsolicitedResponse`]. /// arrives that is not explicitly handled by [`UnsolicitedResponse`].
/// ///
/// This method differs from [`Handle::wait`] in that it will periodically refresh the IDLE /// This method differs from [`Handle::wait`] in that it will periodically refresh the IDLE
@ -230,7 +220,7 @@ impl<'a, T: SetReadTimeout + Read + Write + 'a> Handle<'a, T> {
/// This is the recommended method to use for waiting. /// This is the recommended method to use for waiting.
pub fn wait_keepalive<F>(self, callback: F) -> Result<()> pub fn wait_keepalive<F>(self, callback: F) -> Result<()>
where where
F: FnMut(UnsolicitedResponse) -> CallbackAction, F: FnMut(UnsolicitedResponse) -> bool,
{ {
// The server MAY consider a client inactive if it has an IDLE command // The server MAY consider a client inactive if it has an IDLE command
// running, and if such a server has an inactivity timeout it MAY log // running, and if such a server has an inactivity timeout it MAY log
@ -244,11 +234,11 @@ impl<'a, T: SetReadTimeout + Read + Write + 'a> Handle<'a, T> {
} }
/// Block until the given given amount of time has elapsed, the given callback /// Block until the given given amount of time has elapsed, the given callback
/// returns `Stop`, or until a response arrives that is not explicitly handled /// returns `false`, or until a response arrives that is not explicitly handled
/// by [`UnsolicitedResponse`]. /// by [`UnsolicitedResponse`].
pub fn wait_with_timeout<F>(self, timeout: Duration, callback: F) -> Result<WaitOutcome> pub fn wait_with_timeout<F>(self, timeout: Duration, callback: F) -> Result<WaitOutcome>
where where
F: FnMut(UnsolicitedResponse) -> CallbackAction, F: FnMut(UnsolicitedResponse) -> bool,
{ {
self.timed_wait(timeout, false, callback) self.timed_wait(timeout, false, callback)
} }
@ -260,7 +250,7 @@ impl<'a, T: SetReadTimeout + Read + Write + 'a> Handle<'a, T> {
callback: F, callback: F,
) -> Result<WaitOutcome> ) -> Result<WaitOutcome>
where where
F: FnMut(UnsolicitedResponse) -> CallbackAction, F: FnMut(UnsolicitedResponse) -> bool,
{ {
self.session self.session
.stream .stream