Slightly cleaner form exmaple.

This commit is contained in:
Sergio Benitez 2016-04-04 19:02:51 -07:00
parent 2dbcfa10e3
commit 0041724aa4
1 changed files with 8 additions and 8 deletions

View File

@ -8,11 +8,6 @@ mod files;
use rocket::Rocket; use rocket::Rocket;
use rocket::response::Redirect; use rocket::response::Redirect;
#[route(GET, path = "/user/<username>")]
fn user_page(username: &str) -> String {
format!("This is {}'s page.", username)
}
#[derive(FromForm)] #[derive(FromForm)]
struct UserLogin<'r> { struct UserLogin<'r> {
username: &'r str, username: &'r str,
@ -20,7 +15,6 @@ struct UserLogin<'r> {
age: Result<isize, &'r str>, age: Result<isize, &'r str>,
} }
// FIXME: fn login<'a>(user: UserLogin<'a>)
#[route(POST, path = "/login", form = "<user>")] #[route(POST, path = "/login", form = "<user>")]
fn login(user: UserLogin) -> Result<Redirect, String> { fn login(user: UserLogin) -> Result<Redirect, String> {
if user.age.is_err() { if user.age.is_err() {
@ -28,8 +22,9 @@ fn login(user: UserLogin) -> Result<Redirect, String> {
return Err(format!("'{}' is not a valid age integer.", input)); return Err(format!("'{}' is not a valid age integer.", input));
} }
if user.age.unwrap() < 20 { let age = user.age.unwrap();
return Err(format!("Sorry, {} is too young!", user.age.unwrap())); if age < 20 {
return Err(format!("Sorry, {} is too young!", age));
} }
match user.username { match user.username {
@ -41,6 +36,11 @@ fn login(user: UserLogin) -> Result<Redirect, String> {
} }
} }
#[route(GET, path = "/user/<username>")]
fn user_page(username: &str) -> String {
format!("This is {}'s page.", username)
}
fn main() { fn main() {
let mut rocket = Rocket::new("localhost", 8000); let mut rocket = Rocket::new("localhost", 8000);
rocket.mount("/", routes![files::index, files::files, user_page, login]); rocket.mount("/", routes![files::index, files::files, user_page, login]);