Rocket/examples/forms/src/main.rs

54 lines
1.3 KiB
Rust
Raw Normal View History

#![feature(plugin)]
#![plugin(rocket_macros)]
extern crate rocket;
2016-03-30 08:02:21 +00:00
mod files;
use rocket::Rocket;
use rocket::response::Redirect;
use rocket::Error;
2016-03-30 08:02:21 +00:00
#[route(GET, path = "/user/<username>")]
fn user_page(username: &str) -> String {
format!("This is {}'s page.", username)
}
2016-03-30 08:02:21 +00:00
// #[derive(FormItem)] // FIXME: Make that happen.
struct UserLogin<'a> {
username: &'a str,
password: &'a str
}
2016-03-30 08:02:21 +00:00
trait FormItem: Sized {
fn from_form_string(s: &str) -> Result<Self, Error>;
}
impl<'a> FormItem for UserLogin<'a> {
fn from_form_string(s: &str) -> Result<Self, Error> {
Ok(UserLogin {
username: "this",
password: "that"
})
}
}
// TODO: Actually look at form parameters.
2016-03-30 08:02:21 +00:00
// FIXME: fn login<'a>(user: UserLogin<'a>)
#[route(POST, path = "/login", form = "<user>")]
fn login(user: UserLogin) -> Result<Redirect, String> {
match user.username {
"Sergio" => match user.password {
"password" => Ok(Redirect::other("/user/some_name")),
_ => Err("Wrong password!".to_string())
},
_ => Err(format!("Unrecognized user, '{}'.", user.username))
}
}
fn main() {
2016-03-30 08:02:21 +00:00
let mut rocket = Rocket::new("localhost", 8000);
rocket.mount("/", routes![files::index, files::files, user_page, login]);
rocket.launch();
}