Skip to main content

imap_client/tasks/tasks/
expunge.rs

1use std::num::NonZeroU32;
2
3use imap_next::imap_types::{
4    command::CommandBody,
5    response::{Data, StatusBody, StatusKind},
6};
7
8use super::TaskError;
9use crate::tasks::Task;
10
11/// Permanently removes messages containing the Deleted flag in their
12/// envelope.
13///
14/// Be aware that the returned vector can contain multiple time the
15/// same sequence number, depending on the server implementation
16/// style:
17///
18/// > For example, if the last 5 messages in a 9-message mailbox are
19/// expunged, a "lower to higher" server will send five untagged
20/// EXPUNGE responses for message sequence number 5, whereas a "higher
21/// to lower server" will send successive untagged EXPUNGE responses
22/// for message sequence numbers 9, 8, 7, 6, and 5
23#[derive(Clone, Debug, Default)]
24pub struct ExpungeTask {
25    output: Vec<NonZeroU32>,
26}
27
28impl ExpungeTask {
29    pub fn new() -> Self {
30        Default::default()
31    }
32}
33
34impl Task for ExpungeTask {
35    type Output = Result<Vec<NonZeroU32>, TaskError>;
36
37    fn command_body(&self) -> CommandBody<'static> {
38        CommandBody::Expunge
39    }
40
41    fn process_data(&mut self, data: Data<'static>) -> Option<Data<'static>> {
42        if let Data::Expunge(seq) = data {
43            self.output.push(seq);
44            None
45        } else {
46            Some(data)
47        }
48    }
49
50    fn process_tagged(self, status_body: StatusBody<'static>) -> Self::Output {
51        match status_body.kind {
52            StatusKind::Ok => Ok(self.output),
53            StatusKind::No => Err(TaskError::UnexpectedNoResponse(status_body)),
54            StatusKind::Bad => Err(TaskError::UnexpectedBadResponse(status_body)),
55        }
56    }
57}