extern crate rand;
extern crate serde;
extern crate serde_json;
use rand::Rng;
use serde::Deserialize;
use std::error::Error;
pub mod corpora;
pub mod primes;
pub mod tlds;
pub mod emojis;
#[derive(Deserialize, Debug)]
struct JSONDataset {
data: Vec<String>,
}
fn get_dataset(key: &str) -> Result<JSONDataset, Box<dyn Error>> {
let json_dataset: &str = match key {
"dinosaur" => corpora::DATA_DINOSAURS,
"cat" => corpora::DATA_CATS,
"dog" => corpora::DATA_DOGS,
"horse" => corpora::DATA_HORSES,
"fabric" => corpora::DATA_FABRICS,
"gemstone" => corpora::DATA_GEMSTONES,
"mood" => corpora::DATA_MOODS,
"tvshow" => corpora::DATA_TV_SHOWS,
"cats" => corpora::DATA_CATS,
"dinosaurs" => corpora::DATA_DINOSAURS,
"dogs" => corpora::DATA_DOGS,
"donkeys" => corpora::DATA_DONKEYS,
"horses" => corpora::DATA_HORSES,
"tv_shows" => corpora::DATA_TV_SHOWS,
"fruits" => corpora::DATA_FRUITS,
"pizzatoppings" => corpora::DATA_PIZZATOPPINGS,
"herbs" => corpora::DATA_HERBS,
"spices" => corpora::DATA_SPICES,
"mixtures" => corpora::DATA_MIXTURES,
"teas" => corpora::DATA_TEAS,
"vegetables" => corpora::DATA_VEGETABLES,
"rpgs" => corpora::DATA_RPGS,
"board_games" => corpora::DATA_BOARD_GAMES,
"wrestling_moves" => corpora::DATA_WRESTLING_MOVES,
"countries" => corpora::DATA_COUNTRIES,
"nationalities" => corpora::DATA_NATIONALITIES,
"governmentforms" => corpora::DATA_GOVERNMENTFORMS,
"authors" => corpora::DATA_AUTHORS,
"bodyparts" => corpora::DATA_BODYPARTS,
"firstnames" => corpora::DATA_FIRSTNAMES,
"lastnames" => corpora::DATA_LASTNAMES,
"moods" => corpora::DATA_MOODS,
"neutralnames" => corpora::DATA_NEUTRALNAMES,
"occupations" => corpora::DATA_OCCUPATIONS,
"prefixes" => corpora::DATA_PREFIXES,
"scientists" => corpora::DATA_SCIENTISTS,
"suffixes" => corpora::DATA_SUFFIXES,
"tolkien_character_names" => corpora::DATA_TOLKIEN_CHARACTER_NAMES,
"fabrics" => corpora::DATA_FABRICS,
"gemstones" => corpora::DATA_GEMSTONES,
"metals" => corpora::DATA_METALS,
"packaging" => corpora::DATA_PACKAGING,
"music_genres" => corpora::DATA_MUSIC_GENRES,
"objects" => corpora::DATA_OBJECTS,
"flowers" => corpora::DATA_FLOWERS,
"toxic_plants" => corpora::DATA_TOXIC_PLANTS,
"weather_conditions" => corpora::DATA_WEATHER_CONDITIONS,
"sports" => corpora::DATA_SPORTS,
"appliances" => corpora::DATA_APPLIANCES,
"new_technologies" => corpora::DATA_NEW_TECHNOLOGIES,
"programming_languages" => corpora::DATA_PROGRAMMING_LANGUAGES,
"tlds" => tlds::DATA_TLDS,
_ => "",
};
let dataset: JSONDataset = serde_json::from_str(json_dataset)?;
return Ok(dataset);
}
pub fn gen_switch(name: String) -> String {
let n: &str = name.as_str();
let data = match get_dataset(n) {
Ok(val) => val.data,
Err(err) => {
eprintln!("Failed getting dataset for {}. {}", name, err);
return "Error: dataset not found".into();
}
};
let mut rnd = rand::rng();
let mut index: usize = 0;
if data.len() - 1 > 0 {
index = rnd.random_range(0..data.len() - 1);
}
return data[index].to_string();
}
pub fn gen_corpora_switch(name: String) -> String {
return gen_switch(name);
}
pub fn gen_prime() -> usize {
let mut rnd = rand::rng();
let index = rnd.random_range(0..primes::DATA_PRIMES.len() - 1);
primes::DATA_PRIMES[index]
}
pub fn gen_emoji() -> String {
let mut rnd = rand::rng();
let index = rnd.random_range(0..emojis::DATA_EMOJIS.len() - 1);
emojis::DATA_EMOJIS[index].into()
}