@@ -219,7 +219,7 @@ pub fn decode(s: &str) -> Result<(Hrp, Vec<u8>), DecodeError> {
219
219
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
220
220
#[ cfg( feature = "alloc" ) ]
221
221
#[ inline]
222
- pub fn encode < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , fmt :: Error > {
222
+ pub fn encode < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , EncodeError > {
223
223
encode_lower :: < Ck > ( hrp, data)
224
224
}
225
225
@@ -229,7 +229,9 @@ pub fn encode<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::Error>
229
229
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
230
230
#[ cfg( feature = "alloc" ) ]
231
231
#[ inline]
232
- pub fn encode_lower < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , fmt:: Error > {
232
+ pub fn encode_lower < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , EncodeError > {
233
+ let _ = encoded_length :: < Ck > ( & hrp, data) ?;
234
+
233
235
let mut buf = String :: new ( ) ;
234
236
encode_lower_to_fmt :: < Ck , String > ( & mut buf, hrp, data) ?;
235
237
Ok ( buf)
@@ -241,7 +243,9 @@ pub fn encode_lower<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::
241
243
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
242
244
#[ cfg( feature = "alloc" ) ]
243
245
#[ inline]
244
- pub fn encode_upper < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , fmt:: Error > {
246
+ pub fn encode_upper < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , EncodeError > {
247
+ let _ = encoded_length :: < Ck > ( & hrp, data) ?;
248
+
245
249
let mut buf = String :: new ( ) ;
246
250
encode_upper_to_fmt :: < Ck , String > ( & mut buf, hrp, data) ?;
247
251
Ok ( buf)
@@ -256,7 +260,7 @@ pub fn encode_to_fmt<Ck: Checksum, W: fmt::Write>(
256
260
fmt : & mut W ,
257
261
hrp : Hrp ,
258
262
data : & [ u8 ] ,
259
- ) -> Result < ( ) , fmt :: Error > {
263
+ ) -> Result < ( ) , EncodeError > {
260
264
encode_lower_to_fmt :: < Ck , W > ( fmt, hrp, data)
261
265
}
262
266
@@ -269,7 +273,9 @@ pub fn encode_lower_to_fmt<Ck: Checksum, W: fmt::Write>(
269
273
fmt : & mut W ,
270
274
hrp : Hrp ,
271
275
data : & [ u8 ] ,
272
- ) -> Result < ( ) , fmt:: Error > {
276
+ ) -> Result < ( ) , EncodeError > {
277
+ let _ = encoded_length :: < Ck > ( & hrp, data) ?;
278
+
273
279
let iter = data. iter ( ) . copied ( ) . bytes_to_fes ( ) ;
274
280
let chars = iter. with_checksum :: < Ck > ( & hrp) . chars ( ) ;
275
281
for c in chars {
@@ -287,7 +293,9 @@ pub fn encode_upper_to_fmt<Ck: Checksum, W: fmt::Write>(
287
293
fmt : & mut W ,
288
294
hrp : Hrp ,
289
295
data : & [ u8 ] ,
290
- ) -> Result < ( ) , fmt:: Error > {
296
+ ) -> Result < ( ) , EncodeError > {
297
+ let _ = encoded_length :: < Ck > ( & hrp, data) ?;
298
+
291
299
let iter = data. iter ( ) . copied ( ) . bytes_to_fes ( ) ;
292
300
let chars = iter. with_checksum :: < Ck > ( & hrp) . chars ( ) ;
293
301
for c in chars {
@@ -455,6 +463,49 @@ impl From<DecodeError> for DecodeFromReaderError {
455
463
fn from ( e : DecodeError ) -> Self { Self :: Decode ( e) }
456
464
}
457
465
466
+ /// An error while encoding an address.
467
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
468
+ #[ non_exhaustive]
469
+ pub enum EncodeError {
470
+ /// Encoding HRP and data into a bech32 string exceeds the spec limit of 90 characters.
471
+ TooLong ( EncodedLengthError ) ,
472
+ /// Error writing to the formatter.
473
+ Fmt ( fmt:: Error ) ,
474
+ }
475
+
476
+ impl fmt:: Display for EncodeError {
477
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
478
+ use EncodeError :: * ;
479
+
480
+ match * self {
481
+ TooLong ( ref e) => write_err ! ( f, "encoded string too long" ; e) ,
482
+ Fmt ( ref e) => write_err ! ( f, "write to formatter failed" ; e) ,
483
+ }
484
+ }
485
+ }
486
+
487
+ #[ cfg( feature = "std" ) ]
488
+ impl std:: error:: Error for EncodeError {
489
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
490
+ use EncodeError :: * ;
491
+
492
+ match * self {
493
+ TooLong ( ref e) => Some ( e) ,
494
+ Fmt ( ref e) => Some ( e) ,
495
+ }
496
+ }
497
+ }
498
+
499
+ impl From < EncodedLengthError > for EncodeError {
500
+ #[ inline]
501
+ fn from ( e : EncodedLengthError ) -> Self { Self :: TooLong ( e) }
502
+ }
503
+
504
+ impl From < fmt:: Error > for EncodeError {
505
+ #[ inline]
506
+ fn from ( e : fmt:: Error ) -> Self { Self :: Fmt ( e) }
507
+ }
508
+
458
509
/// Encoding bech32 string exceeds the spec limit of 90 characters.
459
510
#[ derive( Debug , Clone , PartialEq , Eq ) ]
460
511
#[ non_exhaustive]
0 commit comments