diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-01-18 18:02:24 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-01-18 18:02:24 +0100 |
commit | 43b668531f060a2c0f950da96b363b2ea7cf4e06 (patch) | |
tree | 0f1872809d75c813775bbde343dadd694d50c2fe /src/imap/flow.rs | |
parent | 185033c462b92854117bc57258bf33b3579a7ca5 (diff) | |
download | aerogramme-43b668531f060a2c0f950da96b363b2ea7cf4e06.tar.gz aerogramme-43b668531f060a2c0f950da96b363b2ea7cf4e06.zip |
fix a transition bug
Diffstat (limited to 'src/imap/flow.rs')
-rw-r--r-- | src/imap/flow.rs | 39 |
1 files 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<User>, MailboxView, MailboxPerm, Tag<'static>, Arc<Notify>), 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(()) } |