Skip to content

Commit 8044ea3

Browse files
committed
style: Simplify DecodeError description to an enum
Replace the inner `description` field of `DecodeError` from a string to an enum. The enum implements `Display` that produces the same string.
1 parent 03fdede commit 8044ea3

File tree

6 files changed

+124
-55
lines changed

6 files changed

+124
-55
lines changed

prost-types/src/any.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,21 @@ impl Any {
1919
M: Default + Name + Sized,
2020
{
2121
let expected_type_url = M::type_url();
22+
let actual_type_url = &self.type_url;
2223

2324
if let (Some(expected), Some(actual)) = (
2425
TypeUrl::new(&expected_type_url),
25-
TypeUrl::new(&self.type_url),
26+
TypeUrl::new(actual_type_url),
2627
) {
2728
if expected == actual {
2829
return M::decode(self.value.as_slice());
2930
}
3031
}
3132

32-
let mut err = DecodeError::new(format!(
33-
"expected type URL: \"{}\" (got: \"{}\")",
34-
expected_type_url, &self.type_url
35-
));
36-
err.push("unexpected type URL", "type_url");
37-
Err(err)
33+
Err(DecodeError::new_unexpected_type_url(
34+
actual_type_url,
35+
expected_type_url,
36+
))
3837
}
3938
}
4039

prost/src/encoding.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//! This module is `pub`, but is only for prost internal use. The `prost-derive` crate needs access for its `Message` implementations.
77
88
use alloc::collections::BTreeMap;
9-
use alloc::format;
109
use alloc::string::String;
1110
use alloc::vec::Vec;
1211
use core::mem;
1312
use core::str;
1413

1514
use ::bytes::{Buf, BufMut, Bytes};
1615

16+
use crate::error::DecodeErrorKind;
1717
use crate::DecodeError;
1818
use crate::Message;
1919

@@ -84,7 +84,7 @@ impl DecodeContext {
8484
#[inline]
8585
pub(crate) fn limit_reached(&self) -> Result<(), DecodeError> {
8686
if self.recurse_count == 0 {
87-
Err(DecodeError::new("recursion limit reached"))
87+
Err(DecodeErrorKind::RecursionLimitReached.into())
8888
} else {
8989
Ok(())
9090
}
@@ -115,13 +115,13 @@ pub fn encode_key(tag: u32, wire_type: WireType, buf: &mut impl BufMut) {
115115
pub fn decode_key(buf: &mut impl Buf) -> Result<(u32, WireType), DecodeError> {
116116
let key = decode_varint(buf)?;
117117
if key > u64::from(u32::MAX) {
118-
return Err(DecodeError::new(format!("invalid key value: {key}")));
118+
return Err(DecodeErrorKind::InvalidKey { key }.into());
119119
}
120120
let wire_type = WireType::try_from(key & 0x07)?;
121121
let tag = key as u32 >> 3;
122122

123123
if tag < MIN_TAG {
124-
return Err(DecodeError::new("invalid tag value: 0"));
124+
return Err(DecodeErrorKind::InvalidTag.into());
125125
}
126126

127127
Ok((tag, wire_type))
@@ -149,7 +149,7 @@ where
149149
let len = decode_varint(buf)?;
150150
let remaining = buf.remaining();
151151
if len > remaining as u64 {
152-
return Err(DecodeError::new("buffer underflow"));
152+
return Err(DecodeErrorKind::BufferUnderflow.into());
153153
}
154154

155155
let limit = remaining - len as usize;
@@ -158,7 +158,7 @@ where
158158
}
159159

160160
if buf.remaining() != limit {
161-
return Err(DecodeError::new("delimited length exceeded"));
161+
return Err(DecodeErrorKind::DelimitedLengthExceeded.into());
162162
}
163163
Ok(())
164164
}
@@ -180,18 +180,18 @@ pub fn skip_field(
180180
match inner_wire_type {
181181
WireType::EndGroup => {
182182
if inner_tag != tag {
183-
return Err(DecodeError::new("unexpected end group tag"));
183+
return Err(DecodeErrorKind::UnexpectedEndGroupTag.into());
184184
}
185185
break 0;
186186
}
187187
_ => skip_field(inner_wire_type, inner_tag, buf, ctx.enter_recursion())?,
188188
}
189189
},
190-
WireType::EndGroup => return Err(DecodeError::new("unexpected end group tag")),
190+
WireType::EndGroup => return Err(DecodeErrorKind::UnexpectedEndGroupTag.into()),
191191
};
192192

193193
if len > buf.remaining() as u64 {
194-
return Err(DecodeError::new("buffer underflow"));
194+
return Err(DecodeErrorKind::BufferUnderflow.into());
195195
}
196196

197197
buf.advance(len as usize);
@@ -396,7 +396,7 @@ macro_rules! fixed_width {
396396
) -> Result<(), DecodeError> {
397397
check_wire_type($wire_type, wire_type)?;
398398
if buf.remaining() < $width {
399-
return Err(DecodeError::new("buffer underflow"));
399+
return Err(DecodeErrorKind::BufferUnderflow.into());
400400
}
401401
*value = buf.$get();
402402
Ok(())
@@ -599,9 +599,7 @@ pub mod string {
599599
mem::forget(drop_guard);
600600
Ok(())
601601
}
602-
Err(_) => Err(DecodeError::new(
603-
"invalid string value: data is not UTF-8 encoded",
604-
)),
602+
Err(_) => Err(DecodeErrorKind::InvalidString.into()),
605603
}
606604
}
607605
}
@@ -686,6 +684,8 @@ impl sealed::BytesAdapter for Vec<u8> {
686684
}
687685

688686
pub mod bytes {
687+
use crate::error::DecodeErrorKind;
688+
689689
use super::*;
690690

691691
pub fn encode(tag: u32, value: &impl BytesAdapter, buf: &mut impl BufMut) {
@@ -703,7 +703,7 @@ pub mod bytes {
703703
check_wire_type(WireType::LengthDelimited, wire_type)?;
704704
let len = decode_varint(buf)?;
705705
if len > buf.remaining() as u64 {
706-
return Err(DecodeError::new("buffer underflow"));
706+
return Err(DecodeErrorKind::BufferUnderflow.into());
707707
}
708708
let len = len as usize;
709709

@@ -732,7 +732,7 @@ pub mod bytes {
732732
check_wire_type(WireType::LengthDelimited, wire_type)?;
733733
let len = decode_varint(buf)?;
734734
if len > buf.remaining() as u64 {
735-
return Err(DecodeError::new("buffer underflow"));
735+
return Err(DecodeErrorKind::BufferUnderflow.into());
736736
}
737737
let len = len as usize;
738738

@@ -866,6 +866,8 @@ pub mod message {
866866
}
867867

868868
pub mod group {
869+
use crate::error::DecodeErrorKind;
870+
869871
use super::*;
870872

871873
pub fn encode<M>(tag: u32, msg: &M, buf: &mut impl BufMut)
@@ -894,7 +896,7 @@ pub mod group {
894896
let (field_tag, field_wire_type) = decode_key(buf)?;
895897
if field_wire_type == WireType::EndGroup {
896898
if field_tag != tag {
897-
return Err(DecodeError::new("unexpected end group tag"));
899+
return Err(DecodeErrorKind::UnexpectedEndGroupTag.into());
898900
}
899901
return Ok(());
900902
}

prost/src/encoding/length_delimiter.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::DecodeErrorKind;
12
pub use crate::error::{DecodeError, EncodeError, UnknownEnumValue};
23
pub use crate::message::Message;
34
pub use crate::name::Name;
@@ -45,9 +46,7 @@ pub fn length_delimiter_len(length: usize) -> usize {
4546
pub fn decode_length_delimiter(mut buf: impl Buf) -> Result<usize, DecodeError> {
4647
let length = decode_varint(&mut buf)?;
4748
if length > usize::MAX as u64 {
48-
return Err(DecodeError::new(
49-
"length delimiter exceeds maximum usize value",
50-
));
49+
return Err(DecodeErrorKind::LengthDelimiterTooLarge.into());
5150
}
5251
Ok(length as usize)
5352
}

prost/src/encoding/varint.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::num::NonZeroU64;
33

44
use ::bytes::{Buf, BufMut};
55

6-
use crate::DecodeError;
6+
use crate::{error::DecodeErrorKind, DecodeError};
77

88
/// Encodes an integer value into LEB128 variable length format, and writes it to the buffer.
99
/// The buffer must have enough remaining space (maximum 10 bytes).
@@ -38,7 +38,7 @@ pub fn decode_varint(buf: &mut impl Buf) -> Result<u64, DecodeError> {
3838
let bytes = buf.chunk();
3939
let len = bytes.len();
4040
if len == 0 {
41-
return Err(DecodeError::new("invalid varint"));
41+
return Err(DecodeErrorKind::InvalidVarint.into());
4242
}
4343

4444
let byte = bytes[0];
@@ -143,7 +143,7 @@ fn decode_varint_slice(bytes: &[u8]) -> Result<(u64, usize), DecodeError> {
143143

144144
// We have overrun the maximum size of a varint (10 bytes) or the final byte caused an overflow.
145145
// Assume the data is corrupt.
146-
Err(DecodeError::new("invalid varint"))
146+
Err(DecodeErrorKind::InvalidVarint.into())
147147
}
148148

149149
/// Decodes a LEB128-encoded variable length integer from the buffer, advancing the buffer as
@@ -163,14 +163,14 @@ fn decode_varint_slow(buf: &mut impl Buf) -> Result<u64, DecodeError> {
163163
// Check for u64::MAX overflow. See [`ConsumeVarint`][1] for details.
164164
// [1]: https://github.com/protocolbuffers/protobuf-go/blob/v1.27.1/encoding/protowire/wire.go#L358
165165
if count == 9 && byte >= 0x02 {
166-
return Err(DecodeError::new("invalid varint"));
166+
return Err(DecodeErrorKind::InvalidVarint.into());
167167
} else {
168168
return Ok(value);
169169
}
170170
}
171171
}
172172

173-
Err(DecodeError::new("invalid varint"))
173+
Err(DecodeErrorKind::InvalidVarint.into())
174174
}
175175

176176
#[cfg(test)]

prost/src/encoding/wire_type.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::DecodeError;
2-
use alloc::format;
1+
use crate::{error::DecodeErrorKind, DecodeError};
32

43
/// Represent the wire type for protobuf encoding.
54
///
@@ -27,9 +26,7 @@ impl TryFrom<u64> for WireType {
2726
3 => Ok(WireType::StartGroup),
2827
4 => Ok(WireType::EndGroup),
2928
5 => Ok(WireType::ThirtyTwoBit),
30-
_ => Err(DecodeError::new(format!(
31-
"invalid wire type value: {value}"
32-
))),
29+
_ => Err(DecodeErrorKind::InvalidWireType { value }.into()),
3330
}
3431
}
3532
}
@@ -39,9 +36,7 @@ impl TryFrom<u64> for WireType {
3936
#[inline]
4037
pub fn check_wire_type(expected: WireType, actual: WireType) -> Result<(), DecodeError> {
4138
if expected != actual {
42-
return Err(DecodeError::new(format!(
43-
"invalid wire type: {actual:?} (expected {expected:?})"
44-
)));
39+
return Err(DecodeErrorKind::UnexpectedWireType { actual, expected }.into());
4540
}
4641
Ok(())
4742
}

0 commit comments

Comments
 (0)