Skip to content

Commit 94ae5be

Browse files
authored
password-hash: impl From<phc::Error> for Error (#2124)
This is useful within the impls of traits like `PasswordHasher` where the return type is `password_hash::Result<phc::PasswordHash>` but various `phc::Error`s may occur when constructing the `PasswordHash` from the params, salt, and output
1 parent c45480e commit 94ae5be

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

password-hash/src/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,22 @@ impl fmt::Display for Error {
6565
}
6666

6767
impl core::error::Error for Error {}
68+
69+
#[cfg(feature = "phc")]
70+
impl From<phc::Error> for Error {
71+
fn from(err: phc::Error) -> Self {
72+
match err {
73+
phc::Error::B64Encoding(_) | phc::Error::MissingField | phc::Error::TrailingData => {
74+
Self::EncodingInvalid
75+
}
76+
phc::Error::OutputSize { .. } => Self::OutputSize,
77+
phc::Error::ParamNameDuplicated
78+
| phc::Error::ParamNameInvalid
79+
| phc::Error::ParamValueTooLong
80+
| phc::Error::ParamsMaxExceeded
81+
| phc::Error::ValueTooLong => Self::ParamsInvalid,
82+
phc::Error::SaltTooShort | phc::Error::SaltTooLong => Self::SaltInvalid,
83+
_ => Self::Internal, // Branch since `phc::Error` is `non_exhaustive`
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)