team04_server/log/
uuid_human_hash.rs

1//! A way to "hash" the 128-bit UUIDs into two 3-letter words, such as `eat-fox`,
2//! so that humans can more easily refer to lobbies or players
3//! (instead of having to read the hex-representation of the UUIDs).
4
5use uuid::Uuid;
6
7pub const WORDS1: [&'static str; 32] = [
8    "add", "and", "any", "ask", "bad", "big", "buy", "can", "cat", "cow", "cut", "dig", "dim",
9    "dry", "eat", "far", "fit", "fly", "fun", "has", "hex", "its", "lit", "mad", "new", "oak",
10    "odd", "one", "red", "sad", "shy", "wet",
11];
12pub const WORDS2: [&'static str; 32] = [
13    "ace", "aim", "air", "arm", "ash", "axe", "bag", "bat", "bit", "box", "car", "bug", "day",
14    "egg", "elk", "fan", "fox", "ham", "ice", "ink", "ivy", "inc", "jam", "jar", "jet", "key",
15    "kit", "owl", "pen", "sky", "spy", "van",
16];
17
18/// The last 5 bits of the sum of all bytes in the uuid are the index into the WORDS2 list,
19/// The 5 bits before those are the index into the WORDS1 list.
20/// Output is `WORDS1[i]`, a dash, then `WORDS2[j]`.
21pub fn uuid_human_hash(uuid: &Uuid) -> String {
22    let bytes = uuid.as_bytes();
23    let mut sum: u16 = 0;
24    for i in 0..bytes.len() {
25        sum += bytes[i] as u16;
26    }
27    format!(
28        "{}-{}",
29        WORDS1[((sum >> 5) & (0b11111)) as usize],
30        WORDS2[(sum & (0b11111)) as usize]
31    )
32}