Rust isn't subtyping lifetimes correctly, so we'll appease it.

This commit is contained in:
Sergio Benitez 2016-08-01 19:47:21 -07:00
parent 578b50b1f9
commit 37cbceff67
1 changed files with 11 additions and 9 deletions

View File

@ -18,8 +18,8 @@ struct AdultAge(isize);
#[derive(FromForm)] #[derive(FromForm)]
struct UserLogin<'r> { struct UserLogin<'r> {
username: &'r str, username: &'r str,
password: Result<StrongPassword<'r>, &'r str>, password: Result<StrongPassword<'r>, &'static str>,
age: Result<AdultAge, &'r str>, age: Result<AdultAge, &'static str>,
} }
impl<'v> FromFormValue<'v> for StrongPassword<'v> { impl<'v> FromFormValue<'v> for StrongPassword<'v> {
@ -40,12 +40,12 @@ impl<'v> FromFormValue<'v> for AdultAge {
fn parse(v: &'v str) -> Result<Self, Self::Error> { fn parse(v: &'v str) -> Result<Self, Self::Error> {
let age = match isize::parse(v) { let age = match isize::parse(v) {
Ok(v) => v, Ok(v) => v,
Err(_) => return Err("Age value is not a number.") Err(_) => return Err("Age value is not a number."),
}; };
match age > 20 { match age > 20 {
true => Ok(AdultAge(age)), true => Ok(AdultAge(age)),
false => Err("Must be at least 21.") false => Err("Must be at least 21."),
} }
} }
} }
@ -61,11 +61,13 @@ fn login(user: UserLogin) -> Result<Redirect, String> {
} }
match user.username { match user.username {
"Sergio" => match user.password.unwrap().0 { "Sergio" => {
"password" => Ok(Redirect::other("/user/Sergio")), match user.password.unwrap().0 {
_ => Err("Wrong password!".to_string()) "password" => Ok(Redirect::other("/user/Sergio")),
}, _ => Err("Wrong password!".to_string()),
_ => Err(format!("Unrecognized user, '{}'.", user.username)) }
}
_ => Err(format!("Unrecognized user, '{}'.", user.username)),
} }
} }