~jplatte/turbo.fish

367fc649ab986aa3a8c8c7e53e0b3c451a21371a — loovjo 2 years ago c6d23c8
More exciting and exotic type

doing all testing on play.rust-lang.org probably wasn't the greatest idea

removed main function

Added type recursion depth limit
1 files changed, 52 insertions(+), 22 deletions(-)

M src/random.rs
M src/random.rs => src/random.rs +52 -22
@@ 1,27 1,57 @@
use rand::{self, Rng};

static TYPES: &[&str] = &[
    "bool",
    "char",
    "i8",
    "i16",
    "i32",
    "i64",
    "isize",
    "u8",
    "u16",
    "u32",
    "u64",
    "usize",
    "f32",
    "f64",
    "&str",
    "String",
    "Vec<_>",
    "HashSet<_>",
    "HashMap<_, _>",
const RECURSION_LIMIT: u8 = 1;

static TYPES: &[(&str, usize)] = &[
    ("_", 0),
    ("bool", 0),
    ("char", 0),
    ("i8", 0),
    ("i16", 0),
    ("i32", 0),
    ("i64", 0),
    ("isize", 0),
    ("u8", 0),
    ("u16", 0),
    ("u32", 0),
    ("u64", 0),
    ("usize", 0),
    ("f32", 0),
    ("f64", 0),
    ("&str", 0),
    ("String", 0),
    ("()", 0),
    ("Vec", 1),
    ("HashSet", 1),
    ("HashMap", 2),
    ("Box", 1),
    ("Result", 2),
];

pub fn random_type() -> &'static str {
    rand::thread_rng().choose(TYPES).unwrap()
pub fn random_type() -> String {
    random_type_depth(0)
}

fn random_type_depth(depth: u8) -> String {
    let (type_name, generics) = rand::thread_rng().choose(TYPES).unwrap();
    if *generics == 0 {
        type_name.to_string()
    } else {
        if depth == RECURSION_LIMIT {
            format!(
                "{}<{}>",
                type_name,
                (0..*generics).map(|_| "_").collect::<Vec<_>>().join(", ")
            )
        } else {
            format!(
                "{}<{}>",
                type_name,
                (0..*generics)
                    .map(|_| random_type_depth(depth + 1))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }
}