2022-05-31 22:02:27 +00:00
|
|
|
use std::str::FromStr;
|
2022-05-31 22:06:26 +00:00
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
2022-05-31 13:49:10 +00:00
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use rand::prelude::*;
|
|
|
|
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
|
2023-11-15 14:56:43 +00:00
|
|
|
use crate::timestamp::now_msec;
|
2022-05-31 13:49:10 +00:00
|
|
|
|
2022-06-15 16:40:39 +00:00
|
|
|
/// An internal Mail Identifier is composed of two components:
|
2022-05-31 13:49:10 +00:00
|
|
|
/// - 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
|
2022-06-15 16:40:39 +00:00
|
|
|
/// They are not part of the protocol but an internal representation
|
2022-06-30 08:50:50 +00:00
|
|
|
/// required by Aerogramme.
|
2022-06-15 16:40:39 +00:00
|
|
|
/// Their main property is to be unique without having to rely
|
|
|
|
/// on synchronization between IMAP processes.
|
2022-07-12 13:59:13 +00:00
|
|
|
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
2022-06-29 13:52:09 +00:00
|
|
|
pub struct UniqueIdent(pub [u8; 24]);
|
2022-06-15 16:40:39 +00:00
|
|
|
|
|
|
|
struct IdentGenerator {
|
2022-05-31 13:49:10 +00:00
|
|
|
pid: u128,
|
|
|
|
sn: AtomicU64,
|
|
|
|
}
|
|
|
|
|
2022-06-15 16:40:39 +00:00
|
|
|
impl IdentGenerator {
|
2022-05-31 13:49:10 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
fn gen(&self) -> UniqueIdent {
|
2022-05-31 13:49:10 +00:00
|
|
|
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));
|
2022-06-29 13:52:09 +00:00
|
|
|
UniqueIdent(res)
|
2022-05-31 13:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
2022-06-15 16:40:39 +00:00
|
|
|
static ref GENERATOR: IdentGenerator = IdentGenerator::new();
|
2022-05-31 13:49:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
pub fn gen_ident() -> UniqueIdent {
|
2022-05-31 13:49:10 +00:00
|
|
|
GENERATOR.gen()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- serde --
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
impl<'de> Deserialize<'de> for UniqueIdent {
|
2022-05-31 13:49:10 +00:00
|
|
|
fn deserialize<D>(d: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let v = String::deserialize(d)?;
|
2022-06-29 13:52:09 +00:00
|
|
|
UniqueIdent::from_str(&v).map_err(D::Error::custom)
|
2022-05-31 13:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
impl Serialize for UniqueIdent {
|
2022-05-31 13:49:10 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2022-05-31 15:07:34 +00:00
|
|
|
serializer.serialize_str(&self.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
impl std::fmt::Display for UniqueIdent {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", hex::encode(self.0))
|
2022-05-31 13:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-31 22:02:27 +00:00
|
|
|
|
2022-07-12 13:59:13 +00:00
|
|
|
impl std::fmt::Debug for UniqueIdent {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", hex::encode(self.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
impl FromStr for UniqueIdent {
|
2022-05-31 22:02:27 +00:00
|
|
|
type Err = &'static str;
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
fn from_str(s: &str) -> Result<UniqueIdent, &'static str> {
|
2022-05-31 22:02:27 +00:00
|
|
|
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);
|
2022-06-29 13:52:09 +00:00
|
|
|
Ok(UniqueIdent(tmp))
|
2022-05-31 22:02:27 +00:00
|
|
|
}
|
|
|
|
}
|