aerogramme/src/imap/flow.rs

47 lines
1.2 KiB
Rust
Raw Normal View History

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-20 16:09:20 +00:00
2022-06-29 11:16:58 +00:00
use crate::mail::mailbox::Mailbox;
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,
Authenticated(User),
Selected(User, Mailbox),
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-22 15:26:52 +00:00
Authenticate(User),
Select(Mailbox),
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)),
(State::Authenticated(u), Transition::Select(m)) => Ok(State::Selected(u, m)),
(State::Selected(u, _), Transition::Unselect) => Ok(State::Authenticated(u)),
(_, Transition::Logout) => Ok(State::Logout),
_ => Err(Error::ForbiddenTransition),
}
2022-06-17 16:39:36 +00:00
}
}