quickwit_common/
coolid.rs

1// Copyright (C) 2021 Quickwit, Inc.
2//
3// Quickwit is offered under the AGPL v3.0 and as commercial software.
4// For commercial licensing, contact us at hello@quickwit.io.
5//
6// AGPL:
7// This program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Affero General Public License as
9// published by the Free Software Foundation, either version 3 of the
10// License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Affero General Public License for more details.
16//
17// You should have received a copy of the GNU Affero General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20use 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
90/// Returns a randomly generated id
91pub 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}