Created
February 1, 2016 07:08
-
-
Save SkylerLipthay/d59f04158376b59f0240 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub enum FormError { | |
UniqueViolation, | |
LengthRange(u32, u32), | |
} | |
// impl MyError for FormError { ... } | |
#[derive(Deserialize)] | |
pub struct SignupData { | |
username: String, | |
password: String, | |
} | |
impl SignupData { | |
// In actual implementation, map keys could be interned strings or enum variants. | |
pub fn validate(&self, conn: &GenericConnection) -> MyResult<BTreeMap<String, FormError>> { | |
let mut map = BTreeMap::new(); | |
let n = try!(conn.execute("SELECT 1 FROM users WHERE username = $1;", &[&self.username])); | |
if n != 0 { | |
map.insert("username", FormError::UniqueViolation); | |
} | |
let n = self.password.len(); | |
if n < 8 || n > 32 { | |
map.insert("password", FormError::LengthRange(8, 32)); | |
} | |
map | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment