66//! This module is `pub`, but is only for prost internal use. The `prost-derive` crate needs access for its `Message` implementations.
77
88use alloc:: collections:: BTreeMap ;
9- use alloc:: format;
109use alloc:: string:: String ;
1110use alloc:: vec:: Vec ;
1211use core:: mem;
1312use core:: str;
1413
1514use :: bytes:: { Buf , BufMut , Bytes } ;
1615
16+ use crate :: error:: DecodeErrorKind ;
1717use crate :: DecodeError ;
1818use 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) {
115115pub 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
688686pub 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
868868pub 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 }
0 commit comments