feat: hashids

This commit is contained in:
Heng-Yi Wu 2022-11-01 21:15:13 +08:00
parent e258bcc2bd
commit b5da40fbdc
No known key found for this signature in database
GPG key ID: 3A63404431B91B99
10 changed files with 2165 additions and 16 deletions

18
src/util/hashids.rs Normal file
View file

@ -0,0 +1,18 @@
use harsh::Harsh;
use lazy_static::lazy_static;
lazy_static! {
pub static ref HARSH: Harsh = Harsh::builder().length(6).build().unwrap();
}
pub fn to_hashids(number: u64) -> String {
HARSH.encode(&[number])
}
pub fn to_u64(hash_id: &str) -> Result<u64, &str> {
let ids = HARSH
.decode(hash_id)
.map_err(|_e| "Failed to decode hash ID")?;
let id = ids.get(0).ok_or("No ID found in hash ID")?;
Ok(*id)
}