~jplatte/turbo.fish

377fe15288c9495ce5d4ce5da1f301c718dbea52 — Jonas Platte 3 years ago f7eeed5
Add /random, where you get a random turbofish!
3 files changed, 38 insertions(+), 2 deletions(-)

M Cargo.toml
M src/main.rs
A src/random.rs
M Cargo.toml => Cargo.toml +1 -0
@@ 4,6 4,7 @@ name = "turbofish"
version = "0.1.0"

[dependencies]
rand = "0.3.17"
rocket = "0.3.3"
rocket_codegen = "0.3.3"


M src/main.rs => src/main.rs +11 -2
@@ 1,17 1,21 @@
#![feature(non_ascii_idents, plugin)]
#![plugin(rocket_codegen)]

extern crate rand;
extern crate rocket;
extern crate rocket_contrib;

mod turbofish;
mod random;

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use rocket::response::NamedFile;
use rocket::http::uri::URI;
use rocket::response::{NamedFile, Redirect};
use rocket_contrib::Template;

use random::random_type;
use turbofish::TurboFish;

#[get("/")]


@@ 22,6 26,11 @@ fn index() -> Template {
    Template::render("turbofish", context)
}

#[get("/random")]
fn random() -> Redirect {
    Redirect::to(&URI::percent_encode(&format!("::<{}>", random_type())))
}

#[get("/<turbofish>")]
fn turbofish(turbofish: TurboFish) -> Template {
    let mut context = HashMap::new();


@@ 39,7 48,7 @@ fn files(file: PathBuf) -> Option<NamedFile> {
fn main() {
    // TODO: Custom 404
    rocket::ignite()
        .mount("/", routes![index, turbofish, files])
        .mount("/", routes![index, random, turbofish, files])
        .attach(Template::fairing())
        .launch();
}

A src/random.rs => src/random.rs +26 -0
@@ 0,0 1,26 @@
use rand::{self, Rng};

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

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