2018-08-07 02:58:07 +00:00
|
|
|
#![feature(plugin, decl_macro)]
|
2016-09-29 02:31:26 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
|
|
|
|
2018-04-12 23:07:37 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2016-09-29 02:31:26 +00:00
|
|
|
|
2017-03-31 06:06:53 +00:00
|
|
|
use std::io;
|
2017-06-03 01:46:31 +00:00
|
|
|
|
2018-04-12 23:07:37 +00:00
|
|
|
use rocket::request::Form;
|
2016-09-29 02:31:26 +00:00
|
|
|
use rocket::response::NamedFile;
|
2017-03-31 06:06:53 +00:00
|
|
|
use rocket::http::RawStr;
|
2016-09-29 02:31:26 +00:00
|
|
|
|
2017-06-03 01:46:31 +00:00
|
|
|
#[cfg(test)] mod tests;
|
|
|
|
|
2018-04-12 23:07:37 +00:00
|
|
|
#[derive(Debug, FromFormValue)]
|
2016-09-29 02:31:26 +00:00
|
|
|
enum FormOption {
|
|
|
|
A, B, C
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, FromForm)]
|
2017-06-06 20:41:04 +00:00
|
|
|
struct FormInput<'r> {
|
2016-09-29 02:31:26 +00:00
|
|
|
checkbox: bool,
|
|
|
|
number: usize,
|
2017-04-04 02:06:30 +00:00
|
|
|
#[form(field = "type")]
|
2016-09-29 02:31:26 +00:00
|
|
|
radio: FormOption,
|
2017-06-06 20:41:04 +00:00
|
|
|
password: &'r RawStr,
|
2017-04-04 02:06:30 +00:00
|
|
|
#[form(field = "textarea")]
|
|
|
|
text_area: String,
|
2016-09-29 02:31:26 +00:00
|
|
|
select: FormOption,
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
#[post("/", data = "<sink>")]
|
2017-06-06 20:41:04 +00:00
|
|
|
fn sink<'r>(sink: Result<Form<'r, FormInput<'r>>, Option<String>>) -> String {
|
2016-10-12 07:14:42 +00:00
|
|
|
match sink {
|
|
|
|
Ok(form) => format!("{:?}", form.get()),
|
|
|
|
Err(Some(f)) => format!("Invalid form input: {}", f),
|
|
|
|
Err(None) => format!("Form input was invalid UTF8."),
|
|
|
|
}
|
2016-09-29 02:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn index() -> io::Result<NamedFile> {
|
|
|
|
NamedFile::open("static/index.html")
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:46:31 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite().mount("/", routes![index, sink])
|
|
|
|
}
|
|
|
|
|
2016-09-29 02:31:26 +00:00
|
|
|
fn main() {
|
2017-06-03 01:46:31 +00:00
|
|
|
rocket().launch();
|
2016-09-29 02:31:26 +00:00
|
|
|
}
|