mod utils; use wasm_bindgen::prelude::*; use rand::Rng; // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. #[cfg(feature = "wee_alloc")] #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; #[wasm_bindgen] extern { fn alert(s: &str); } #[wasm_bindgen] pub fn generate(len: usize, lower: bool, upper: bool, number: bool, special: bool) -> String { let mut upper_charset: Vec = String::from("ABCDEFGHIJKLMNOPQRSTUVWXYZ").chars().collect(); let mut lower_charset: Vec = String::from("abcdefghijklmnopqrstuvwxyz").chars().collect(); let mut number_charset: Vec = String::from("1234567890").chars().collect(); let mut special_charset: Vec = String::from("!@#$%^&*()+{}[]").chars().collect(); let mut result: Vec = Vec::new(); let mut rng = rand::thread_rng(); if lower { result.append(&mut lower_charset); } if upper { result.append(&mut upper_charset); } if number { result.append(&mut number_charset); } if special { result.append(&mut special_charset); } (0..len) .map(|_| { let idx = rng.gen_range(0..result.len()); result[idx] as char }) .collect() }