aboutsummaryrefslogtreecommitdiff
path: root/src/mail_ident.rs
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2022-06-27 16:56:20 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2022-06-27 16:56:20 +0200
commit390bad0ec451a571e119903054b581a9d9a00cbe (patch)
tree467fdf3ade0325b8c69ea8350a2f15fa181d8981 /src/mail_ident.rs
parentd3f8a6627ca13a020bac9936d1f40a18239b6d6d (diff)
downloadaerogramme-390bad0ec451a571e119903054b581a9d9a00cbe.tar.gz
aerogramme-390bad0ec451a571e119903054b581a9d9a00cbe.zip
Refactor files in a "mail" crate
Diffstat (limited to 'src/mail_ident.rs')
-rw-r--r--src/mail_ident.rs95
1 files changed, 0 insertions, 95 deletions
diff --git a/src/mail_ident.rs b/src/mail_ident.rs
deleted file mode 100644
index 07e053a..0000000
--- a/src/mail_ident.rs
+++ /dev/null
@@ -1,95 +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 crate::time::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 Mailrage/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, Debug)]
-pub struct MailIdent(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) -> MailIdent {
- 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));
- MailIdent(res)
- }
-}
-
-lazy_static! {
- static ref GENERATOR: IdentGenerator = IdentGenerator::new();
-}
-
-pub fn gen_ident() -> MailIdent {
- GENERATOR.gen()
-}
-
-// -- serde --
-
-impl<'de> Deserialize<'de> for MailIdent {
- fn deserialize<D>(d: D) -> Result<Self, D::Error>
- where
- D: Deserializer<'de>,
- {
- let v = String::deserialize(d)?;
- MailIdent::from_str(&v).map_err(D::Error::custom)
- }
-}
-
-impl Serialize for MailIdent {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- serializer.serialize_str(&self.to_string())
- }
-}
-
-impl ToString for MailIdent {
- fn to_string(&self) -> String {
- hex::encode(self.0)
- }
-}
-
-impl FromStr for MailIdent {
- type Err = &'static str;
-
- fn from_str(s: &str) -> Result<MailIdent, &'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(MailIdent(tmp))
- }
-}