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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! # Folder module
//!
//! Module dedicated to folder (as known as mailbox) management.
//!
//! The main entities are [`FolderKind`], [`Folder`] and [`Folders`].
//!
//! The [`config`] module exposes all the folder configuration used by
//! the account configuration.
//!
//! Backend features reside in their own module as well: [`add`],
//! [`list`], [`expunge`], [`purge`], [`delete`].
//!
//! Finally, the [`sync`] module contains everything needed to
//! synchronize a remote folder with a local one.

#[cfg(feature = "folder-add")]
pub mod add;
pub mod config;
#[cfg(feature = "folder-delete")]
pub mod delete;
#[cfg(feature = "folder-expunge")]
pub mod expunge;
#[cfg(feature = "imap")]
pub mod imap;
#[cfg(feature = "folder-list")]
pub mod list;
#[cfg(feature = "maildir")]
pub mod maildir;
#[cfg(feature = "folder-purge")]
pub mod purge;
#[cfg(feature = "account-sync")]
pub mod sync;

use std::hash::Hash;
use std::{
    fmt,
    ops::{Deref, DerefMut},
    str::FromStr,
};
use thiserror::Error;

/// Errors dedicated to folder management.
#[derive(Debug, Error)]
pub enum Error {
    #[error("cannot parse folder kind {0}")]
    ParseFolderKindError(String),
}

pub const INBOX: &str = "INBOX";
pub const SENT: &str = "Sent";
pub const DRAFT: &str = "Drafts";
pub const DRAFTS: &str = "Drafts";
pub const TRASH: &str = "Trash";

/// The folder kind enumeration.
///
/// The folder kind is a category that gives a specific purpose to a
/// folder. It is used internally by the library to operate on the
/// right folder.
///
/// [`FolderConfig::aliases`](crate::folder::config::FolderConfig)
/// allows users to map custom folder names but also to map the
/// following folder kinds.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum FolderKind {
    /// The kind of folder that contains received emails.
    ///
    /// This folder kind is mostly used for listing new or recent
    /// emails.
    Inbox,

    /// The kind of folder that contains sent emails.
    ///
    /// This folder kind is used to store a copy of sent emails.
    Sent,

    /// The kind of folder than contains not finished emails.
    ///
    /// This kind of folder is used to store drafts. Emails in this
    /// folder are supposed to be edited. Once completed they should
    /// be removed from the folder.
    Drafts,

    /// The kind of folder that contains trashed emails.
    ///
    /// This kind of folder is used as a trash bin. Emails contained
    /// in this folder are supposed to be deleted.
    Trash,

    /// The user-defined kind of folder.
    ///
    /// This kind of folder represents the alias as defined by the
    /// user in [`config::FolderConfig`]::aliases.
    UserDefined(String),
}

impl FolderKind {
    /// Return `true` if the current folder kind matches the Inbox
    /// variant.
    pub fn is_inbox(&self) -> bool {
        matches!(self, FolderKind::Inbox)
    }

    /// Return `true` if the current folder kind matches the Sent
    /// variant.
    pub fn is_sent(&self) -> bool {
        matches!(self, FolderKind::Sent)
    }

    /// Return `true` if the current folder kind matches the Drafts
    /// variant.
    pub fn is_drafts(&self) -> bool {
        matches!(self, FolderKind::Drafts)
    }

    /// Return `true` if the current folder kind matches the Trash
    /// variant.
    pub fn is_trash(&self) -> bool {
        matches!(self, FolderKind::Trash)
    }

    /// Return `true` if the current folder kind matches the
    /// UserDefined variant.
    pub fn is_user_defined(&self) -> bool {
        matches!(self, FolderKind::UserDefined(_))
    }

    /// Return `true` if the give string matches the Inbox variant.
    pub fn matches_inbox(folder: impl AsRef<str>) -> bool {
        folder
            .as_ref()
            .parse::<FolderKind>()
            .map(|kind| kind.is_inbox())
            .unwrap_or_default()
    }

    /// Return `true` if the given string matches the Sent variant.
    pub fn matches_sent(folder: impl AsRef<str>) -> bool {
        folder
            .as_ref()
            .parse::<FolderKind>()
            .map(|kind| kind.is_sent())
            .unwrap_or_default()
    }

    /// Return `true` if the given string matches the Drafts variant.
    pub fn matches_drafts(folder: impl AsRef<str>) -> bool {
        folder
            .as_ref()
            .parse::<FolderKind>()
            .map(|kind| kind.is_drafts())
            .unwrap_or_default()
    }

    /// Return `true` if the given string matches the Trash variant.
    pub fn matches_trash(folder: impl AsRef<str>) -> bool {
        folder
            .as_ref()
            .parse::<FolderKind>()
            .map(|kind| kind.is_trash())
            .unwrap_or_default()
    }

    /// Return the folder kind as string slice.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Inbox => INBOX,
            Self::Sent => SENT,
            Self::Drafts => DRAFTS,
            Self::Trash => TRASH,
            Self::UserDefined(alias) => alias.as_str(),
        }
    }
}

impl FromStr for FolderKind {
    type Err = Error;

    fn from_str(kind: &str) -> Result<Self, Self::Err> {
        match kind {
            kind if kind.eq_ignore_ascii_case(INBOX) => Ok(Self::Inbox),
            kind if kind.eq_ignore_ascii_case(SENT) => Ok(Self::Sent),
            kind if kind.eq_ignore_ascii_case(DRAFT) => Ok(Self::Drafts),
            kind if kind.eq_ignore_ascii_case(DRAFTS) => Ok(Self::Drafts),
            kind if kind.eq_ignore_ascii_case(TRASH) => Ok(Self::Trash),
            kind => Err(Error::ParseFolderKindError(kind.to_owned()).into()),
        }
    }
}

impl<T: AsRef<str>> From<T> for FolderKind {
    fn from(kind: T) -> Self {
        kind.as_ref()
            .parse()
            .ok()
            .unwrap_or_else(|| Self::UserDefined(kind.as_ref().to_owned()))
    }
}

impl fmt::Display for FolderKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// The folder structure.
///
/// The folder is just a container for emails. Depending on the
/// backend used, the folder can be seen as a mailbox (IMAP/JMAP) or
/// as a system directory (Maildir).
#[derive(Clone, Debug, Default, Eq)]
pub struct Folder {
    /// The optional folder kind.
    pub kind: Option<FolderKind>,

    /// The folder name.
    pub name: String,

    /// The folder description.
    ///
    /// The description depends on the backend used: it can be IMAP
    /// attributes or Maildir path.
    pub desc: String,
}

impl Folder {
    /// Return `true` if the folder kind matches the Inbox variant.
    pub fn is_inbox(&self) -> bool {
        self.kind
            .as_ref()
            .map(|kind| kind.is_inbox())
            .unwrap_or_default()
    }

    /// Return `true` if the folder kind matches the Sent variant.
    pub fn is_sent(&self) -> bool {
        self.kind
            .as_ref()
            .map(|kind| kind.is_sent())
            .unwrap_or_default()
    }

    /// Return `true` if the folder kind matches the Drafts variant.
    pub fn is_drafts(&self) -> bool {
        self.kind
            .as_ref()
            .map(|kind| kind.is_drafts())
            .unwrap_or_default()
    }

    /// Return `true` if the folder kind matches the Trash variant.
    pub fn is_trash(&self) -> bool {
        self.kind
            .as_ref()
            .map(|kind| kind.is_trash())
            .unwrap_or_default()
    }

    /// Return the folder kind as string slice if existing, otherwise
    /// return the folder name as string slice.
    pub fn get_kind_or_name(&self) -> &str {
        self.kind
            .as_ref()
            .map(FolderKind::as_str)
            .unwrap_or(self.name.as_str())
    }
}

impl PartialEq for Folder {
    fn eq(&self, other: &Self) -> bool {
        match (&self.kind, &other.kind) {
            (Some(self_kind), Some(other_kind)) => self_kind == other_kind,
            (None, None) => self.name == other.name,
            _ => false,
        }
        // self.kind == other.kind || self.name == other.name
    }
}
impl Hash for Folder {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match &self.kind {
            Some(kind) => kind.hash(state),
            None => self.name.hash(state),
        }
    }
}

impl fmt::Display for Folder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.kind {
            Some(kind) => write!(f, "{kind}"),
            None => write!(f, "{}", self.name),
        }
    }
}

/// The list of folders.
///
/// This structure is just a convenient wrapper used to implement
/// custom mappers for backends.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Folders(Vec<Folder>);

impl Deref for Folders {
    type Target = Vec<Folder>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Folders {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl FromIterator<Folder> for Folders {
    fn from_iter<T: IntoIterator<Item = Folder>>(iter: T) -> Self {
        let mut folders = Folders::default();
        folders.extend(iter);
        folders
    }
}

impl From<Folders> for Vec<Folder> {
    fn from(val: Folders) -> Self {
        val.0
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::hash_map::DefaultHasher, hash::Hasher};

    use super::*;
    fn folder_inbox_foo() -> Folder {
        Folder {
            kind: Some(FolderKind::Inbox),
            name: "foo".to_owned(),
            desc: "1".to_owned(),
        }
    }
    fn folder_none_foo() -> Folder {
        Folder {
            kind: None,
            name: "foo".to_owned(),
            desc: "2".to_owned(),
        }
    }
    fn folder_none_bar() -> Folder {
        Folder {
            kind: None,
            name: "bar".to_owned(),
            desc: "3".to_owned(),
        }
    }
    fn folder_inbox_bar() -> Folder {
        Folder {
            kind: Some(FolderKind::Inbox),
            name: "bar".to_owned(),
            desc: "4".to_owned(),
        }
    }

    fn hash<H: Hash>(item: H) -> u64 {
        let mut hasher = DefaultHasher::new();
        item.hash(&mut hasher);
        hasher.finish()
    }

    #[test]
    fn folder_inbox_bar_equals_inbox_foo_test() {
        assert_eq!(folder_inbox_bar(), folder_inbox_foo());
    }

    #[test]
    fn folder_inbox_bar_equals_inbox_foo_test_hash() {
        assert_eq!(hash(folder_inbox_bar()), hash(folder_inbox_foo()));
    }

    #[test]
    fn folder_none_foo_not_equals_inbox_foo_test() {
        assert_ne!(folder_none_foo(), folder_inbox_foo());
    }

    #[test]
    fn folder_none_foo_not_equals_inbox_foo_test_hash() {
        assert_ne!(hash(folder_none_foo()), hash(folder_inbox_foo()));
    }

    #[test]
    fn folder_none_foo_not_equals_none_bar_test() {
        assert_ne!(folder_none_foo(), folder_none_bar());
    }

    #[test]
    fn folder_none_foo_not_equals_none_bar_test_hash() {
        assert_ne!(hash(folder_none_foo()), hash(folder_none_bar()));
    }
}