aboutsummaryrefslogtreecommitdiff
path: root/src/imap/flow.rs
blob: 8ba75bc23b83b50f6992d42ed60da44575dde959 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::mailbox::Mailbox;

pub struct User {
    pub name: String,
    pub creds: Credentials,
}

pub enum State {
    NotAuthenticated,
    Authenticated(User),
    Selected(User, Mailbox),
    Logout
}
pub enum Error {
    ForbiddenTransition,
}

// See RFC3501 section 3.
// https://datatracker.ietf.org/doc/html/rfc3501#page-13
impl State {
    pub fn authenticate(&mut self, user: User) -> Result<(), Error> {
        self = match state {
            State::NotAuthenticated => State::Authenticated(user),
            _ => return Err(ForbiddenTransition),
        };
        Ok(())
    }

    pub fn logout(&mut self) -> Self {
        self = State::Logout;
        Ok(())
    }

    pub fn select(&mut self, mailbox: Mailbox) -> Result<(), Error> {
        self = match state {
            State::Authenticated(user) => State::Selected(user, mailbox),
            _ => return Err(ForbiddenTransition),
        };
        Ok(())
    }

    pub fn unselect(state: State) -> Result<(), Error> {
        self = match state {
            State::Selected(user, _) => State::Authenticated(user),
            _ => return Err(ForbiddenTransition),
        };
        Ok(())
    }
}