Skip to content

Instantly share code, notes, and snippets.

@SkylerLipthay
Created February 1, 2016 07:08
Show Gist options
  • Save SkylerLipthay/d59f04158376b59f0240 to your computer and use it in GitHub Desktop.
Save SkylerLipthay/d59f04158376b59f0240 to your computer and use it in GitHub Desktop.
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