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
//! src/generate.rs
use rand::{
distributions::Uniform,
rngs::{OsRng, SmallRng},
thread_rng, Rng, SeedableRng,
};
/// Generates a random string of a specified length using a given character set.
///
/// # Arguments
///
/// * `length` - The length of the generated string.
/// * `charset` - A slice of bytes representing the character set to use for generating the string.
///
/// # Returns
///
/// A `String` containing randomly selected characters from the provided character set.
///
/// # Examples
///
/// ```
/// use advanced_random_string::{charset, generate};
///
/// let random_string = generate(10, charset::BASE62);
/// println!("Generated string: {}", random_string);
///
/// // Specify a custom charset
/// let charset = b"MY_CHARSET";
/// let random_string_with_custom_charset = generate(10, charset);
/// println!("Generated string: {}", random_string_with_custom_charset);
/// ```
pub fn generate(length: usize, charset: &[u8]) -> String {
let mut rng = thread_rng();
generate_with_rng(length, charset, &mut rng)
}
/// Generates a unsecure random string with SmallRng of a specified length using a given character set.
///
/// # Arguments
///
/// * `length` - The length of the generated string.
/// * `charset` - A slice of bytes representing the character set to use for generating the string.
///
/// # Returns
///
/// A `String` containing randomly selected characters from the provided character set.
///
/// # Examples
///
/// ```
/// use advanced_random_string::{charset, generate_unsecure};
///
/// let random_string = generate_unsecure(10, charset::BASE62);
/// println!("Generated string: {}", random_string);
/// ```
pub fn generate_unsecure(length: usize, charset: &[u8]) -> String {
let mut rng = SmallRng::from_entropy();
generate_with_rng(length, charset, &mut rng)
}
/// Generates a secure random string with OsRng of a specified length using a given character set.
///
/// # Arguments
///
/// * `length` - The length of the generated string.
/// * `charset` - A slice of bytes representing the character set to use for generating the string.
///
/// # Returns
///
/// A `String` containing randomly selected characters from the provided character set.
///
/// # Examples
///
/// ```
/// use advanced_random_string::{charset, generate_os_secure};
///
/// let random_string = generate_os_secure(10, charset::BASE62);
/// println!("Generated string: {}", random_string);
/// ```
pub fn generate_os_secure(length: usize, charset: &[u8]) -> String {
let mut rng = OsRng;
generate_with_rng(length, charset, &mut rng)
}
/// Generates a random string of a specified length using a given character set and RNG.
///
/// # Arguments
///
/// * `length` - The length of the generated string.
/// * `charset` - A slice of bytes representing the character set to use for generating the string.
/// * `rng` - A mutable reference to an RNG implementing the `Rng` trait.
///
/// # Returns
///
/// A `String` containing randomly selected characters from the provided character set.
///
/// # Examples
///
/// ```
/// use rand::SeedableRng;
/// use rand::rngs::SmallRng;
/// use advanced_random_string::{charset, generate_with_rng};
///
/// let mut rng = SmallRng::from_entropy();
/// let random_string = generate_with_rng(10, charset::BASE62, &mut rng);
/// println!("Generated string: {}", random_string);
/// ```
pub fn generate_with_rng<R: Rng>(length: usize, charset: &[u8], rng: &mut R) -> String {
let uniform = Uniform::from(0..charset.len());
(0..length)
.map(|_| {
let idx = rng.sample(uniform);
charset[idx] as char
})
.collect()
}