email/folder/sync/
hunk.rs

1//! Module dedicated to email folders synchronization hunk.
2//!
3//! The core structure of the module is the [`FolderSyncHunk`], which
4//! represents a change in a patch.
5
6use std::fmt;
7
8use super::*;
9use crate::sync::SyncDestination;
10
11/// Alias for the folder name.
12pub type FolderName = String;
13
14/// Alias for the unique set of folder names.
15pub type FoldersName = HashSet<FolderName>;
16
17/// The folder synchronization hunk.
18#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
19pub enum FolderSyncHunk {
20    /// The given folder name needs to be created to the given
21    /// destination.
22    Create(FolderName, SyncDestination),
23
24    /// The given folder name needs to be added to the cache for the
25    /// given destination.
26    Cache(FolderName, SyncDestination),
27
28    /// The given folder needs to be deleted from the given
29    /// destination.
30    Delete(FolderName, SyncDestination),
31
32    /// The given folder needs to be removed from the cache for the
33    /// given destination.
34    Uncache(FolderName, SyncDestination),
35}
36
37impl FolderSyncHunk {
38    pub fn is_left(&self) -> bool {
39        match self {
40            Self::Create(_, SyncDestination::Left) => true,
41            Self::Cache(_, SyncDestination::Left) => true,
42            Self::Delete(_, SyncDestination::Left) => true,
43            Self::Uncache(_, SyncDestination::Left) => true,
44            _ => false,
45        }
46    }
47
48    pub fn is_right(&self) -> bool {
49        match self {
50            Self::Create(_, SyncDestination::Right) => true,
51            Self::Cache(_, SyncDestination::Right) => true,
52            Self::Delete(_, SyncDestination::Right) => true,
53            Self::Uncache(_, SyncDestination::Right) => true,
54            _ => false,
55        }
56    }
57
58    pub fn folder(&self) -> &str {
59        match self {
60            Self::Create(folder, _) => folder.as_str(),
61            Self::Cache(folder, _) => folder.as_str(),
62            Self::Delete(folder, _) => folder.as_str(),
63            Self::Uncache(folder, _) => folder.as_str(),
64        }
65    }
66}
67
68impl fmt::Display for FolderSyncHunk {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        match self {
71            Self::Create(folder, target) => write!(f, "Creating {target} folder {folder}"),
72            Self::Cache(folder, target) => {
73                write!(f, "Adding {target} folder {folder} to cache")
74            }
75            Self::Delete(folder, target) => write!(f, "Deleting {target} folder {folder}"),
76            Self::Uncache(folder, target) => {
77                write!(f, "Removing {target} folder {folder} from cache")
78            }
79        }
80    }
81}
82
83/// The folder synchronization cache hunk.
84///
85/// Similar to the [`FolderSyncHunk`], except that this hunk is
86/// specific to the cache (SQLite).
87#[derive(Clone, Debug, Eq, PartialEq)]
88pub enum FolderSyncCacheHunk {
89    /// The given folder name needs to be added to the cache for the
90    /// given destination.
91    Insert(FolderName, SyncDestination),
92
93    /// The given folder name needs to be removed from the cache for
94    /// the given destination.
95    Delete(FolderName, SyncDestination),
96}