ambient_friendly_id/
lib.rs

1// This code was adapted from `friendly_id` (MIT/Apache2):
2//
3// https://github.com/mariuszs/friendly_id/blob/d691da682027b84eddd771d7adac0c0dc2563a35/src/base62.rs
4//
5// The crate depends on `failure`, which is unmaintained. As we only need to generate some random IDs,
6// we can make do by reimplementing it.
7
8pub fn friendly_id() -> String {
9    const BASE: u128 = 62;
10    const ALPHABET: [u8; BASE as usize] = [
11        b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E',
12        b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T',
13        b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i',
14        b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x',
15        b'y', b'z',
16    ];
17
18    let mut num: u128 = rand::random();
19
20    let mut bytes = Vec::new();
21    while num > 0 {
22        bytes.push(ALPHABET[(num % BASE) as usize]);
23        num /= BASE;
24    }
25    bytes.reverse();
26
27    String::from_utf8(bytes).unwrap()
28}