Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A more lightweight, informative error value in TryFrom<i32> for enums #1010

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions prost-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ fn try_enumeration(input: TokenStream) -> Result<TokenStream, Error> {
}

impl #impl_generics ::core::convert::TryFrom::<i32> for #ident #ty_generics #where_clause {
type Error = ::prost::DecodeError;
type Error = ::prost::UnknownEnumValue;

fn try_from(value: i32) -> ::core::result::Result<#ident, ::prost::DecodeError> {
fn try_from(value: i32) -> ::core::result::Result<#ident, Self::Error> {
match value {
#(#try_from,)*
_ => ::core::result::Result::Err(::prost::DecodeError::new("invalid enumeration value")),
_ => ::core::result::Result::Err(::prost::UnknownEnumValue(value)),
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions prost/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,20 @@ impl From<EncodeError> for std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidInput, error)
}
}

/// An error indicating that an unknown enumeration value was encountered.
///
/// The Protobuf spec mandates that enumeration value sets are ‘open’, so this
/// error's value represents an integer value unrecognized by the
/// presently used enum definition.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct UnknownEnumValue(pub i32);

impl fmt::Display for UnknownEnumValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "unknown enumeration value {}", self.0)
}
}

#[cfg(feature = "std")]
impl std::error::Error for UnknownEnumValue {}
2 changes: 1 addition & 1 deletion prost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod types;
#[doc(hidden)]
pub mod encoding;

pub use crate::error::{DecodeError, EncodeError};
pub use crate::error::{DecodeError, EncodeError, UnknownEnumValue};
pub use crate::message::Message;
pub use crate::name::Name;

Expand Down
5 changes: 1 addition & 4 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,7 @@ mod tests {
Ok(PrivacyLevel::PrivacyLevelprivacyLevelFour),
PrivacyLevel::try_from(4)
);
assert_eq!(
Err(prost::DecodeError::new("invalid enumeration value")),
PrivacyLevel::try_from(5)
);
assert_eq!(Err(prost::UnknownEnumValue(5)), PrivacyLevel::try_from(5));

assert_eq!(
Ok(ERemoteClientBroadcastMsg::KERemoteClientBroadcastMsgDiscovery),
Expand Down