2022-06-22 12:58:57 +00:00
|
|
|
use std::error::Error as StdError;
|
2022-06-22 15:26:52 +00:00
|
|
|
use std::fmt;
|
2022-06-30 12:02:57 +00:00
|
|
|
use std::sync::Arc;
|
2022-06-20 16:09:20 +00:00
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
use crate::imap::mailbox_view::MailboxView;
|
2022-06-29 11:16:58 +00:00
|
|
|
use crate::mail::user::User;
|
2022-06-17 16:39:36 +00:00
|
|
|
|
2022-06-22 12:58:57 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
ForbiddenTransition,
|
|
|
|
}
|
|
|
|
impl fmt::Display for Error {
|
2022-06-22 15:26:52 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "Forbidden Transition")
|
|
|
|
}
|
2022-06-22 12:58:57 +00:00
|
|
|
}
|
2022-06-22 15:26:52 +00:00
|
|
|
impl StdError for Error {}
|
2022-06-22 12:58:57 +00:00
|
|
|
|
2022-06-17 16:39:36 +00:00
|
|
|
pub enum State {
|
|
|
|
NotAuthenticated,
|
2022-06-30 12:02:57 +00:00
|
|
|
Authenticated(Arc<User>),
|
|
|
|
Selected(Arc<User>, MailboxView),
|
2022-06-30 09:28:03 +00:00
|
|
|
// Examined is like Selected, but indicates that the mailbox is read-only
|
2022-06-30 12:02:57 +00:00
|
|
|
Examined(Arc<User>, MailboxView),
|
2022-06-22 15:26:52 +00:00
|
|
|
Logout,
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
2022-06-22 12:58:57 +00:00
|
|
|
|
|
|
|
pub enum Transition {
|
2022-06-29 10:50:44 +00:00
|
|
|
None,
|
2022-06-30 12:02:57 +00:00
|
|
|
Authenticate(Arc<User>),
|
2022-06-30 09:28:03 +00:00
|
|
|
Examine(MailboxView),
|
2022-06-29 13:39:54 +00:00
|
|
|
Select(MailboxView),
|
2022-06-22 15:26:52 +00:00
|
|
|
Unselect,
|
|
|
|
Logout,
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See RFC3501 section 3.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc3501#page-13
|
|
|
|
impl State {
|
2022-06-22 12:58:57 +00:00
|
|
|
pub fn apply(self, tr: Transition) -> Result<Self, Error> {
|
|
|
|
match (self, tr) {
|
2022-06-29 10:50:44 +00:00
|
|
|
(s, Transition::None) => Ok(s),
|
2022-06-22 12:58:57 +00:00
|
|
|
(State::NotAuthenticated, Transition::Authenticate(u)) => Ok(State::Authenticated(u)),
|
2022-07-13 13:26:00 +00:00
|
|
|
(
|
|
|
|
State::Authenticated(u) | State::Selected(u, _) | State::Examined(u, _),
|
|
|
|
Transition::Select(m),
|
|
|
|
) => Ok(State::Selected(u, m)),
|
|
|
|
(
|
|
|
|
State::Authenticated(u) | State::Selected(u, _) | State::Examined(u, _),
|
|
|
|
Transition::Examine(m),
|
|
|
|
) => Ok(State::Examined(u, m)),
|
2022-07-13 13:26:55 +00:00
|
|
|
(State::Selected(u, _) | State::Examined(u, _), Transition::Unselect) => {
|
|
|
|
Ok(State::Authenticated(u))
|
|
|
|
}
|
2022-06-22 12:58:57 +00:00
|
|
|
(_, Transition::Logout) => Ok(State::Logout),
|
|
|
|
_ => Err(Error::ForbiddenTransition),
|
|
|
|
}
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
|
|
|
}
|