From 421540acf80bb54f5a305045b629c5ca1e7318fa Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Thu, 11 Dec 2025 14:12:54 -0700 Subject: [PATCH] password-hash: impl `From` for `Error` This is useful within the impls of traits like `PasswordHasher` where the return type is `password_hash::Result` but various `phc::Error`s may occur when constructing the `PasswordHash` from the params, salt, and output --- password-hash/src/error.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/password-hash/src/error.rs b/password-hash/src/error.rs index d62294325..765188305 100644 --- a/password-hash/src/error.rs +++ b/password-hash/src/error.rs @@ -65,3 +65,22 @@ impl fmt::Display for Error { } impl core::error::Error for Error {} + +#[cfg(feature = "phc")] +impl From for Error { + fn from(err: phc::Error) -> Self { + match err { + phc::Error::B64Encoding(_) | phc::Error::MissingField | phc::Error::TrailingData => { + Self::EncodingInvalid + } + phc::Error::OutputSize { .. } => Self::OutputSize, + phc::Error::ParamNameDuplicated + | phc::Error::ParamNameInvalid + | phc::Error::ParamValueTooLong + | phc::Error::ParamsMaxExceeded + | phc::Error::ValueTooLong => Self::ParamsInvalid, + phc::Error::SaltTooShort | phc::Error::SaltTooLong => Self::SaltInvalid, + _ => Self::Internal, // Branch since `phc::Error` is `non_exhaustive` + } + } +}