aboutsummaryrefslogtreecommitdiff
path: root/src/imap/response.rs
blob: 7b7f92ddc720035ab1c8ad3a64e689ab82adfbdc (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use anyhow::Result;
use imap_codec::imap_types::command::Command;
use imap_codec::imap_types::core::Tag;
use imap_codec::imap_types::response::{Code, Data, Status};

#[derive(Debug)]
pub enum Body<'a> {
    Data(Data<'a>),
    Status(Status<'a>),
}

pub struct ResponseBuilder<'a> {
    tag: Option<Tag<'a>>,
    code: Option<Code<'a>>,
    text: String,
    body: Vec<Body<'a>>,
}

impl<'a> ResponseBuilder<'a> {
    pub fn to_req(mut self, cmd: &Command<'a>) -> Self {
        self.tag = Some(cmd.tag.clone());
        self
    }
    pub fn tag(mut self, tag: Tag<'a>) -> Self {
        self.tag = Some(tag);
        self
    }

    pub fn message(mut self, txt: impl Into<String>) -> Self {
        self.text = txt.into();
        self
    }

    pub fn code(mut self, code: Code<'a>) -> Self {
        self.code = Some(code);
        self
    }

    pub fn data(mut self, data: Data<'a>) -> Self {
        self.body.push(Body::Data(data));
        self
    }

    pub fn many_data(mut self, data: Vec<Data<'a>>) -> Self {
        for d in data.into_iter() {
            self = self.data(d);
        }
        self
    }

    #[allow(dead_code)]
    pub fn info(mut self, status: Status<'a>) -> Self {
        self.body.push(Body::Status(status));
        self
    }

    #[allow(dead_code)]
    pub fn many_info(mut self, status: Vec<Status<'a>>) -> Self {
        for d in status.into_iter() {
            self = self.info(d);
        }
        self
    }

    pub fn set_body(mut self, body: Vec<Body<'a>>) -> Self {
        self.body = body;
        self
    }

    pub fn ok(self) -> Result<Response<'a>> {
        Ok(Response {
            completion: Status::ok(self.tag, self.code, self.text)?,
            body: self.body,
        })
    }

    pub fn no(self) -> Result<Response<'a>> {
        Ok(Response {
            completion: Status::no(self.tag, self.code, self.text)?,
            body: self.body,
        })
    }

    pub fn bad(self) -> Result<Response<'a>> {
        Ok(Response {
            completion: Status::bad(self.tag, self.code, self.text)?,
            body: self.body,
        })
    }
}

#[derive(Debug)]
pub struct Response<'a> {
    pub body: Vec<Body<'a>>,
    pub completion: Status<'a>,
}

impl<'a> Response<'a> {
    pub fn build() -> ResponseBuilder<'a> {
        ResponseBuilder {
            tag: None,
            code: None,
            text: "".to_string(),
            body: vec![],
        }
    }

    pub fn bye() -> Result<Response<'a>> {
        Ok(Response {
            completion: Status::bye(None, "bye")?,
            body: vec![],
        })
    }
}

#[derive(Debug)]
pub enum ResponseOrIdle {
    Response(Response<'static>),
    StartIdle,
    IdleEvent(Vec<Body<'static>>),
}