add utility crate
This commit is contained in:
parent
535eb3540b
commit
2f9a6859f1
|
@ -28,7 +28,7 @@ mod tests {
|
||||||
use super::get_database;
|
use super::get_database;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_get_mock_without_initialization() {
|
fn can_get_mock() {
|
||||||
assert!(get_database().is_ok());
|
get_database().unwrap().as_mock_connection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "util"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
chrono = "0.4.24"
|
||||||
|
radix_fmt = "1.0.0"
|
||||||
|
rand = "0.8.5"
|
|
@ -0,0 +1,31 @@
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use radix_fmt::radix_36;
|
||||||
|
|
||||||
|
const TIME_2000: i64 = 946_684_800_000;
|
||||||
|
|
||||||
|
/// FIXME: Should we continue aid, or use other (more secure and scalable) guids
|
||||||
|
/// such as [Cuid2](https://github.com/paralleldrive/cuid2)?
|
||||||
|
pub fn create_aid(date: DateTime<Utc>) -> String {
|
||||||
|
let time = date.timestamp_millis() - TIME_2000;
|
||||||
|
let time = if time < 0 { 0 } else { time };
|
||||||
|
let num: i16 = rand::random();
|
||||||
|
let mut noise = format!("{:0>2}", radix_36(num).to_string());
|
||||||
|
let noise = noise.split_off(noise.len() - 2);
|
||||||
|
format!("{:0>8}{}", radix_36(time).to_string(), noise,)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use chrono::{TimeZone, Utc};
|
||||||
|
|
||||||
|
use super::create_aid;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn generate_aid() {
|
||||||
|
let date = Utc.with_ymd_and_hms(2023, 5, 25, 11, 49, 37).unwrap();
|
||||||
|
let aid = create_aid(date);
|
||||||
|
assert_eq!(aid.len(), 10);
|
||||||
|
assert!(aid.starts_with("9f6mynag"));
|
||||||
|
assert_ne!(create_aid(Utc::now()), create_aid(Utc::now()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod id;
|
Loading…
Reference in New Issue