53fc84a270
Add token, hex, and pin helpers for the common ASCII string formats already represented by the crate's built-in charsets. These methods keep frequent use cases concise while continuing to route through string_from, so callers still get the same uniform per-character sampling behavior. Avoid adding a password preset in this change. Password generation implies policy choices around symbols, ambiguous characters, and service-specific constraints, while these presets are direct names for existing alphabets. Document every preset with doctested examples and add a runnable presets example. Update the demo to show both presets and custom string generation. Test Plan: - cargo test - cargo clippy - cargo clippy --benches - cargo clippy --tests - cargo +nightly fmt Refs: IDEAS.md ergonomics backlog
31 lines
837 B
Rust
31 lines
837 B
Rust
//! Run with: `cargo run --example demo`
|
|
|
|
use ez_urandom::{OsRandom, charset};
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
let mut rng = OsRandom::try_new()?;
|
|
|
|
println!("u8 : {}", rng.get_u8()?);
|
|
println!("u32 : {}", rng.get_u32()?);
|
|
println!("u64 : {}", rng.get_u64()?);
|
|
println!("i32 : {}", rng.get_i32()?);
|
|
|
|
println!("dice 1-6 : {}", rng.gen_range_u32(6)? + 1);
|
|
println!("offset : {}", rng.gen_range_i32_in(-10..=10)?);
|
|
println!("index : {}", rng.gen_range_usize_in(0..16)?);
|
|
|
|
let token = rng.token(24)?;
|
|
println!("token : {token}");
|
|
|
|
let hex = rng.hex(32)?;
|
|
println!("hex : {hex}");
|
|
|
|
let custom = rng.string_from(charset::ALPHANUMERIC, 12)?;
|
|
println!("custom : {custom}");
|
|
|
|
let pin = rng.pin(6)?;
|
|
println!("pin : {pin}");
|
|
|
|
Ok(())
|
|
}
|