karton/src/util/hashids.rs

19 lines
442 B
Rust
Raw Normal View History

2022-11-01 21:15:13 +08:00
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")?;
2022-11-08 16:30:16 -05:00
let id = ids.first().ok_or("No ID found in hash ID")?;
2022-11-01 21:15:13 +08:00
Ok(*id)
}