44
55use crate :: {
66 BytesRef , Choice , Decode , DecodeValue , DerOrd , EncodeValue , Error , ErrorKind , Header , Length ,
7- Reader , Result , SliceReader , Tag , Tagged , ValueOrd , Writer ,
7+ Reader , SliceReader , Tag , Tagged , ValueOrd , Writer ,
88} ;
99use core:: cmp:: Ordering ;
1010
@@ -40,7 +40,7 @@ impl<'a> AnyRef<'a> {
4040 } ;
4141
4242 /// Create a new [`AnyRef`] from the provided [`Tag`] and DER bytes.
43- pub fn new ( tag : Tag , bytes : & ' a [ u8 ] ) -> Result < Self > {
43+ pub fn new ( tag : Tag , bytes : & ' a [ u8 ] ) -> Result < Self , Error > {
4444 let value = BytesRef :: new ( bytes) . map_err ( |_| ErrorKind :: Length { tag } ) ?;
4545 Ok ( Self { tag, value } )
4646 }
@@ -56,12 +56,12 @@ impl<'a> AnyRef<'a> {
5656 }
5757
5858 /// Attempt to decode this [`AnyRef`] type into the inner value.
59- pub fn decode_as < T > ( self ) -> Result < T >
59+ pub fn decode_as < T > ( self ) -> Result < T , < T as DecodeValue < ' a > > :: Error >
6060 where
6161 T : Choice < ' a > + DecodeValue < ' a > ,
6262 {
6363 if !T :: can_decode ( self . tag ) {
64- return Err ( self . tag . unexpected_error ( None ) ) ;
64+ return Err ( self . tag . unexpected_error ( None ) . into ( ) ) ;
6565 }
6666
6767 let header = Header {
@@ -71,7 +71,7 @@ impl<'a> AnyRef<'a> {
7171
7272 let mut decoder = SliceReader :: new ( self . value ( ) ) ?;
7373 let result = T :: decode_value ( & mut decoder, header) ?;
74- decoder. finish ( result)
74+ Ok ( decoder. finish ( result) ? )
7575 }
7676
7777 /// Is this value an ASN.1 `NULL` value?
@@ -81,14 +81,15 @@ impl<'a> AnyRef<'a> {
8181
8282 /// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
8383 /// nested reader and calling the provided argument with it.
84- pub fn sequence < F , T > ( self , f : F ) -> Result < T >
84+ pub fn sequence < F , T , E > ( self , f : F ) -> Result < T , E >
8585 where
86- F : FnOnce ( & mut SliceReader < ' a > ) -> Result < T > ,
86+ F : FnOnce ( & mut SliceReader < ' a > ) -> Result < T , E > ,
87+ E : From < Error > ,
8788 {
8889 self . tag . assert_eq ( Tag :: Sequence ) ?;
8990 let mut reader = SliceReader :: new ( self . value . as_slice ( ) ) ?;
9091 let result = f ( & mut reader) ?;
91- reader. finish ( result)
92+ Ok ( reader. finish ( result) ? )
9293 }
9394}
9495
@@ -99,14 +100,18 @@ impl<'a> Choice<'a> for AnyRef<'a> {
99100}
100101
101102impl < ' a > Decode < ' a > for AnyRef < ' a > {
102- fn decode < R : Reader < ' a > > ( reader : & mut R ) -> Result < AnyRef < ' a > > {
103+ type Error = Error ;
104+
105+ fn decode < R : Reader < ' a > > ( reader : & mut R ) -> Result < AnyRef < ' a > , Error > {
103106 let header = Header :: decode ( reader) ?;
104107 Self :: decode_value ( reader, header)
105108 }
106109}
107110
108111impl < ' a > DecodeValue < ' a > for AnyRef < ' a > {
109- fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> Result < Self > {
112+ type Error = Error ;
113+
114+ fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> Result < Self , Error > {
110115 Ok ( Self {
111116 tag : header. tag ,
112117 value : BytesRef :: decode_value ( reader, header) ?,
@@ -115,11 +120,11 @@ impl<'a> DecodeValue<'a> for AnyRef<'a> {
115120}
116121
117122impl EncodeValue for AnyRef < ' _ > {
118- fn value_len ( & self ) -> Result < Length > {
123+ fn value_len ( & self ) -> Result < Length , Error > {
119124 Ok ( self . value . len ( ) )
120125 }
121126
122- fn encode_value ( & self , writer : & mut impl Writer ) -> Result < ( ) > {
127+ fn encode_value ( & self , writer : & mut impl Writer ) -> Result < ( ) , Error > {
123128 writer. write ( self . value ( ) )
124129 }
125130}
@@ -131,7 +136,7 @@ impl Tagged for AnyRef<'_> {
131136}
132137
133138impl ValueOrd for AnyRef < ' _ > {
134- fn value_cmp ( & self , other : & Self ) -> Result < Ordering > {
139+ fn value_cmp ( & self , other : & Self ) -> Result < Ordering , Error > {
135140 self . value . der_cmp ( & other. value )
136141 }
137142}
@@ -145,7 +150,7 @@ impl<'a> From<AnyRef<'a>> for BytesRef<'a> {
145150impl < ' a > TryFrom < & ' a [ u8 ] > for AnyRef < ' a > {
146151 type Error = Error ;
147152
148- fn try_from ( bytes : & ' a [ u8 ] ) -> Result < AnyRef < ' a > > {
153+ fn try_from ( bytes : & ' a [ u8 ] ) -> Result < AnyRef < ' a > , Error > {
149154 AnyRef :: from_der ( bytes)
150155 }
151156}
@@ -175,7 +180,7 @@ mod allocating {
175180
176181 impl Any {
177182 /// Create a new [`Any`] from the provided [`Tag`] and DER bytes.
178- pub fn new ( tag : Tag , bytes : impl Into < Box < [ u8 ] > > ) -> Result < Self > {
183+ pub fn new ( tag : Tag , bytes : impl Into < Box < [ u8 ] > > ) -> Result < Self , Error > {
179184 let value = BytesOwned :: new ( bytes) ?;
180185
181186 // Ensure the tag and value are a valid `AnyRef`.
@@ -189,15 +194,15 @@ mod allocating {
189194 }
190195
191196 /// Attempt to decode this [`Any`] type into the inner value.
192- pub fn decode_as < ' a , T > ( & ' a self ) -> Result < T >
197+ pub fn decode_as < ' a , T > ( & ' a self ) -> Result < T , < T as DecodeValue < ' a > > :: Error >
193198 where
194199 T : Choice < ' a > + DecodeValue < ' a > ,
195200 {
196201 AnyRef :: from ( self ) . decode_as ( )
197202 }
198203
199204 /// Encode the provided type as an [`Any`] value.
200- pub fn encode_from < T > ( msg : & T ) -> Result < Self >
205+ pub fn encode_from < T > ( msg : & T ) -> Result < Self , Error >
201206 where
202207 T : Tagged + EncodeValue ,
203208 {
@@ -211,9 +216,10 @@ mod allocating {
211216
212217 /// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
213218 /// nested reader and calling the provided argument with it.
214- pub fn sequence < ' a , F , T > ( & ' a self , f : F ) -> Result < T >
219+ pub fn sequence < ' a , F , T , E > ( & ' a self , f : F ) -> Result < T , E >
215220 where
216- F : FnOnce ( & mut SliceReader < ' a > ) -> Result < T > ,
221+ F : FnOnce ( & mut SliceReader < ' a > ) -> Result < T , E > ,
222+ E : From < Error > ,
217223 {
218224 AnyRef :: from ( self ) . sequence ( f)
219225 }
@@ -234,25 +240,29 @@ mod allocating {
234240 }
235241
236242 impl < ' a > Decode < ' a > for Any {
237- fn decode < R : Reader < ' a > > ( reader : & mut R ) -> Result < Self > {
243+ type Error = Error ;
244+
245+ fn decode < R : Reader < ' a > > ( reader : & mut R ) -> Result < Self , Error > {
238246 let header = Header :: decode ( reader) ?;
239247 Self :: decode_value ( reader, header)
240248 }
241249 }
242250
243251 impl < ' a > DecodeValue < ' a > for Any {
244- fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> Result < Self > {
252+ type Error = Error ;
253+
254+ fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> Result < Self , Error > {
245255 let value = reader. read_vec ( header. length ) ?;
246256 Self :: new ( header. tag , value)
247257 }
248258 }
249259
250260 impl EncodeValue for Any {
251- fn value_len ( & self ) -> Result < Length > {
261+ fn value_len ( & self ) -> Result < Length , Error > {
252262 Ok ( self . value . len ( ) )
253263 }
254264
255- fn encode_value ( & self , writer : & mut impl Writer ) -> Result < ( ) > {
265+ fn encode_value ( & self , writer : & mut impl Writer ) -> Result < ( ) , Error > {
256266 writer. write ( self . value . as_slice ( ) )
257267 }
258268 }
@@ -271,7 +281,7 @@ mod allocating {
271281 }
272282
273283 impl ValueOrd for Any {
274- fn value_cmp ( & self , other : & Self ) -> Result < Ordering > {
284+ fn value_cmp ( & self , other : & Self ) -> Result < Ordering , Error > {
275285 self . value . der_cmp ( & other. value )
276286 }
277287 }
0 commit comments