2017-08-29 03:14:59 +00:00
|
|
|
#![feature(plugin, decl_macro, custom_derive)]
|
2016-09-09 03:38:58 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
2016-07-16 04:09:08 +00:00
|
|
|
|
|
|
|
extern crate rocket;
|
|
|
|
|
|
|
|
mod files;
|
2017-03-31 06:06:53 +00:00
|
|
|
#[cfg(test)] mod tests;
|
2016-07-16 04:09:08 +00:00
|
|
|
|
|
|
|
use rocket::response::Redirect;
|
2016-10-12 07:14:42 +00:00
|
|
|
use rocket::request::{Form, FromFormValue};
|
2017-03-31 06:06:53 +00:00
|
|
|
use rocket::http::RawStr;
|
2016-07-16 04:09:08 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct StrongPassword<'r>(&'r str);
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct AdultAge(isize);
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct UserLogin<'r> {
|
2017-03-31 06:06:53 +00:00
|
|
|
username: &'r RawStr,
|
2016-08-02 02:47:21 +00:00
|
|
|
password: Result<StrongPassword<'r>, &'static str>,
|
|
|
|
age: Result<AdultAge, &'static str>,
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'v> FromFormValue<'v> for StrongPassword<'v> {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
2017-03-31 06:06:53 +00:00
|
|
|
fn from_form_value(v: &'v RawStr) -> Result<Self, Self::Error> {
|
2016-07-16 04:09:08 +00:00
|
|
|
if v.len() < 8 {
|
2017-03-31 06:06:53 +00:00
|
|
|
Err("too short!")
|
2016-07-16 04:09:08 +00:00
|
|
|
} else {
|
2017-03-31 06:06:53 +00:00
|
|
|
Ok(StrongPassword(v.as_str()))
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'v> FromFormValue<'v> for AdultAge {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
2017-03-31 06:06:53 +00:00
|
|
|
fn from_form_value(v: &'v RawStr) -> Result<Self, Self::Error> {
|
2016-09-30 08:25:07 +00:00
|
|
|
let age = match isize::from_form_value(v) {
|
2016-07-16 04:09:08 +00:00
|
|
|
Ok(v) => v,
|
2017-03-31 06:06:53 +00:00
|
|
|
Err(_) => return Err("value is not a number."),
|
2016-07-16 04:09:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match age > 20 {
|
|
|
|
true => Ok(AdultAge(age)),
|
2017-03-31 06:06:53 +00:00
|
|
|
false => Err("must be at least 21."),
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
#[post("/login", data = "<user_form>")]
|
|
|
|
fn login<'a>(user_form: Form<'a, UserLogin<'a>>) -> Result<Redirect, String> {
|
|
|
|
let user = user_form.get();
|
|
|
|
|
|
|
|
if let Err(e) = user.age {
|
|
|
|
return Err(format!("Age is invalid: {}", e));
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
if let Err(e) = user.password {
|
|
|
|
return Err(format!("Password is invalid: {}", e));
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
if user.username == "Sergio" {
|
|
|
|
if let Ok(StrongPassword("password")) = user.password {
|
2016-11-03 18:09:08 +00:00
|
|
|
Ok(Redirect::to("/user/Sergio"))
|
2016-10-12 07:14:42 +00:00
|
|
|
} else {
|
|
|
|
Err("Wrong password!".to_string())
|
2016-08-02 02:47:21 +00:00
|
|
|
}
|
2016-10-12 07:14:42 +00:00
|
|
|
} else {
|
|
|
|
Err(format!("Unrecognized user, '{}'.", user.username))
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 11:06:28 +00:00
|
|
|
#[get("/user/<username>")]
|
2017-03-31 07:18:58 +00:00
|
|
|
fn user_page(username: &RawStr) -> String {
|
2016-07-16 04:09:08 +00:00
|
|
|
format!("This is {}'s page.", username)
|
|
|
|
}
|
|
|
|
|
2017-02-17 01:22:23 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", routes![files::index, files::files, user_page, login])
|
2017-02-17 01:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2017-03-16 05:10:09 +00:00
|
|
|
rocket().launch();
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|