Skip to content

Instantly share code, notes, and snippets.

@goyox86
Created March 16, 2017 13:20
Show Gist options
  • Save goyox86/cdd16fa761030cf08d8862cae1d80399 to your computer and use it in GitHub Desktop.
Save goyox86/cdd16fa761030cf08d8862cae1d80399 to your computer and use it in GitHub Desktop.
#[derive(Serialize, Deserialize, Insertable)]
#[table_name="users"]
pub struct NewUser {
pub name: String,
pub username: String,
pub email: String,
#[serde(rename(deserialize = "password"))]
#[serde(deserialize_with = "hash_user_password")]
pub hashed_password: String
}
fn hash_user_password<D>(deserializer: D) -> Result<String, D::Error>
where D: Deserializer
{
let password: String = Deserialize::deserialize(deserializer)?;
hash(&password, DEFAULT_COST)
.map_err(de::Error::custom)
.map(|hashed_password|hashed_password)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment