quickwit_common/
coolid.rs1use rand::distributions::Alphanumeric;
21use rand::prelude::*;
22
23const ADJECTIVES: &[&str] = &[
24 "aged",
25 "ancient",
26 "autumn",
27 "billowing",
28 "bitter",
29 "black",
30 "blue",
31 "bold",
32 "broken",
33 "cold",
34 "cool",
35 "crimson",
36 "damp",
37 "dark",
38 "dawn",
39 "delicate",
40 "divine",
41 "dry",
42 "empty",
43 "falling",
44 "floral",
45 "fragrant",
46 "frosty",
47 "green",
48 "hidden",
49 "holy",
50 "icy",
51 "late",
52 "lingering",
53 "little",
54 "lively",
55 "long",
56 "misty",
57 "morning",
58 "muddy",
59 "nameless",
60 "old",
61 "patient",
62 "polished",
63 "proud",
64 "purple",
65 "quiet",
66 "red",
67 "restless",
68 "rough",
69 "shy",
70 "silent",
71 "small",
72 "snowy",
73 "solitary",
74 "sparkling",
75 "spring",
76 "still",
77 "summer",
78 "throbbing",
79 "twilight",
80 "wandering",
81 "weathered",
82 "white",
83 "wild",
84 "winter",
85 "wispy",
86 "withered",
87 "young",
88];
89
90pub fn new_coolid(name: &str) -> String {
92 let mut rng = rand::thread_rng();
93 let adjective = ADJECTIVES[rng.gen_range(0..ADJECTIVES.len())];
94 let slug: String = rng
95 .sample_iter(&Alphanumeric)
96 .take(4)
97 .map(char::from)
98 .collect();
99 format!("{}-{}-{}", name, adjective, slug)
100}
101
102#[cfg(test)]
103mod tests {
104 use std::collections::HashSet;
105
106 use super::new_coolid;
107
108 #[test]
109 fn test_coolid() {
110 let cool_ids: HashSet<String> = std::iter::repeat_with(|| new_coolid("hello"))
111 .take(100)
112 .collect();
113 assert_eq!(cool_ids.len(), 100);
114 }
115}