use std::fmt;
use crate::{envelope::Envelope, folder::sync::hunk::FolderName, sync::SyncDestination};
pub type Id = String;
pub type RefreshSourceCache = bool;
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum EmailSyncHunk {
GetThenCache(FolderName, Id, SyncDestination),
CopyThenCache(
FolderName,
Envelope,
SyncDestination,
SyncDestination,
RefreshSourceCache,
),
UpdateCachedFlags(FolderName, Envelope, SyncDestination),
UpdateFlags(FolderName, Envelope, SyncDestination),
Uncache(FolderName, Id, SyncDestination),
Delete(FolderName, Id, SyncDestination),
}
impl fmt::Display for EmailSyncHunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::GetThenCache(folder, id, source) => {
write!(f, "Adding envelope {id} to {source} cache ({folder})")
}
Self::CopyThenCache(folder, envelope, source, target, _) => {
let id = &envelope.id;
write!(
f,
"Copying {source} envelope {id} to {target} folder {folder}"
)
}
Self::UpdateCachedFlags(folder, envelope, target) => {
let id = &envelope.id;
let flags = envelope.flags.to_string();
write!(
f,
"Updating flags {flags} of {target} cached envelope {id} ({folder})"
)
}
Self::UpdateFlags(folder, envelope, target) => {
let id = &envelope.id;
let flags = envelope.flags.to_string();
write!(
f,
"Setting flags {flags} of {target} envelope {id} ({folder})"
)
}
Self::Uncache(folder, id, target) => {
write!(f, "Removing envelope {id} from {target} cache ({folder})")
}
Self::Delete(folder, id, target) => {
write!(f, "Deleting {target} email {id} ({folder})")
}
}
}
}
impl EmailSyncHunk {
pub fn folder(&self) -> &str {
match self {
Self::GetThenCache(folder, _, _) => folder.as_str(),
Self::CopyThenCache(folder, _, _, _, _) => folder.as_str(),
Self::UpdateCachedFlags(folder, _, _) => folder.as_str(),
Self::UpdateFlags(folder, _, _) => folder.as_str(),
Self::Uncache(folder, _, _) => folder.as_str(),
Self::Delete(folder, _, _) => folder.as_str(),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum EmailSyncCacheHunk {
Insert(FolderName, Envelope, SyncDestination),
Delete(FolderName, Id, SyncDestination),
}