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
//! Module dedicated to email folders synchronization hunk.
//!
//! The core structure of the module is the [`FolderSyncHunk`], which
//! represents a change in a patch.

use std::fmt;

use crate::account::sync::Target;

use super::*;

/// Alias for the folder name.
pub type FolderName = String;

/// Alias for the unique set of folder names.
pub type FoldersName = HashSet<FolderName>;

/// The folder synchronization hunk.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FolderSyncHunk {
    /// The given folder name needs to be created to the given
    /// destination.
    Create(FolderName, Target),

    /// The given folder name needs to be added to the cache for the
    /// given destination.
    Cache(FolderName, Target),

    /// The given folder needs to be deleted from the given
    /// destination.
    Delete(FolderName, Target),

    /// The given folder needs to be removed from the cache for the
    /// given destination.
    Uncache(FolderName, Target),
}

impl fmt::Display for FolderSyncHunk {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Create(folder, target) => write!(f, "Creating {target} folder {folder}"),
            Self::Cache(folder, target) => {
                write!(f, "Adding {target} folder {folder} to cache")
            }
            Self::Delete(folder, target) => write!(f, "Deleting {target} folder {folder}"),
            Self::Uncache(folder, target) => {
                write!(f, "Removing {target} folder {folder} from cache")
            }
        }
    }
}

impl FolderSyncHunk {
    pub fn folder(&self) -> &str {
        match self {
            Self::Create(folder, _) => folder.as_str(),
            Self::Cache(folder, _) => folder.as_str(),
            Self::Delete(folder, _) => folder.as_str(),
            Self::Uncache(folder, _) => folder.as_str(),
        }
    }
}

/// The folder synchronization cache hunk.
///
/// Similar to the [`FolderSyncHunk`], except that this hunk is
/// specific to the cache (SQLite).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FolderSyncCacheHunk {
    /// The given folder name needs to be added to the cache for the
    /// given destination.
    Insert(FolderName, Target),

    /// The given folder name needs to be removed from the cache for
    /// the given destination.
    Delete(FolderName, Target),
}