use std::fmt;
use rand::Rng as _;
#[derive(Clone, Copy)]
pub struct Alphanumeric {
length: usize,
}
impl Alphanumeric {
pub fn new(length: usize) -> Self {
Self { length }
}
}
impl Default for Alphanumeric {
fn default() -> Self {
Self { length: 12 }
}
}
impl fmt::Display for Alphanumeric {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let lowercase = f.alternate();
for c in rand::rng()
.sample_iter(&rand::distr::Alphanumeric)
.take(self.length)
.map(|c| if lowercase { c.to_ascii_lowercase() } else { c })
{
write!(f, "{c}", c = c as char)?;
}
Ok(())
}
}