rustyrs 0.5.5

Generates unique slugs for various uses
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
pub use core::*;

#[cfg(feature = "wasm")]
mod wasm {
    use crate::core::{
        random_slugs as _random_slugs,
        combinations as _combinations,
        get_words,
        ADJ_FILE,
        NOUN_FILE,
        WordSelector
    };
    use wasm_bindgen::prelude::*;
    use rand::seq::SliceRandom;

    #[wasm_bindgen]
    pub fn random_slugs(word_length: i32, num_outputs: Option<i32>) -> Option<Vec<String>> {
        match _random_slugs(word_length, num_outputs) {
            Ok(v) => Some(v),
            Err(_e) => None,
        }
    }

    #[wasm_bindgen]
    pub struct SlugGenerator {
        generator: WordSelector,
    }

    #[wasm_bindgen]
    impl SlugGenerator {
        #[wasm_bindgen(constructor)]
        pub fn new(word_length: i32) -> Result<SlugGenerator, JsError> {
            if word_length < 1 || word_length > 5 {
                Err(JsError::new(
                    "word_length must be between 1 and 5"
                ))
            } else {
                let mut rng = rand::thread_rng();
                let mut adjs = get_words(ADJ_FILE);
                let mut nouns = get_words(NOUN_FILE);
                adjs.shuffle(&mut rng);
                nouns.shuffle(&mut rng);
                let generator = if let Ok(gen) = WordSelector::new(
                    adjs, nouns,
                    word_length as usize
                ) {
                    gen
                } else {
                    return Err(JsError::new("Failure creating WordSelector object"))
                };
                Ok(Self {generator})
            }
        }

        // Calls the next item in the generator. Returns None when no more unique
        // slugs can be generated
        pub fn next(&mut self) -> Option<String> {
            if let Ok(slug) = self.generator.choose() {
                Some(slug)
            } else {
                None
            }
        }
    }

    // TODO: fix the fact integers overflow in wasm
    #[wasm_bindgen]
    pub fn combinations(word_length: i32) -> Option<u64> {
        match _combinations(word_length) {
            Ok(v) => Some(v as u64),
            Err(_e) => None
        }
    }
}

#[cfg(feature = "python")]
mod python {
    use pyo3::exceptions::{PyRuntimeError, PyValueError};
    use pyo3::prelude::*;
    use rand::seq::SliceRandom;

    use crate::core::{
        combinations as _combinations,
        random_slugs as _random_slugs,
        get_slug as _get_slug,
        GeneralException,
        WordSelector,
        EternalSlugGenerator as _EternalSlugGenerator,
        get_words,
        ADJ_FILE,
        NOUN_FILE
    };

    #[pyclass]
    pub struct SlugGenerator {
        generator: WordSelector
    }

    #[pymethods]
    impl SlugGenerator {
        #[new]
        fn new(word_length: i32) -> PyResult<Self> {
            if word_length < 1 || word_length > 5 {
                Err(PyValueError::new_err(
                    "word_length must be between 1 and 5"
                ))
            } else {
                let mut rng = rand::thread_rng();
                let mut adjs = get_words(ADJ_FILE);
                let mut nouns = get_words(NOUN_FILE);
                adjs.shuffle(&mut rng);
                nouns.shuffle(&mut rng);
                let generator = if let Ok(gen) = WordSelector::new(
                    adjs, nouns,
                    word_length as usize
                ) {
                    gen
                } else {
                    return Err(PyRuntimeError::new_err("Failure creating WordSelector object"))
                };
                Ok(Self {generator})
            }
        }
        fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
            slf
        }

        fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<String> {
            match slf.generator.choose() {
                Ok(slug) => Some(slug),
                // Err(e) => Err(PyValueError::new_err(
                //     e.to_string()
                // )),
                Err(_e) => None
            }
        }
    }

    #[pyclass]
    pub struct EternalSlugGenerator {
        generator: _EternalSlugGenerator
    }

    #[pymethods]
    impl EternalSlugGenerator {
        #[new]
        fn new(word_length: i32) -> PyResult<Self> {
            let gen_res = _EternalSlugGenerator::new(word_length);
            match gen_res {
                Ok(generator) => Ok(Self { generator }),
                Err(_e) => Err(PyRuntimeError::new_err("Failure creating generator object"))
            }

        }
        fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
            slf
        }

        fn __next__(mut slf: PyRefMut<'_, Self>) -> String {
            slf.generator.next()
        }
    }

    #[pyfunction]
    fn get_slug(word_length: i32) -> PyResult<String> {
        match _get_slug(word_length) {
            Ok(i) => Ok(i),
            Err(e) => Err(PyValueError::new_err(String::from(e))),
        }
    }

    #[pyfunction]
    fn combinations(word_length: i32) -> PyResult<usize> {
        match _combinations(word_length) {
            Ok(i) => Ok(i),
            Err(e) => Err(PyValueError::new_err(String::from(e))),
        }
    }

    #[pyfunction]
    fn random_slugs(word_length: i32, num_outputs: Option<i32>) -> PyResult<Vec<String>> {
        if 0 < word_length && word_length < 6 {
            match _random_slugs(word_length, num_outputs) {
                Ok(r) => Ok(r),
                Err(e) => match e {
                    GeneralException::NoMoreUniqueCombinations => Err(PyValueError::new_err(format!(
                        "Requested to generate more slugs than they are unique combinations. Max for {}-word slugs is: {}",
                        word_length, combinations(word_length).unwrap()
                    ))),
                    e => Err(PyValueError::new_err(String::from(e)))
                }
            }
        } else {
            Err(PyValueError::new_err(
                "Number of words must be between 1 an 5",
            ))
        }
    }

    #[pymodule]
    fn rustyrs(m: &Bound<'_, PyModule>) -> PyResult<()> {
        m.add_function(wrap_pyfunction!(random_slugs, m)?)?;
        m.add_function(wrap_pyfunction!(get_slug, m)?)?;
        m.add_function(wrap_pyfunction!(combinations, m)?)?;
        m.add_class::<SlugGenerator>()?;
        m.add_class::<EternalSlugGenerator>()?;
        Ok(())
    }
}

mod core {
    use rand::seq::SliceRandom;

    #[derive(Debug, Clone)]
    pub enum GeneralException {
        NoMoreUniqueCombinations,
        InvalidWordLength(i32),
        Other(String)
    }
    impl From<GeneralException> for String {
        fn from(error: GeneralException) -> Self {
            match error {
                GeneralException::InvalidWordLength(got) => format!("Only slugs of length 1 to 5 are supported. Tried: {}", got),
                GeneralException::NoMoreUniqueCombinations => "Cannot generate any more unique combinations for this length in words".to_string(),
                GeneralException::Other(payload) => payload
            }
        }
    }

    // bundle the files into the executable
    pub static NOUN_FILE: &'static [u8] = include_bytes!("./data/nouns.txt");
    pub static ADJ_FILE: &'static [u8] = include_bytes!("./data/adjs.txt");

    /// A slug generator generator that will generate slugs forever. Each slug is suffixed with its iteration number.
    /// After iteration through all possible unique combinations the suffix number is incremented and the generator starts
    /// from the beginning. Use cases are for when a smaller slug is required but uniqueness needs to be guaranteed over a longer
    /// period of time.
    /// If the length of the slug is not important then a 4 or 5 word slug generator should be more than satisfactory from a uniqueness
    /// perspective given they could be trillions of unique combinations.
    pub struct EternalSlugGenerator {
        generator: WordSelector,
        its_completed: usize
    }

    impl EternalSlugGenerator {

        pub fn new(word_length: i32) -> Result<Self, GeneralException> {
            match Self::get_word_selector(word_length) {
                Ok(generator) => Ok(EternalSlugGenerator{ generator, its_completed: 0 }),
                Err(e) => Err(e)
            }
        }
        fn get_word_selector(word_length: i32) -> Result<WordSelector, GeneralException> {
            if word_length < 1 || word_length > 5 {
                Err(GeneralException::InvalidWordLength(word_length))
            } else {
                let mut rng = rand::thread_rng();
                let mut adjs = get_words(ADJ_FILE);
                let mut nouns = get_words(NOUN_FILE);
                adjs.shuffle(&mut rng);
                nouns.shuffle(&mut rng);
                match WordSelector::new(
                    adjs, nouns,
                    word_length as usize
                ) {
                    Ok(generator) => Ok(generator),
                    Err(e) => Err(e)
                }
            }
        }
        pub fn next(&mut self) -> String {
            if let Ok(slug) = self.generator.choose() {
                format!("{}-{}", slug, self.its_completed)
            } else {
                self.its_completed += 1;
                self.generator = Self::get_word_selector(self.generator.get_word_len() as i32).unwrap();
                self.next()
            }
        }
    }

    pub fn random_slugs(
        word_length: i32,
        num_outputs: Option<i32>,
    ) -> Result<Vec<String>, GeneralException> {
        let num_outputs_u = num_outputs.unwrap_or(1);
        let max_combos = combinations(word_length)?;
        if num_outputs_u as usize > max_combos {
            Err(GeneralException::NoMoreUniqueCombinations)
        } else {
            create_phrases(word_length as usize, num_outputs_u)
        }
    }
    pub fn get_slug(word_length: i32) -> Result<String, GeneralException> {
        let mut adjs: Vec<String> = get_words(ADJ_FILE);
        let mut nouns: Vec<String> = get_words(NOUN_FILE);
        let mut rng = rand::thread_rng();
        adjs.shuffle(&mut rng);
        nouns.shuffle(&mut rng);
        let mut ws = WordSelector::new(adjs, nouns, word_length as usize)?;
        ws.choose()
    }

    pub fn combinations(word_length: i32) -> Result<usize, GeneralException> {
        let adjs: Vec<String> = get_words(ADJ_FILE);
        let nouns: Vec<String> = get_words(NOUN_FILE);
        match word_length {
            1 => Ok(nouns.len()),
            2 => Ok(adjs.len() * nouns.len()),
            3 => Ok((adjs.len().pow(2)) * nouns.len()),
            4 => Ok((adjs.len().pow(2)) * (nouns.len().pow(2))),
            5 => Ok((adjs.len().pow(3)) * (nouns.len().pow(2))),
            n => Err(GeneralException::InvalidWordLength(n)),
        }
    }
    pub fn get_words(word_file: &[u8]) -> Vec<String> {
        let contents: &str = std::str::from_utf8(word_file).unwrap();
        let words = contents.split("\n").map(|s| s.to_string()).collect();
        words
    }

    fn create_phrases(word_length: usize, num_outputs: i32) -> Result<Vec<String>, GeneralException> {
        let mut rng = rand::thread_rng();
        let mut adjs: Vec<String> = get_words(ADJ_FILE);
        adjs.shuffle(&mut rng);
        let mut nouns: Vec<String> = get_words(NOUN_FILE);
        nouns.shuffle(&mut rng);
        let mut ws = WordSelector::new(adjs, nouns, word_length)?;
        let mut words = Vec::new();
        for _i in 0..num_outputs {
            words.push(ws.choose()?)
        }
        Ok(words)
    }

    /// This special class is designed to ensure uniqueness when generating random names.
    /// It uses combinatoric logic to hold state between calls to .choose()
    pub struct WordSelector {
        adjs: Vec<String>,
        nouns: Vec<String>,
        selection_ptrs: Vec<Vec<usize>>,
        selection_i: usize,
        word_len: usize,
        total_combinations: usize,
        its_completed: usize,
    }
    impl WordSelector {
        pub fn new(
            adjs: Vec<String>,
            nouns: Vec<String>,
            word_len: usize,
        ) -> Result<Self, GeneralException> {
            let selection_ptrs = match word_len {
                1 => Vec::new(),
                2 => {
                    let mut ptrs = Vec::with_capacity(adjs.len());
                    let mut noun_i_ct = 0;
                    for _ in 0..adjs.len() {
                        ptrs.push(vec![noun_i_ct]);
                        noun_i_ct = if noun_i_ct == nouns.len() - 1 {
                            0
                        } else {
                            noun_i_ct + 1
                        };
                    }
                    ptrs
                }
                3 => {
                    let mut ptrs = Vec::with_capacity(adjs.len());
                    let mut noun_i = 0 as usize;
                    let mut adj_2_i = adjs.len() - 1;

                    for i in 0..adjs.len() {
                        ptrs.push(vec![adj_2_i, noun_i]);

                        noun_i = if noun_i == nouns.len() - 1 {
                            0
                        } else {
                            noun_i + 1
                        };
                        adj_2_i = adjs.len() - 1 - i;
                    }
                    ptrs
                }
                4 => {
                    let mut ptrs = Vec::with_capacity(adjs.len());
                    let mut noun_i = 0 as usize;
                    let mut adj_2_i = adjs.len() - 1;
                    let mut noun_2_i = nouns.len() - 1;

                    for i in 0..adjs.len() {
                        ptrs.push(vec![adj_2_i, noun_i, noun_2_i]);

                        noun_i = if noun_i == nouns.len() - 1 {
                            0
                        } else {
                            noun_i + 1
                        };
                        adj_2_i = adjs.len() - 1 - i;
                        noun_2_i = if noun_2_i == nouns.len() - 1 {
                            0
                        } else {
                            noun_2_i + 1
                        }
                    }
                    ptrs
                }
                5 => {
                    let mut ptrs = Vec::with_capacity(adjs.len());
                    let mut noun_i = 0 as usize;
                    let mut adj_2_i = adjs.len() - 1;
                    let mut adj_3_i = adjs.len() / 2;
                    let mut noun_2_i = nouns.len() - 1;

                    for i in 0..adjs.len() {
                        ptrs.push(vec![adj_2_i, noun_i, adj_3_i, noun_2_i]);

                        noun_i = if noun_i == nouns.len() - 1 {
                            0
                        } else {
                            noun_i + 1
                        };
                        adj_2_i = adjs.len() - 1 - i;
                        adj_3_i = if adj_3_i == adjs.len() - 1 {
                            0
                        } else {
                            adj_3_i + 1
                        };
                        noun_2_i = if noun_2_i == nouns.len() - 1 {
                            0
                        } else {
                            noun_2_i + 1
                        }
                    }
                    ptrs
                }
                n => return Err(GeneralException::InvalidWordLength(n as i32)),
            };
            Ok(Self {
                adjs,
                nouns,
                selection_ptrs,
                word_len,
                total_combinations: combinations(word_len as i32)?,
                its_completed: 0,
                selection_i: 0,
            })
        }
        pub fn choose(&mut self) -> Result<String, GeneralException> {
            if self.its_completed == self.total_combinations {
                return Err(GeneralException::NoMoreUniqueCombinations);
            }
            match self.word_len {
                1 => Ok(self.choose_1()),
                2 => Ok(self.choose_2()),
                3 => Ok(self.choose_3()),
                4 => Ok(self.choose_4()),
                5 => Ok(self.choose_5()),
                n => Err(GeneralException::InvalidWordLength(n as i32)),
            }
        }
        fn choose_1(&mut self) -> String {
            let phrase = self.nouns[self.selection_i].clone();
            self.selection_i += 1;
            self.its_completed += 1;
            phrase
        }
        /// Function to return a two word slug. The internal selection_map holds pointers
        /// to the adjective list as keys and a pointer to a noun as the value. For each
        /// iteration both pointers are incremented to ensure that each output does not contain
        /// similar word as the previous output. Pointers are wrapped when they go out of bounds
        /// to ensure all possible combinations can be generated.
        fn choose_2(&mut self) -> String {
            let noun_i = self.selection_ptrs[self.selection_i]
                .last()
                .unwrap()
                .clone();
            let phrase = format!("{}-{}", self.adjs[self.selection_i], self.nouns[noun_i]);
            let noun_ptr = self
                .selection_ptrs
                .get_mut(self.selection_i)
                .unwrap()
                .last_mut()
                .unwrap();

            *noun_ptr = if noun_i == self.nouns.len() - 1 {
                // ptr sent back to beginning of the noun array
                0
            } else {
                noun_i + 1
            };

            self.selection_i = if self.selection_i == self.selection_ptrs.len() - 1 {
                // reached the end of the adjective list so return to the beginning
                0
            } else {
                self.selection_i + 1
            };
            self.its_completed += 1;
            phrase
        }

        fn choose_3(&mut self) -> String {
            let adj_1_i = self.selection_i;
            let adj_2_i = self.selection_ptrs[self.selection_i][0];
            let noun_i = self.selection_ptrs[self.selection_i][1];

            let phrase = format!(
                "{}-{}-{}",
                self.adjs[adj_1_i], self.adjs[adj_2_i], self.nouns[noun_i]
            );

            let ptr_set = self
                .selection_ptrs
                .get_mut(self.selection_i)
                .expect("Unable to obtain mutable reference to index pointer set");

            if noun_i == self.nouns.len() - 1 {
                // reached end of iteration of nouns so decrement
                // the second adj_pointer
                ptr_set[0] = if adj_2_i == 0 {
                    self.adjs.len() - 1
                } else {
                    adj_2_i - 1
                };

                // reset noun pointer
                ptr_set[1] = 0
            } else {
                ptr_set[1] += 1
            }
            self.selection_i = if self.selection_i == self.selection_ptrs.len() - 1 {
                0
            } else {
                self.selection_i + 1
            };
            self.its_completed += 1;
            phrase
        }
        fn choose_4(&mut self) -> String {
            let adj_1_i = self.selection_i;
            let adj_2_i = self.selection_ptrs[self.selection_i][0];
            let noun_i = self.selection_ptrs[self.selection_i][1];
            let noun_2_i = self.selection_ptrs[self.selection_i][2];

            let phrase = format!(
                "{}-{}-of-{}-{}",
                self.adjs[adj_1_i], self.nouns[noun_i], self.adjs[adj_2_i], self.nouns[noun_2_i]
            );

            let ptr_set = self
                .selection_ptrs
                .get_mut(self.selection_i)
                .expect("Unable to obtain mutable reference to index pointer set");

            if noun_2_i == 0 {
                // reached end of iteration of 2nd noun so increment
                // the first noun pointer and reset noun 2 to top
                ptr_set[1] += 1;

                ptr_set[2] = self.nouns.len() - 1
            } else {
                ptr_set[2] -= 1;
            }

            if ptr_set[1] > self.nouns.len() - 1 {
                // decrement 2nd adjective on first noun iteration completion
                ptr_set[0] = if adj_2_i == 0 {
                    self.adjs.len() - 1
                } else {
                    adj_2_i - 1
                };

                // reset noun pointer
                ptr_set[1] = 0
            }
            self.selection_i = if self.selection_i == self.selection_ptrs.len() - 1 {
                0
            } else {
                self.selection_i + 1
            };
            self.its_completed += 1;
            phrase
        }
        fn choose_5(&mut self) -> String {
            let adj_1_i = self.selection_i;
            let adj_2_i = self.selection_ptrs[self.selection_i][0];
            let noun_i = self.selection_ptrs[self.selection_i][1];
            let adj_3_i = self.selection_ptrs[self.selection_i][2];
            let noun_2_i = self.selection_ptrs[self.selection_i][3];

            let phrase = format!(
                "{}-{}-{}-of-{}-{}",
                self.adjs[adj_1_i],
                self.adjs[adj_2_i],
                self.nouns[noun_i],
                self.adjs[adj_3_i],
                self.nouns[noun_2_i]
            );

            let ptr_set = self
                .selection_ptrs
                .get_mut(self.selection_i)
                .expect("Unable to obtain mutable reference to index pointer set");

            if ptr_set[3] == 0 {
                // reached end of iteration of 2nd noun so increment
                // the third adj pointer and reset noun 2 to top
                ptr_set[2] += 1;

                ptr_set[3] = self.nouns.len() - 1
            } else {
                ptr_set[3] -= 1;
            }

            if ptr_set[2] >= self.adjs.len() {
                // increment first noun on third adj it completion
                ptr_set[1] += 1;

                // reset third adj pointer
                ptr_set[2] = 0;
            }

            if ptr_set[1] >= self.nouns.len() {
                // decrement second adj on first noun it comp
                ptr_set[0] = if ptr_set[0] == 0 {
                    self.adjs.len() - 1
                } else {
                    ptr_set[0] - 1
                };
                ptr_set[1] = 0;
            }

            self.selection_i = if self.selection_i == self.selection_ptrs.len() - 1 {
                0
            } else {
                self.selection_i + 1
            };
            self.its_completed += 1;
            phrase
        }

        pub fn get_word_len(&self) -> usize {
            self.word_len
        }
    }
}

#[cfg(test)]
mod tests {

    use std::collections::HashSet;

    use crate::{get_slug, EternalSlugGenerator};

    use super::core::{combinations, random_slugs};

    #[test]
    fn happy_2() {
        assert!(random_slugs(2, Some(1)).unwrap().len() > 0);
    }

    #[test]
    fn unhappy_high() {
        match random_slugs(6, Some(1)) {
            Ok(_v) => assert!(false),
            Err(_e) => assert!(true),
        }
    }

    #[test]
    fn unhappy_low() {
        match random_slugs(0, Some(1)) {
            Ok(_v) => assert!(false),
            Err(_e) => assert!(true),
        }
    }

    #[test]
    fn unhappy_negative() {
        match random_slugs(-1, Some(1)) {
            Ok(_v) => assert!(false),
            Err(_e) => assert!(true),
        }
    }

    #[test]
    fn combinations_happy() {
        let mut combo = 0;
        for i in 1..5 {
            let val = combinations(i).unwrap();
            assert!(val > combo);
            combo += val
        }
    }

    #[test]
    fn combinations_unhappy_high() {
        match combinations(6) {
            Ok(_v) => assert!(false),
            Err(_e) => assert!(true),
        }
    }

    #[test]
    fn combinations_unhappy_low() {
        match combinations(0) {
            Ok(_v) => assert!(false),
            Err(_e) => assert!(true),
        }
    }

    #[test]
    fn combinations_unhappy_negative() {
        match combinations(-1) {
            Ok(_v) => assert!(false),
            Err(_e) => assert!(true),
        }
    }

    #[test]
    fn happy_2_all_unique_half() {
        let combos = combinations(2).unwrap() / 2;
        let slugs = random_slugs(2, Some(combos as i32))
            .expect("unable to create 2 word slugs for all possible combinations");
        assert!(slugs.len() == combos);
        let mut hs = HashSet::new();
        dbg!(&slugs[..10]);
        for slug in slugs {
            hs.insert(slug);
        }
        assert_eq!(hs.len(), combos)
    }

    #[test]
    fn happy_2_all_unique_all() {
        let possible_combos = combinations(2).unwrap();
        let slugs = random_slugs(2, Some(possible_combos as i32))
            .expect("unable to create 2 word slugs for all possible combinations");
        assert!(slugs.len() == possible_combos);
        let mut hs = HashSet::new();
        dbg!(&slugs[..10]);
        for slug in slugs {
            hs.insert(slug);
        }
        assert_eq!(hs.len(), possible_combos)
    }

    #[test]
    fn happy_3_all_unique_1_million() {
        // only generate 10 million to save time because the actual total combinations could be well over half a billion
        let combos = 1_000_000;
        let slugs = random_slugs(3, Some(combos as i32))
            .expect("unable to create 2 word slugs for all possible combinations");
        assert!(slugs.len() == combos);
        let mut hs = HashSet::new();
        dbg!(&slugs[..10]);
        for slug in slugs {
            hs.insert(slug);
        }
        assert_eq!(hs.len(), combos)
    }
    #[test]
    fn happy_4_all_unique_1_million() {
        // only generate 10 million to save time because the actual total combinations could be well over half a billion
        let combos = 1_000_000;
        let slugs = random_slugs(4, Some(combos as i32))
            .expect("unable to create 2 word slugs for all possible combinations");
        assert!(slugs.len() == combos);
        let mut hs = HashSet::new();
        dbg!(&slugs[..10]);
        for slug in slugs {
            hs.insert(slug);
        }
        assert_eq!(hs.len(), combos)
    }

    #[test]
    fn happy_5_all_unique_1_million() {
        // only generate 10 million to save time because the actual total combinations could be well over half a billion
        let combos = 1_000_000;
        let slugs = random_slugs(4, Some(combos as i32))
            .expect("unable to create 2 word slugs for all possible combinations");
        assert!(slugs.len() == combos);
        let mut hs = HashSet::new();
        dbg!(&slugs[..10]);
        for slug in slugs {
            hs.insert(slug);
        }
        assert_eq!(hs.len(), combos)
    }

    #[test]
    fn test_get_slug_different_slug(){
        // check that the get_slug function does not return the
        // same slug twice
        assert_ne!(get_slug(2).unwrap(), get_slug(2).unwrap())
    }

    #[test]
    fn test_eternal_slug_gen(){
        let mut slug_gen = EternalSlugGenerator::new(1).unwrap();
        let max_its = combinations(1).unwrap();
        for _i in 0..max_its {
            let slug = slug_gen.next();
            assert_eq!(slug.chars().nth(slug.len() - 1).unwrap(), '0');
        }
        let next_slug = slug_gen.next();
        assert_eq!(next_slug.chars().nth(next_slug.len() - 1).unwrap(), '1');
    }
}