Skip to main content

imap_client/tasks/tasks/
appenduid.rs

1use std::num::NonZeroU32;
2
3use imap_next::imap_types::{
4    command::CommandBody,
5    datetime::DateTime,
6    extensions::binary::LiteralOrLiteral8,
7    flag::Flag,
8    mailbox::Mailbox,
9    response::{Code, StatusBody, StatusKind},
10};
11
12use super::TaskError;
13use crate::tasks::Task;
14
15#[derive(Clone, Debug)]
16pub struct AppendUidTask {
17    mailbox: Mailbox<'static>,
18    flags: Vec<Flag<'static>>,
19    date: Option<DateTime>,
20    message: LiteralOrLiteral8<'static>,
21}
22
23impl AppendUidTask {
24    pub fn new(mailbox: Mailbox<'static>, message: LiteralOrLiteral8<'static>) -> Self {
25        Self {
26            mailbox,
27            flags: Default::default(),
28            date: Default::default(),
29            message,
30        }
31    }
32
33    pub fn set_flags(&mut self, flags: Vec<Flag<'static>>) {
34        self.flags = flags;
35    }
36
37    pub fn add_flag(&mut self, flag: Flag<'static>) {
38        self.flags.push(flag);
39    }
40
41    pub fn with_flags(mut self, flags: Vec<Flag<'static>>) -> Self {
42        self.set_flags(flags);
43        self
44    }
45
46    pub fn with_flag(mut self, flag: Flag<'static>) -> Self {
47        self.add_flag(flag);
48        self
49    }
50
51    pub fn set_date(&mut self, date: DateTime) {
52        self.date = Some(date);
53    }
54
55    pub fn with_date(mut self, date: DateTime) -> Self {
56        self.set_date(date);
57        self
58    }
59}
60
61impl Task for AppendUidTask {
62    type Output = Result<Option<(NonZeroU32, NonZeroU32)>, TaskError>;
63
64    fn command_body(&self) -> CommandBody<'static> {
65        CommandBody::Append {
66            mailbox: self.mailbox.clone(),
67            flags: self.flags.clone(),
68            date: self.date.clone(),
69            message: self.message.clone(),
70        }
71    }
72
73    fn process_tagged(self, status_body: StatusBody<'static>) -> Self::Output {
74        match status_body.kind {
75            StatusKind::Ok => {
76                if let Some(Code::AppendUid { uid, uid_validity }) = status_body.code {
77                    Ok(Some((uid, uid_validity)))
78                } else {
79                    Ok(None)
80                }
81            }
82            StatusKind::No => Err(TaskError::UnexpectedNoResponse(status_body)),
83            StatusKind::Bad => Err(TaskError::UnexpectedBadResponse(status_body)),
84        }
85    }
86}