fakedata_generator 0.8.0

Generate fake data with various generators.
Documentation
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;

/// JSONDataset represents a generic data structure for storing the parsed JSON. Each JSON taken
/// from Corpora has a `data` field which is an Array of Strings in JSON (= Vec<String> in Rust).
#[derive(Deserialize, Debug)]
struct JSONDataset {
    data: Vec<String>,
}

/// `get_dataset` returns the data from the constants defined in `src/corpora/data.rs` and other files, then parses them into
/// a `JSONDataset struct.
fn get_dataset(key: &str) -> Result<JSONDataset, Box<dyn Error>> {
    let json_dataset: &str = match key {
        // manual values, here for compatibility reasons
        "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,
        // auto-generated by ./helpers/corpora-data.sh
        "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" => tlds::DATA_TLDS,
        _ => "",

    };

    let dataset: JSONDataset = serde_json::from_str(json_dataset)?;

    return Ok(dataset);
}

/// `gen_switch` is a special generator that gets its data in JSON format taken from the [Corpora Project](https://github.com/dariusk/corpora). A copy of the entire Corpora project is included in the `data` directory.
/// Not all data sets are available as of now. See the [src/corpora.rs](https://github.com/kevingimbel/fakedata_generator/blob/master/src/corpora.rs) file for all available sets.
///
/// In addition TLDs are generated from <https://data.iana.org/TLD/tlds-alpha-by-domain.txt>
///
/// Possible input values:
///  - `cats`
/// - `dinosaurs`
/// - `dogs`
/// - `donkeys`
/// - `horses`
/// - `tv_shows`
/// - `fruits`
/// - `pizzatoppings`
/// - `herbs`
/// - `spices`
/// - `mixtures`
/// - `teas`
/// - `vegetables`
/// - `rpgs`
/// - `board_games`
/// - `wrestling_moves`
/// - `countries`
/// - `nationalities`
/// - `governmentforms`
/// - `authors`
/// - `bodyparts`
/// - `firstnames`
/// - `lastnames`
/// - `moods`
/// - `neutralnames`
/// - `occupations`
/// - `prefixes`
/// - `scientists`
/// - `suffixes`
/// - `tolkien_character_names`
/// - `fabrics`
/// - `gemstones`
/// - `metals`
/// - `packaging`
/// - `music_genres`
/// - `objects`
/// - `flowers`
/// - `toxic_plants`
/// - `weather_conditions`
/// - `sports`
/// - `appliances`
/// - `new_technologies`
/// - `programming_languages`
///
/// Each of these will return a random word from the list.
///
/// ## Example
/// ```rust
/// use fakedata_generator::data::gen_switch;
/// let horse: String = gen_switch("horse".to_string());
/// let gem: String = gen_switch("gemstone".to_string());
/// // horse = Appaloosa
/// // gem = emerald
/// ```
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();
}

// gen_corpora_switch is deprecated and should not be used, use `gen_switch` instead.
// `gen_corpora_switch` may be removed in a future release.
pub fn gen_corpora_switch(name: String) -> String {
    return gen_switch(name);
}

// gen_prime returns a random of the first 1000 prime numbers
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()
}