Alias Flag to String

This commit is contained in:
Quentin 2022-06-14 10:35:12 +02:00
parent 9c95b261e0
commit 13ed1b3890
Signed by: quentin
GPG key ID: E9602264D639FF68

View file

@ -5,6 +5,7 @@ use crate::bayou::*;
pub type ImapUid = u32; pub type ImapUid = u32;
pub type ImapUidvalidity = u32; pub type ImapUidvalidity = u32;
pub type Flag = String;
/// A Mail UUID is composed of two components: /// A Mail UUID is composed of two components:
/// - a process identifier, 128 bits /// - a process identifier, 128 bits
@ -20,9 +21,9 @@ pub struct MailUuid(pub [u8; 24]);
/// that are optimized for cloning (they clone underlying values only if they are modified) /// that are optimized for cloning (they clone underlying values only if they are modified)
pub struct UidIndex { pub struct UidIndex {
pub mail_uid: OrdMap<MailUuid, ImapUid>, pub mail_uid: OrdMap<MailUuid, ImapUid>,
pub mail_flags: OrdMap<MailUuid, Vec<String>>, pub mail_flags: OrdMap<MailUuid, Vec<Flag>>,
pub mails_by_uid: OrdMap<ImapUid, MailUuid>, pub mails_by_uid: OrdMap<ImapUid, MailUuid>,
pub flags: HashMap<String, HashSet<MailUuid>>, pub flags: HashMap<Flag, HashSet<MailUuid>>,
pub uidvalidity: ImapUidvalidity, pub uidvalidity: ImapUidvalidity,
pub uidnext: ImapUid, pub uidnext: ImapUid,
@ -31,15 +32,15 @@ pub struct UidIndex {
#[derive(Clone, Serialize, Deserialize, Debug)] #[derive(Clone, Serialize, Deserialize, Debug)]
pub enum UidIndexOp { pub enum UidIndexOp {
MailAdd(MailUuid, ImapUid, Vec<String>), MailAdd(MailUuid, ImapUid, Vec<Flag>),
MailDel(MailUuid), MailDel(MailUuid),
FlagAdd(MailUuid, Vec<String>), FlagAdd(MailUuid, Vec<Flag>),
FlagDel(MailUuid, Vec<String>), FlagDel(MailUuid, Vec<Flag>),
} }
impl UidIndex { impl UidIndex {
#[must_use] #[must_use]
pub fn op_mail_add(&self, uuid: MailUuid, flags: Vec<String>) -> UidIndexOp { pub fn op_mail_add(&self, uuid: MailUuid, flags: Vec<Flag>) -> UidIndexOp {
UidIndexOp::MailAdd(uuid, self.internalseq, flags) UidIndexOp::MailAdd(uuid, self.internalseq, flags)
} }
@ -49,12 +50,12 @@ impl UidIndex {
} }
#[must_use] #[must_use]
pub fn op_flag_add(&self, uuid: MailUuid, flags: Vec<String>) -> UidIndexOp { pub fn op_flag_add(&self, uuid: MailUuid, flags: Vec<Flag>) -> UidIndexOp {
UidIndexOp::FlagAdd(uuid, flags) UidIndexOp::FlagAdd(uuid, flags)
} }
#[must_use] #[must_use]
pub fn op_flag_del(&self, uuid: MailUuid, flags: Vec<String>) -> UidIndexOp { pub fn op_flag_del(&self, uuid: MailUuid, flags: Vec<Flag>) -> UidIndexOp {
UidIndexOp::FlagDel(uuid, flags) UidIndexOp::FlagDel(uuid, flags)
} }
} }
@ -122,9 +123,17 @@ impl BayouState for UidIndex {
}); });
} }
UidIndexOp::FlagDel(uuid, rm_flags) => { UidIndexOp::FlagDel(uuid, rm_flags) => {
// Upate mapping Email -> Flag
if let Some(mail_flags) = new.mail_flags.get_mut(uuid) { if let Some(mail_flags) = new.mail_flags.get_mut(uuid) {
mail_flags.retain(|x| !rm_flags.contains(x)); mail_flags.retain(|x| !rm_flags.contains(x));
} }
// Update mapping Flag -> Email
rm_flags.iter().for_each(|flag| {
new.flags
.entry(flag.clone())
.and_modify(|hs| { hs.remove(uuid); });
});
} }
} }
new new
@ -135,7 +144,7 @@ impl BayouState for UidIndex {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct UidIndexSerializedRepr { struct UidIndexSerializedRepr {
mails: Vec<(ImapUid, MailUuid, Vec<String>)>, mails: Vec<(ImapUid, MailUuid, Vec<Flag>)>,
uidvalidity: ImapUidvalidity, uidvalidity: ImapUidvalidity,
uidnext: ImapUid, uidnext: ImapUid,
internalseq: ImapUid, internalseq: ImapUid,