imap_client/tasks/tasks/
login.rs

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
51
52
53
54
55
56
57
58
59
60
61
62
use imap_next::imap_types::{
    command::CommandBody,
    core::{AString, Vec1},
    response::{Capability, Code, Data, StatusBody, StatusKind},
    secret::Secret,
};

use super::TaskError;
use crate::tasks::Task;

#[derive(Clone, Debug)]
pub struct LoginTask {
    username: AString<'static>,
    password: Secret<AString<'static>>,
    output: Option<Vec1<Capability<'static>>>,
}

impl LoginTask {
    pub fn new(username: AString<'static>, password: Secret<AString<'static>>) -> Self {
        Self {
            username,
            password,
            output: None,
        }
    }
}

impl Task for LoginTask {
    type Output = Result<Option<Vec1<Capability<'static>>>, TaskError>;

    fn command_body(&self) -> CommandBody<'static> {
        CommandBody::Login {
            username: self.username.clone(),
            password: self.password.clone(),
        }
    }

    // Capabilities may (unfortunately) be found in a data response.
    // See https://github.com/modern-email/defects/issues/18
    fn process_data(&mut self, data: Data<'static>) -> Option<Data<'static>> {
        if let Data::Capability(capabilities) = data {
            self.output = Some(capabilities);
            None
        } else {
            Some(data)
        }
    }

    fn process_tagged(self, status_body: StatusBody<'static>) -> Self::Output {
        match status_body.kind {
            StatusKind::Ok => Ok(
                if let Some(Code::Capability(capabilities)) = status_body.code {
                    Some(capabilities)
                } else {
                    self.output
                },
            ),
            StatusKind::No => Err(TaskError::UnexpectedNoResponse(status_body)),
            StatusKind::Bad => Err(TaskError::UnexpectedBadResponse(status_body)),
        }
    }
}