tetratto-shared 12.0.6

Shared stuff for Tetratto
Documentation
use std::time::{UNIX_EPOCH, Duration};
use serde::{Deserialize, Serialize};
use snowflaked::{Builder, Generator};

pub const EPOCH_2024: u64 = 1704067200000;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Snowflake(String);

impl Default for Snowflake {
    fn default() -> Self {
        Self::new()
    }
}

impl Snowflake {
    pub fn builder() -> Builder {
        Builder::new().epoch(UNIX_EPOCH + Duration::from_millis(EPOCH_2024))
    }

    /// Create a new [`Snowflake`]
    pub fn new() -> Self {
        Self(
            Self::builder()
                .build::<Generator>()
                .generate::<u64>()
                .to_string(),
        )
    }
}

impl std::fmt::Display for Snowflake {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}