advanced_random_string/random_string.rs
1//! src/generate.rs
2
3use rand::{
4 distributions::Uniform,
5 rngs::{OsRng, SmallRng},
6 thread_rng, Rng, SeedableRng,
7};
8
9/// Generates a random string of a specified length using a given character set.
10///
11/// # Arguments
12///
13/// * `length` - The length of the generated string.
14/// * `charset` - A slice of bytes representing the character set to use for generating the string.
15///
16/// # Returns
17///
18/// A `String` containing randomly selected characters from the provided character set.
19///
20/// # Examples
21///
22/// ```
23/// use advanced_random_string::{charset, random_string};
24///
25/// let random_string = random_string::generate(10, charset::BASE62);
26/// println!("Generated string: {}", random_string);
27///
28/// // Specify a custom charset
29/// let charset = b"MY_CHARSET";
30/// let random_string_with_custom_charset = random_string::generate(10, charset);
31/// println!("Generated string: {}", random_string_with_custom_charset);
32/// ```
33pub fn generate(length: usize, charset: &[u8]) -> String {
34 let mut rng = thread_rng();
35
36 generate_with_rng(length, charset, &mut rng)
37}
38
39/// Generates a unsecure random string with SmallRng of a specified length using a given character set.
40///
41/// # Arguments
42///
43/// * `length` - The length of the generated string.
44/// * `charset` - A slice of bytes representing the character set to use for generating the string.
45///
46/// # Returns
47///
48/// A `String` containing randomly selected characters from the provided character set.
49///
50/// # Examples
51///
52/// ```
53/// use advanced_random_string::{charset, random_string};
54///
55/// let random_string = random_string::generate_unsecure(10, charset::BASE62);
56/// println!("Generated string: {}", random_string);
57/// ```
58pub fn generate_unsecure(length: usize, charset: &[u8]) -> String {
59 let mut rng = SmallRng::from_entropy();
60
61 generate_with_rng(length, charset, &mut rng)
62}
63
64/// Generates a secure random string with OsRng of a specified length using a given character set.
65///
66/// # Arguments
67///
68/// * `length` - The length of the generated string.
69/// * `charset` - A slice of bytes representing the character set to use for generating the string.
70///
71/// # Returns
72///
73/// A `String` containing randomly selected characters from the provided character set.
74///
75/// # Examples
76///
77/// ```
78/// use advanced_random_string::{charset, random_string};
79///
80/// let random_string = random_string::generate_os_secure(10, charset::BASE62);
81/// println!("Generated string: {}", random_string);
82/// ```
83pub fn generate_os_secure(length: usize, charset: &[u8]) -> String {
84 let mut rng = OsRng;
85
86 generate_with_rng(length, charset, &mut rng)
87}
88
89/// Generates a random string of a specified length using a given character set and RNG.
90///
91/// # Arguments
92///
93/// * `length` - The length of the generated string.
94/// * `charset` - A slice of bytes representing the character set to use for generating the string.
95/// * `rng` - A mutable reference to an RNG implementing the `Rng` trait.
96///
97/// # Returns
98///
99/// A `String` containing randomly selected characters from the provided character set.
100///
101/// # Examples
102///
103/// ```
104/// use rand::SeedableRng;
105/// use rand::rngs::SmallRng;
106/// use advanced_random_string::{charset, random_string};
107///
108/// let mut rng = SmallRng::from_entropy();
109/// let random_string = random_string::generate_with_rng(10, charset::BASE62, &mut rng);
110/// println!("Generated string: {}", random_string);
111/// ```
112pub fn generate_with_rng<R: Rng>(length: usize, charset: &[u8], rng: &mut R) -> String {
113 let uniform = Uniform::from(0..charset.len());
114
115 (0..length)
116 .map(|_| {
117 let idx = rng.sample(uniform);
118 charset[idx] as char
119 })
120 .collect()
121}