add: icon resolver
add: config "no_track" file list option add: rainbeam-shared -> tetratto-shared add: l10n
This commit is contained in:
parent
b6fe2fba37
commit
d2ca9e23d3
40 changed files with 1107 additions and 583 deletions
52
crates/shared/src/snow.rs
Normal file
52
crates/shared/src/snow.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
//! Almost Snowflake
|
||||
//!
|
||||
//! Random IDs which include timestamp information (like Twitter Snowflakes)
|
||||
//!
|
||||
//! IDs are generated with 41 bits of an epoch timestamp, 10 bits of a machine/server ID, and 12 bits of randomly generated numbers.
|
||||
//!
|
||||
//! ```
|
||||
//! tttttttttttttttttttttttttttttttttttttttttiiiiiiiiiirrrrrrrrrrrr...
|
||||
//! Timestamp ID Seed
|
||||
//! ```
|
||||
use crate::epoch_timestamp;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use rand::Rng;
|
||||
|
||||
static SEED_LEN: usize = 12;
|
||||
// static ID_LEN: usize = 10;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct AlmostSnowflake(String);
|
||||
|
||||
pub fn bigint(input: usize) -> BigInt {
|
||||
BigInt::from(input)
|
||||
}
|
||||
|
||||
impl AlmostSnowflake {
|
||||
/// Create a new [`AlmostSnowflake`]
|
||||
pub fn new(server_id: usize) -> Self {
|
||||
// generate random bytes
|
||||
let mut bytes = String::new();
|
||||
|
||||
let mut rng = rand::rng();
|
||||
for _ in 1..=SEED_LEN {
|
||||
bytes.push_str(&rng.random_range(0..10).to_string())
|
||||
}
|
||||
|
||||
// build id
|
||||
let mut id = bigint(epoch_timestamp(2024) as usize) << 22_u128;
|
||||
id |= bigint((server_id % 1024) << 12);
|
||||
id |= bigint((bytes.parse::<usize>().unwrap() + 1) % 4096);
|
||||
|
||||
// return
|
||||
Self(id.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AlmostSnowflake {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue