From 6fe2f513327367fe31ef8a5420dde7941b6781d7 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Wed, 28 Sep 2016 19:31:26 -0700 Subject: [PATCH] Add form kitchen sink example. --- Cargo.toml | 2 +- examples/form_kitchen_sink/Cargo.toml | 9 +++ examples/form_kitchen_sink/src/main.rs | 62 ++++++++++++++++++++ examples/form_kitchen_sink/static/index.html | 49 ++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 examples/form_kitchen_sink/Cargo.toml create mode 100644 examples/form_kitchen_sink/src/main.rs create mode 100644 examples/form_kitchen_sink/static/index.html diff --git a/Cargo.toml b/Cargo.toml index a5fa5dc2..921cb4b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,5 +23,5 @@ members = [ "examples/stream", "examples/json", "examples/handlebars_templates", - "examples/form_types", + "examples/form_kitchen_sink", ] diff --git a/examples/form_kitchen_sink/Cargo.toml b/examples/form_kitchen_sink/Cargo.toml new file mode 100644 index 00000000..26d18b25 --- /dev/null +++ b/examples/form_kitchen_sink/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "form_kitchen_sink" +version = "0.0.1" +authors = ["Sergio Benitez "] +workspace = "../../" + +[dependencies] +rocket = { path = "../../lib" } +rocket_codegen = { path = "../../codegen" } diff --git a/examples/form_kitchen_sink/src/main.rs b/examples/form_kitchen_sink/src/main.rs new file mode 100644 index 00000000..495955c8 --- /dev/null +++ b/examples/form_kitchen_sink/src/main.rs @@ -0,0 +1,62 @@ +#![feature(plugin, custom_derive)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +use rocket::{Rocket, Request}; +use rocket::response::NamedFile; +use rocket::form::FromFormValue; +use std::io; + +// TODO: Make deriving `FromForm` for this enum possible. +#[derive(Debug)] +enum FormOption { + A, B, C +} + +impl<'v> FromFormValue<'v> for FormOption { + type Error = &'v str; + + fn parse(v: &'v str) -> Result { + let variant = match v { + "a" => FormOption::A, + "b" => FormOption::B, + "c" => FormOption::C, + _ => return Err(v) + }; + + Ok(variant) + } +} + +#[derive(Debug, FromForm)] +struct FormInput<'r> { + checkbox: bool, + number: usize, + radio: FormOption, + password: &'r str, + textarea: String, + select: FormOption, +} + +#[post("/", form = "")] +fn sink(sink: FormInput) -> String { + format!("{:?}", sink) +} + +#[post("/", rank = 2)] +fn sink2(request: &Request) -> &'static str { + println!("form: {:?}", std::str::from_utf8(request.data.as_slice())); + "Sorry, the form is invalid." +} + +#[get("/")] +fn index() -> io::Result { + NamedFile::open("static/index.html") +} + +fn main() { + let mut rocket = Rocket::new("localhost", 8000); + rocket.mount("/", routes![index, sink, sink2]); + rocket.launch(); +} diff --git a/examples/form_kitchen_sink/static/index.html b/examples/form_kitchen_sink/static/index.html new file mode 100644 index 00000000..1ddd5b6c --- /dev/null +++ b/examples/form_kitchen_sink/static/index.html @@ -0,0 +1,49 @@ +

Rocket Form Kitchen Sink

+ +
+

+ +

+ + +

+ + +

+ + +

+ + +

+ + +

+