imap_client/tasks/tasks/
sort.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
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
use std::num::NonZeroU32;

use imap_next::imap_types::{
    command::CommandBody,
    core::{Charset, Vec1},
    extensions::sort::SortCriterion,
    response::{Data, StatusBody, StatusKind},
    search::SearchKey,
};
use tracing::warn;

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

#[derive(Clone, Debug)]
pub struct SortTask {
    sort_criteria: Vec1<SortCriterion>,
    charset: Charset<'static>,
    search_criteria: Vec1<SearchKey<'static>>,
    uid: bool,
    output: Option<Vec<NonZeroU32>>,
}

impl SortTask {
    pub fn new(
        sort_criteria: Vec1<SortCriterion>,
        search_criteria: Vec1<SearchKey<'static>>,
    ) -> Self {
        Self {
            sort_criteria,
            charset: Charset::try_from("UTF-8").unwrap(),
            search_criteria,
            uid: true,
            output: Default::default(),
        }
    }

    pub fn set_charset(&mut self, charset: Charset<'static>) {
        self.charset = charset;
    }

    pub fn with_charset(mut self, charset: Charset<'static>) -> Self {
        self.set_charset(charset);
        self
    }

    pub fn set_uid(&mut self, uid: bool) {
        self.uid = uid;
    }

    pub fn with_uid(mut self, uid: bool) -> Self {
        self.set_uid(uid);
        self
    }
}

impl Task for SortTask {
    type Output = Result<Vec<NonZeroU32>, TaskError>;

    fn command_body(&self) -> CommandBody<'static> {
        CommandBody::Sort {
            sort_criteria: self.sort_criteria.clone(),
            charset: self.charset.clone(),
            search_criteria: self.search_criteria.clone(),
            uid: self.uid,
        }
    }

    fn process_data(&mut self, data: Data<'static>) -> Option<Data<'static>> {
        if let Data::Sort(ids) = data {
            if self.output.is_some() {
                warn!("received duplicate sort data");
            }
            self.output = Some(ids);
            None
        } else {
            Some(data)
        }
    }

    fn process_tagged(self, status_body: StatusBody<'static>) -> Self::Output {
        match status_body.kind {
            StatusKind::Ok => match self.output {
                Some(output) => Ok(output),
                None => Err(TaskError::MissingData("SORT".into())),
            },
            StatusKind::No => Err(TaskError::UnexpectedNoResponse(status_body)),
            StatusKind::Bad => Err(TaskError::UnexpectedBadResponse(status_body)),
        }
    }
}