Skip to content

Commit

Permalink
Lightweight error value in TryFrom<i32> for enums (#1010)
Browse files Browse the repository at this point in the history
Add a new UnknownEnumValue error type to prost,
change the TryFrom<i32> conversions generated for enums to use that
as the associated Error type. The error value carries the original
integer, making it also more informative than DecodeError.
  • Loading branch information
mzabaluev authored May 27, 2024
1 parent 683091a commit 4a7bba0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
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

0 comments on commit 4a7bba0

Please sign in to comment.