aboutsummaryrefslogtreecommitdiff
path: root/src/imap/response.rs
blob: 22e91f34da8cacca8c7a04cbe88b217b6068edd3 (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
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, StatusKind};

pub struct ResponseBuilder {
    status: StatusKind,
    tag: Option<Tag<'static>>,
    code: Option<Code<'static>>,
    text: String,
    data: Vec<Data<'static>>,
}

impl<'a> Default for ResponseBuilder {
    fn default() -> ResponseBuilder {
        ResponseBuilder {
            status: StatusKind::Bad,
            tag: None,
            code: None,
            text: "".to_string(),
            data: vec![],
        }
    }
}

impl ResponseBuilder {
    pub fn to_req(mut self, cmd: &Command) -> Self {
        self.tag = Some(cmd.tag);
        self
    }
    pub fn tag(mut self, tag: Tag) -> 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) -> Self {
        self.code = Some(code);
        self
    }

    pub fn data(mut self, data: Data) -> Self {
        self.data.push(data);
        self
    }

    pub fn set_data(mut self, data: Vec<Data>) -> Self {
        self.data = data;
        self
    }

    pub fn build(self) -> Result<Response> {
        Ok(Response {
            status: Status::new(self.tag, self.status, self.code, self.text)?,
            data: self.data,
        })
    }
}

pub struct Response {
    data: Vec<Data<'static>>,
    status: Status<'static>,
}

impl Response {
    pub fn ok() -> ResponseBuilder {
        ResponseBuilder {
            status: StatusKind::Ok,
            ..ResponseBuilder::default()
        }
    }

    pub fn no() -> ResponseBuilder {
        ResponseBuilder {
            status: StatusKind::No,
            ..ResponseBuilder::default()
        }
    }

    pub fn bad() -> ResponseBuilder {
        ResponseBuilder {
            status: StatusKind::Bad,
            ..ResponseBuilder::default()
        }
    }

    pub fn bye() -> Result<Response> {
        Ok(Response {
            status: Status::bye(None, "bye")?,
            data: vec![],
        })
    }
}