From 43b668531f060a2c0f950da96b363b2ea7cf4e06 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 18 Jan 2024 18:02:24 +0100 Subject: fix a transition bug --- src/imap/flow.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/imap/flow.rs b/src/imap/flow.rs index 72d9e8e..e817e77 100644 --- a/src/imap/flow.rs +++ b/src/imap/flow.rs @@ -25,6 +25,18 @@ pub enum State { Idle(Arc, MailboxView, MailboxPerm, Tag<'static>, Arc), Logout, } +impl fmt::Display for State { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use State::*; + match self { + NotAuthenticated => write!(f, "NotAuthenticated"), + Authenticated(..) => write!(f, "Authenticated"), + Selected(..) => write!(f, "Selected"), + Idle(..) => write!(f, "Idle"), + Logout => write!(f, "Logout"), + } + } +} #[derive(Clone)] pub enum MailboxPerm { @@ -41,13 +53,29 @@ pub enum Transition { Unselect, Logout, } +impl fmt::Display for Transition { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use Transition::*; + match self { + None => write!(f, "None"), + Authenticate(..) => write!(f, "Authenticated"), + Select(..) => write!(f, "Selected"), + Idle(..) => write!(f, "Idle"), + UnIdle => write!(f, "UnIdle"), + Unselect => write!(f, "Unselect"), + Logout => write!(f, "Logout"), + } + } +} // See RFC3501 section 3. // https://datatracker.ietf.org/doc/html/rfc3501#page-13 impl State { pub fn apply(&mut self, tr: Transition) -> Result<(), Error> { - let new_state = match (std::mem::replace(self, State::NotAuthenticated), tr) { - (_s, Transition::None) => return Ok(()), + tracing::debug!(state=%self, transition=%tr, "try change state"); + + let new_state = match (std::mem::replace(self, State::Logout), tr) { + (s, Transition::None) => s, (State::NotAuthenticated, Transition::Authenticate(u)) => State::Authenticated(u), ( State::Authenticated(u) | State::Selected(u, _, _), @@ -63,10 +91,13 @@ impl State { State::Selected(u, m, p) }, (_, Transition::Logout) => State::Logout, - _ => return Err(Error::ForbiddenTransition), + (s, t) => { + tracing::error!(state=%s, transition=%t, "forbidden transition"); + return Err(Error::ForbiddenTransition) + } }; - *self = new_state; + tracing::debug!(state=%self, "transition succeeded"); Ok(()) } -- cgit v1.2.3