Rocket/examples/form_kitchen_sink/src/main.rs

52 lines
1.0 KiB
Rust

#![feature(plugin, decl_macro)]
#![plugin(rocket_codegen)]
#[macro_use] extern crate rocket;
use std::io;
use rocket::request::Form;
use rocket::response::NamedFile;
use rocket::http::RawStr;
#[cfg(test)] mod tests;
#[derive(Debug, FromFormValue)]
enum FormOption {
A, B, C
}
#[derive(Debug, FromForm)]
struct FormInput<'r> {
checkbox: bool,
number: usize,
#[form(field = "type")]
radio: FormOption,
password: &'r RawStr,
#[form(field = "textarea")]
text_area: String,
select: FormOption,
}
#[post("/", data = "<sink>")]
fn sink<'r>(sink: Result<Form<'r, FormInput<'r>>, Option<String>>) -> String {
match sink {
Ok(form) => format!("{:?}", form.get()),
Err(Some(f)) => format!("Invalid form input: {}", f),
Err(None) => format!("Form input was invalid UTF8."),
}
}
#[get("/")]
fn index() -> io::Result<NamedFile> {
NamedFile::open("static/index.html")
}
fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", routes![index, sink])
}
fn main() {
rocket().launch();
}