use uuid::Uuid;
pub fn base62_uuid(pad: bool) -> String {
zero_pad(&base62::encode(Uuid::new_v4().as_u128()), pad, 22)
}
pub fn u128_uuid(pad: bool) -> String {
zero_pad(&Uuid::new_v4().as_u128().to_string(), pad, 39)
}
pub fn decode_u128(s: &str) -> String {
Uuid::from_u128(s.parse::<u128>().unwrap())
.hyphenated()
.to_string()
}
pub fn encode_u128(s: &str, pad: bool) -> String {
zero_pad(&Uuid::parse_str(s).unwrap().as_u128().to_string(), pad, 39)
}
pub fn decode(s: &str) -> String {
Uuid::from_u128(base62::decode(s).unwrap())
.hyphenated()
.to_string()
}
pub fn encode(s: &str, pad: bool) -> String {
zero_pad(
&base62::encode(Uuid::parse_str(s).unwrap().as_u128()),
pad,
22,
)
}
pub fn zero_pad(s: &str, pad: bool, n: usize) -> String {
if !pad {
return s.to_string();
}
let mut s = s.to_string();
while s.len() < n {
s.insert(0, '0');
}
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip() {
let id = base62_uuid(true);
let id_decoded = decode(&id);
let id_encoded = encode(&id_decoded, true);
assert_eq!(id_encoded, id);
}
#[test]
#[ignore]
fn roundtrip_100000() {
for _ in 0..100000 {
roundtrip();
}
}
}