1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Generate random uid strings containing letters, numbers, or base62 values.
//!
//! This module provides a way to generate various types of
//! random strings that can be used as a UID. It also allows
//! generation of UID strings that are base62 representation
//! of numbers to allow for converstion to and from `u16`, `u32`,
//! and `u64`.
//!
//! The `UidStore` struct provides helper functions that helps
//! avoid generation of duplicate uid values, which becomes
//! very likely when using short UID's.
//!
//! Standalone functions to generate random strings:
//!
//! ```rust
//! # use uid_store::*;
//! let uid = random_string(8);
//! let uid = random_number(10);
//! let uid = human_random_string(8);
//! ```
//!
//! Convert a number to and from a base62 uid:
//!
//! ```rust
//! # use uid_store::*;
//! let uid = number_to_uid(1000);
//! let number = uid_to_number(&uid).unwrap();
//! ```
//!
//! Generate a sequence of UID's that should be unique:
//!
//! ```rust
//! # use uid_store::*;
//! let mut u = UidStore::new();
//! let uid = u.next(6);
//! let uid = u.next_u16();
//! let uid = u.next_u32();
//! let uid = u.next_u64();
//! ```
//!
//! Initialise a `UidStore` with a sequence of previously
//! generated uid values.
//!
//! ```rust
//! # use uid_store::*;
//! let mut u = UidStore::new();
//! u.make_unique("ifh983u");
//! u.make_unique("Rig3hGa");
//! u.make_unique("h84gh8A");
//! u.make_unique("h84gh8A"); // Duplicate uid triggers new uid generation
//! ```
//!
//! Notice above that `make_unique` was passed the same UID twice, when
//! this happened, a new UID was generated and returned. This should be
//! handled:
//!
//! ```rust
//! # use uid_store::*;
//! # let mut u = UidStore::new();
//! if let Some(deduplicate) = u.make_unique("h84gh8A") {
//!     println!("Duplicate UID for item replaced with: {}", deduplicate);
//! }
//! ```
//!

use std::collections::HashSet;

mod random;

/// UidStore holds a collection of previously generated UID
/// values to ensure a value is only ever generated once.
pub struct UidStore {
    items: HashSet<String>,
}

impl UidStore {
    pub fn new() -> UidStore {
        UidStore {
            items: HashSet::new(),
        }
    }

    /// Generate a UID string with a `length` number of characters.
    pub fn next(&mut self, length: usize) -> &String {
        loop {
            let id = random_string(length);
            if !self.items.insert(id.clone()) {
                continue;
            }
            return self.items.get(&id).unwrap();
        }
    }

    /// Generate a UID string that avoids commonly
    /// confused letters such as i,I,1,L, 0,O,o.
    pub fn next_human(&mut self, length: usize) -> &String {
        loop {
            let id = human_random_string(length);
            if !self.items.insert(id.clone()) {
                continue;
            }
            return self.items.get(&id).unwrap();
        }
    }

    /// Generate a UID string that represents a random `u16` number.
    /// The length of the string depends on the size of the number.
    pub fn next_u16(&mut self) -> &String {
        loop {
            let id = random_max_size(u16::MAX as usize);
            if !self.items.insert(id.clone()) {
                continue;
            }
            return self.items.get(&id).unwrap();
        }
    }

    /// Generate a UID string that represents a random `u32` number.
    /// The length of the string depends on the size of the number.
    pub fn next_u32(&mut self) -> &String {
        loop {
            let id = random_max_size(u32::MAX as usize);
            if !self.items.insert(id.clone()) {
                continue;
            }
            return self.items.get(&id).unwrap();
        }
    }

    /// Generate a UID string that represents a random `u64` number.
    /// The length of the string depends on the size of the number.
    pub fn next_u64(&mut self) -> &String {
        loop {
            let id = random_max_size(u64::MAX as usize);
            if !self.items.insert(id.clone()) {
                continue;
            }
            return self.items.get(&id).unwrap();
        }
    }

    /// Returns true if a UID is already in use.
    pub fn contains(&self, id: &str) -> bool {
        self.items.contains(id)
    }

    /// Returns how many UID's have already been used.
    pub fn size(&self) -> usize {
        self.items.len()
    }

    /// Register a UID with this `UidStore`. Returns `None` if this
    /// string is unique and not previously seen. If the string is
    /// already known and in use, a new uid string is returned.
    pub fn make_unique(&mut self, uid: &str) -> Option<&str> {
        if self.items.contains(uid) {
            return Some(self.next(uid.len()));
        }
        self.items.insert(uid.to_string());
        None
    }
}

/// Generate a random base62 string with a fixed string `length`.
pub fn random_string(length: usize) -> String {
    let result: String = (0..length)
        .map(|_| {
            let idx = random::next_u32() as usize % CHARSET.len();
            CHARSET[idx] as char
        })
        .collect();

    result
}

/// Generate a string of numbers with the specified `length`.
pub fn random_number(length: usize) -> String {
    let result: String = (0..length)
        .map(|_| {
            let idx = random::next_u32() as usize % NUMSET.len();
            NUMSET[idx] as char
        })
        .collect();

    result
}

/// Generate a base62 string using a random number
/// no larger than a specified maximum size.
pub fn random_max_size(maximum_size: usize) -> String {
    if maximum_size > u32::MAX as usize {
        let uid = random::next_u64() as usize % maximum_size;
        return number_to_uid(uid);
    }
    let uid = random::next_u32() as usize % maximum_size;
    number_to_uid(uid)
}

/// Convert the contents of a base62 string back to
/// the number that was used to generate the string.
/// Reverse using `uid_to_number()`.
pub fn number_to_uid(mut uid: usize) -> String {
    let mut result = String::new();
    if uid == 0 {
        return "A".to_string();
    }
    while uid > 0 {
        let next = uid % CHARSET.len();
        uid = uid / CHARSET.len();
        result.push(CHARSET[next] as char);
    }
    result
}

/// Convert a base62 string into the underlying number it
/// represents. Returns None if the string is not a valid
/// base62 number. Reverse using `number_to_uid()`.
pub fn uid_to_number(uid: &str) -> Option<usize> {
    let mut result: usize = 0;
    for c in uid.chars().rev() {
        /* Rust 1.18
        let value = match c {
            'A'..'Z' => c - 'A',
            'a'..'z' => c - 'a' + 26,
            '0'..'9' => c - '0' + 26 + 26,
        };
        */
        let value;
        if c >= 'A' && c <= 'Z' {
            value = (c as usize) - ('A' as usize);
        } else if c >= 'a' && c <= 'z' {
            value = (c as usize) - ('a' as usize) as usize + 26;
        } else if c >= '0' && c <= '9' {
            value = (c as usize) - ('0' as usize) + 26 + 26;
        } else {
            return None;
        }
        result = result * 62 + value;
    }
    Some(result)
}

/// Generate a random string that doedn't include easily confused
/// characters such as i,I,1 and o,O,0.
pub fn human_random_string(length: usize) -> String {
    let result: String = (0..length)
        .map(|_| {
            let idx = random::next_u32() as usize % READABLE_CHARSET.len();
            READABLE_CHARSET[idx] as char
        })
        .collect();

    result
}

const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789";

const READABLE_CHARSET: &[u8] = b"ABCDEFGHJKMNPQRSTUVWXYZ\
abcdefghjkmnpqrstuvwxyz\
123456789";

const NUMSET: &[u8] = b"0123456789";

#[cfg(test)]
mod tests {
    use crate::human_random_string;
    use crate::number_to_uid;
    use crate::random_number;
    use crate::random_string;
    use crate::uid_to_number;
    use crate::UidStore;

    #[test]
    fn test_number_to_uid() {
        assert_eq!(number_to_uid(0), "A");
        assert_eq!(number_to_uid(1), "B");
        assert_eq!(number_to_uid(52), "0");
        assert_eq!(number_to_uid(9902), "sjC");
        assert_eq!(uid_to_number("A"), Some(0));
        assert_eq!(uid_to_number("B"), Some(1));
        assert_eq!(uid_to_number("0"), Some(52));
        assert_eq!(uid_to_number("sjC"), Some(9902));
        assert_eq!(uid_to_number(&number_to_uid(94029)), Some(94029));
        assert_eq!(uid_to_number(&number_to_uid(2294029)), Some(2294029));
        assert_eq!(uid_to_number(&number_to_uid(43494029)), Some(43494029));
    }

    #[test]
    fn test_random_max_size() {
        let mut u = UidStore::new();
        for _ in [0..1000] {
            assert!(uid_to_number(u.next_u16()).unwrap() < u16::MAX.into());
            assert!(uid_to_number(u.next_u32()).unwrap() < u32::MAX.try_into().unwrap());
            assert!(uid_to_number(u.next_u64()).unwrap() < u64::MAX.try_into().unwrap());
        }
    }

    #[test]
    fn test_random() {
        let id = random_string(5);
        assert_eq!(id.len(), 5);

        let id2 = random_string(5);
        assert!(id != id2);

        let id3 = random_number(6);
        let id4 = random_number(6);
        assert_eq!(id3.len(), 6);
        assert!(id3 != id4);

        let id5 = human_random_string(5);
        let id6 = human_random_string(5);
        assert_eq!(id5.len(), 5);
        assert!(id5 != id6);
    }

    #[test]
    fn test_unique() {
        let mut u = UidStore::new();
        let id: String;
        let id2: String;
        {
            id = u.next(10).to_string();
            assert_eq!(id.len(), 10, "failed");
            assert!(u.contains(&id));
        };
        assert_eq!(u.size(), 1, "failed");
        {
            id2 = u.next(8).to_string();
            assert_eq!(id2.len(), 8, "failed");
            assert!(u.contains(&id2));
        };
        assert_eq!(u.size(), 2, "failed");
        assert_ne!(id, id2, "failed");

        let xo = "0123456789";
        {
            let o = u.make_unique(xo);
            assert!(o.is_none(), "failed. Found {:?}", o.unwrap());
        };
        assert_eq!(u.size(), 3, "failed");

        {
            let o = u.make_unique(xo);
            assert!(o.is_some(), "failed. Found {:?}", o);
            assert_ne!(o.unwrap(), xo, "failed");
        };
        assert_eq!(u.size(), 4, "failed");
    }
}