Rocket/examples/extended_validation/src/main.rs

90 lines
2.0 KiB
Rust
Raw Normal View History

#![feature(plugin, custom_derive)]
2016-09-09 03:38:58 +00:00
#![plugin(rocket_codegen)]
extern crate rocket;
mod files;
#[cfg(test)]
mod tests;
use rocket::response::Redirect;
use rocket::request::{Form, FromFormValue};
#[derive(Debug)]
struct StrongPassword<'r>(&'r str);
#[derive(Debug)]
struct AdultAge(isize);
#[derive(FromForm)]
struct UserLogin<'r> {
username: &'r str,
password: Result<StrongPassword<'r>, &'static str>,
age: Result<AdultAge, &'static str>,
}
impl<'v> FromFormValue<'v> for StrongPassword<'v> {
type Error = &'static str;
fn from_form_value(v: &'v str) -> Result<Self, Self::Error> {
if v.len() < 8 {
Err("Too short!")
} else {
Ok(StrongPassword(v))
}
}
}
impl<'v> FromFormValue<'v> for AdultAge {
type Error = &'static str;
fn from_form_value(v: &'v str) -> Result<Self, Self::Error> {
let age = match isize::from_form_value(v) {
Ok(v) => v,
Err(_) => return Err("Age value is not a number."),
};
match age > 20 {
true => Ok(AdultAge(age)),
false => Err("Must be at least 21."),
}
}
}
#[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));
}
if let Err(e) = user.password {
return Err(format!("Password is invalid: {}", e));
}
if user.username == "Sergio" {
if let Ok(StrongPassword("password")) = user.password {
2016-11-03 18:09:08 +00:00
Ok(Redirect::to("/user/Sergio"))
} else {
Err("Wrong password!".to_string())
}
} else {
Err(format!("Unrecognized user, '{}'.", user.username))
}
}
2016-09-04 11:06:28 +00:00
#[get("/user/<username>")]
fn user_page(username: &str) -> String {
format!("This is {}'s page.", username)
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![files::index, files::files, user_page, login])
}
fn main() {
rocket().launch()
}