diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-03-20 17:31:54 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-03-20 17:31:54 +0100 |
commit | ed47855ef1a6c9d10d48080367ff8b280530e362 (patch) | |
tree | fc79b171718598c84034e212a6b8746a5fb6b782 /aero-collections/src/mail | |
parent | 22e4f295556fdd4c25cf43983a56ff74acab7739 (diff) | |
download | aerogramme-ed47855ef1a6c9d10d48080367ff8b280530e362.tar.gz aerogramme-ed47855ef1a6c9d10d48080367ff8b280530e362.zip |
Share UniqueIdent between collections
Diffstat (limited to 'aero-collections/src/mail')
-rw-r--r-- | aero-collections/src/mail/incoming.rs | 2 | ||||
-rw-r--r-- | aero-collections/src/mail/mailbox.rs | 2 | ||||
-rw-r--r-- | aero-collections/src/mail/mod.rs | 1 | ||||
-rw-r--r-- | aero-collections/src/mail/namespace.rs | 2 | ||||
-rw-r--r-- | aero-collections/src/mail/query.rs | 2 | ||||
-rw-r--r-- | aero-collections/src/mail/snapshot.rs | 2 | ||||
-rw-r--r-- | aero-collections/src/mail/uidindex.rs | 2 | ||||
-rw-r--r-- | aero-collections/src/mail/unique_ident.rs | 101 |
8 files changed, 6 insertions, 108 deletions
diff --git a/aero-collections/src/mail/incoming.rs b/aero-collections/src/mail/incoming.rs index 8220461..cd2f8fd 100644 --- a/aero-collections/src/mail/incoming.rs +++ b/aero-collections/src/mail/incoming.rs @@ -15,7 +15,7 @@ use aero_bayou::timestamp::now_msec; use crate::mail::mailbox::Mailbox; use crate::mail::uidindex::ImapUidvalidity; -use crate::mail::unique_ident::*; +use crate::unique_ident::*; use crate::user::User; use crate::mail::IMF; diff --git a/aero-collections/src/mail/mailbox.rs b/aero-collections/src/mail/mailbox.rs index a767678..25aacf5 100644 --- a/aero-collections/src/mail/mailbox.rs +++ b/aero-collections/src/mail/mailbox.rs @@ -9,7 +9,7 @@ use aero_bayou::Bayou; use aero_bayou::timestamp::now_msec; use crate::mail::uidindex::*; -use crate::mail::unique_ident::*; +use crate::unique_ident::*; use crate::mail::IMF; pub struct Mailbox { diff --git a/aero-collections/src/mail/mod.rs b/aero-collections/src/mail/mod.rs index 85361f3..ca9b08b 100644 --- a/aero-collections/src/mail/mod.rs +++ b/aero-collections/src/mail/mod.rs @@ -3,7 +3,6 @@ pub mod mailbox; pub mod query; pub mod snapshot; pub mod uidindex; -pub mod unique_ident; pub mod namespace; // Internet Message Format diff --git a/aero-collections/src/mail/namespace.rs b/aero-collections/src/mail/namespace.rs index 452ac68..b1f6a70 100644 --- a/aero-collections/src/mail/namespace.rs +++ b/aero-collections/src/mail/namespace.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use aero_bayou::timestamp::now_msec; use crate::mail::uidindex::ImapUidvalidity; -use crate::mail::unique_ident::{gen_ident, UniqueIdent}; +use crate::unique_ident::{gen_ident, UniqueIdent}; pub const MAILBOX_HIERARCHY_DELIMITER: char = '.'; diff --git a/aero-collections/src/mail/query.rs b/aero-collections/src/mail/query.rs index 3e6fe99..7faba41 100644 --- a/aero-collections/src/mail/query.rs +++ b/aero-collections/src/mail/query.rs @@ -1,6 +1,6 @@ use super::mailbox::MailMeta; use super::snapshot::FrozenMailbox; -use super::unique_ident::UniqueIdent; +use crate::unique_ident::UniqueIdent; use anyhow::Result; use futures::future::FutureExt; use futures::stream::{BoxStream, Stream, StreamExt}; diff --git a/aero-collections/src/mail/snapshot.rs b/aero-collections/src/mail/snapshot.rs index ed756b5..9503d4d 100644 --- a/aero-collections/src/mail/snapshot.rs +++ b/aero-collections/src/mail/snapshot.rs @@ -2,10 +2,10 @@ use std::sync::Arc; use anyhow::Result; +use crate::unique_ident::UniqueIdent; use super::mailbox::Mailbox; use super::query::{Query, QueryScope}; use super::uidindex::UidIndex; -use super::unique_ident::UniqueIdent; /// A Frozen Mailbox has a snapshot of the current mailbox /// state that is desynchronized with the real mailbox state. diff --git a/aero-collections/src/mail/uidindex.rs b/aero-collections/src/mail/uidindex.rs index 637a1ac..ca975a3 100644 --- a/aero-collections/src/mail/uidindex.rs +++ b/aero-collections/src/mail/uidindex.rs @@ -4,7 +4,7 @@ use im::{HashMap, OrdMap, OrdSet}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use aero_bayou::*; -use crate::mail::unique_ident::UniqueIdent; +use crate::unique_ident::UniqueIdent; pub type ModSeq = NonZeroU64; pub type ImapUid = NonZeroU32; diff --git a/aero-collections/src/mail/unique_ident.rs b/aero-collections/src/mail/unique_ident.rs deleted file mode 100644 index 0987a2c..0000000 --- a/aero-collections/src/mail/unique_ident.rs +++ /dev/null @@ -1,101 +0,0 @@ -use std::str::FromStr; -use std::sync::atomic::{AtomicU64, Ordering}; - -use lazy_static::lazy_static; -use rand::prelude::*; -use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; - -use aero_bayou::timestamp::now_msec; - -/// An internal Mail Identifier is composed of two components: -/// - a process identifier, 128 bits, itself composed of: -/// - the timestamp of when the process started, 64 bits -/// - a 64-bit random number -/// - a sequence number, 64 bits -/// They are not part of the protocol but an internal representation -/// required by Aerogramme. -/// Their main property is to be unique without having to rely -/// on synchronization between IMAP processes. -#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)] -pub struct UniqueIdent(pub [u8; 24]); - -struct IdentGenerator { - pid: u128, - sn: AtomicU64, -} - -impl IdentGenerator { - fn new() -> Self { - let time = now_msec() as u128; - let rand = thread_rng().gen::<u64>() as u128; - Self { - pid: (time << 64) | rand, - sn: AtomicU64::new(0), - } - } - - fn gen(&self) -> UniqueIdent { - let sn = self.sn.fetch_add(1, Ordering::Relaxed); - let mut res = [0u8; 24]; - res[0..16].copy_from_slice(&u128::to_be_bytes(self.pid)); - res[16..24].copy_from_slice(&u64::to_be_bytes(sn)); - UniqueIdent(res) - } -} - -lazy_static! { - static ref GENERATOR: IdentGenerator = IdentGenerator::new(); -} - -pub fn gen_ident() -> UniqueIdent { - GENERATOR.gen() -} - -// -- serde -- - -impl<'de> Deserialize<'de> for UniqueIdent { - fn deserialize<D>(d: D) -> Result<Self, D::Error> - where - D: Deserializer<'de>, - { - let v = String::deserialize(d)?; - UniqueIdent::from_str(&v).map_err(D::Error::custom) - } -} - -impl Serialize for UniqueIdent { - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> - where - S: Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl std::fmt::Display for UniqueIdent { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", hex::encode(self.0)) - } -} - -impl std::fmt::Debug for UniqueIdent { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", hex::encode(self.0)) - } -} - -impl FromStr for UniqueIdent { - type Err = &'static str; - - fn from_str(s: &str) -> Result<UniqueIdent, &'static str> { - let bytes = hex::decode(s).map_err(|_| "invalid hex")?; - - if bytes.len() != 24 { - return Err("bad length"); - } - - let mut tmp = [0u8; 24]; - tmp[..].copy_from_slice(&bytes); - Ok(UniqueIdent(tmp)) - } -} |