@@ -54,19 +54,20 @@ assert_eq!(variant, Variant::Bech32);
54
54
#![ deny( non_camel_case_types) ]
55
55
#![ deny( non_snake_case) ]
56
56
#![ deny( unused_mut) ]
57
+
57
58
#![ cfg_attr( feature = "strict" , deny( warnings) ) ]
58
59
#![ cfg_attr( all( not( feature = "std" ) , not( test) ) , no_std) ]
59
60
60
- #[ cfg( all ( not ( feature = "std" ) , not ( test ) ) ) ]
61
+ #[ cfg( feature = "alloc" ) ]
61
62
extern crate alloc;
62
63
63
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
64
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
64
65
use alloc:: borrow:: Cow ;
65
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
66
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
66
67
use alloc:: { string:: String , vec:: Vec } ;
67
68
use core:: convert:: Infallible ;
68
69
use core:: { fmt, mem} ;
69
- #[ cfg( any ( feature = "std" , test ) ) ]
70
+ #[ cfg( feature = "std" ) ]
70
71
use std:: borrow:: Cow ;
71
72
72
73
/// Integer in the range `0..32`
@@ -213,6 +214,7 @@ pub trait FromBase32: Sized {
213
214
fn from_base32 ( b32 : & [ u5 ] ) -> Result < Self , Self :: Err > ;
214
215
}
215
216
217
+ #[ cfg( feature = "alloc" ) ]
216
218
impl WriteBase32 for Vec < u5 > {
217
219
type Err = Infallible ;
218
220
@@ -227,6 +229,7 @@ impl WriteBase32 for Vec<u5> {
227
229
}
228
230
}
229
231
232
+ #[ cfg( feature = "alloc" ) ]
230
233
impl FromBase32 for Vec < u8 > {
231
234
type Err = Error ;
232
235
@@ -236,6 +239,7 @@ impl FromBase32 for Vec<u8> {
236
239
}
237
240
238
241
/// A trait for converting a value to a type `T` that represents a `u5` slice.
242
+ #[ cfg( feature = "alloc" ) ]
239
243
pub trait ToBase32 {
240
244
/// Convert `Self` to base32 vector
241
245
fn to_base32 ( & self ) -> Vec < u5 > {
@@ -250,11 +254,13 @@ pub trait ToBase32 {
250
254
}
251
255
252
256
/// Interface to calculate the length of the base32 representation before actually serializing
257
+ #[ cfg( feature = "alloc" ) ]
253
258
pub trait Base32Len : ToBase32 {
254
259
/// Calculate the base32 serialized length
255
260
fn base32_len ( & self ) -> usize ;
256
261
}
257
262
263
+ #[ cfg( feature = "alloc" ) ]
258
264
impl < T : AsRef < [ u8 ] > > ToBase32 for T {
259
265
fn write_base32 < W : WriteBase32 > ( & self , writer : & mut W ) -> Result < ( ) , <W as WriteBase32 >:: Err > {
260
266
// Amount of bits left over from last round, stored in buffer.
@@ -299,6 +305,7 @@ impl<T: AsRef<[u8]>> ToBase32 for T {
299
305
}
300
306
}
301
307
308
+ #[ cfg( feature = "alloc" ) ]
302
309
impl < T : AsRef < [ u8 ] > > Base32Len for T {
303
310
fn base32_len ( & self ) -> usize {
304
311
let bits = self . as_ref ( ) . len ( ) * 8 ;
@@ -320,6 +327,7 @@ pub trait CheckBase32<T: AsRef<[u5]>> {
320
327
fn check_base32 ( self ) -> Result < T , Self :: Err > ;
321
328
}
322
329
330
+ #[ cfg( feature = "alloc" ) ]
323
331
impl < T : AsRef < [ u8 ] > > CheckBase32 < Vec < u5 > > for T {
324
332
type Err = Error ;
325
333
@@ -329,6 +337,7 @@ impl<T: AsRef<[u8]>> CheckBase32<Vec<u5>> for T {
329
337
}
330
338
331
339
#[ derive( Clone , Copy , PartialEq , Eq ) ]
340
+ #[ cfg( feature = "alloc" ) ]
332
341
enum Case {
333
342
Upper ,
334
343
Lower ,
@@ -341,6 +350,7 @@ enum Case {
341
350
/// * **MixedCase**: If the HRP contains both uppercase and lowercase characters.
342
351
/// * **InvalidChar**: If the HRP contains any non-ASCII characters (outside 33..=126).
343
352
/// * **InvalidLength**: If the HRP is outside 1..83 characters long.
353
+ #[ cfg( feature = "alloc" ) ]
344
354
fn check_hrp ( hrp : & str ) -> Result < Case , Error > {
345
355
if hrp. is_empty ( ) || hrp. len ( ) > 83 {
346
356
return Err ( Error :: InvalidLength ) ;
@@ -380,6 +390,7 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
380
390
/// * If [check_hrp] returns an error for the given HRP.
381
391
/// # Deviations from standard
382
392
/// * No length limits are enforced for the data part
393
+ #[ cfg( feature = "alloc" ) ]
383
394
pub fn encode_to_fmt < T : AsRef < [ u5 ] > > (
384
395
fmt : & mut fmt:: Write ,
385
396
hrp : & str ,
@@ -409,6 +420,7 @@ pub fn encode_to_fmt<T: AsRef<[u5]>>(
409
420
/// * If [check_hrp] returns an error for the given HRP.
410
421
/// # Deviations from standard
411
422
/// * No length limits are enforced for the data part
423
+ #[ cfg( feature = "alloc" ) ]
412
424
pub fn encode_without_checksum_to_fmt < T : AsRef < [ u5 ] > > (
413
425
fmt : & mut fmt:: Write ,
414
426
hrp : & str ,
@@ -447,6 +459,7 @@ const BECH32M_CONST: u32 = 0x2bc8_30a3;
447
459
448
460
impl Variant {
449
461
// Produce the variant based on the remainder of the polymod operation
462
+ #[ cfg( feature = "alloc" ) ]
450
463
fn from_remainder ( c : u32 ) -> Option < Self > {
451
464
match c {
452
465
BECH32_CONST => Some ( Variant :: Bech32 ) ,
@@ -469,6 +482,7 @@ impl Variant {
469
482
/// * If [check_hrp] returns an error for the given HRP.
470
483
/// # Deviations from standard
471
484
/// * No length limits are enforced for the data part
485
+ #[ cfg( feature = "alloc" ) ]
472
486
pub fn encode < T : AsRef < [ u5 ] > > ( hrp : & str , data : T , variant : Variant ) -> Result < String , Error > {
473
487
let mut buf = String :: new ( ) ;
474
488
encode_to_fmt ( & mut buf, hrp, data, variant) ?. unwrap ( ) ;
@@ -481,6 +495,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
481
495
/// * If [check_hrp] returns an error for the given HRP.
482
496
/// # Deviations from standard
483
497
/// * No length limits are enforced for the data part
498
+ #[ cfg( feature = "alloc" ) ]
484
499
pub fn encode_without_checksum < T : AsRef < [ u5 ] > > ( hrp : & str , data : T ) -> Result < String , Error > {
485
500
let mut buf = String :: new ( ) ;
486
501
encode_without_checksum_to_fmt ( & mut buf, hrp, data) ?. unwrap ( ) ;
@@ -490,6 +505,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
490
505
/// Decode a bech32 string into the raw HRP and the data bytes.
491
506
///
492
507
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
508
+ #[ cfg( feature = "alloc" ) ]
493
509
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
494
510
let ( hrp_lower, mut data) = split_and_decode ( s) ?;
495
511
if data. len ( ) < CHECKSUM_LENGTH {
@@ -511,9 +527,11 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
511
527
/// Decode a bech32 string into the raw HRP and the data bytes, assuming no checksum.
512
528
///
513
529
/// Returns the HRP in lowercase and the data.
530
+ #[ cfg( feature = "alloc" ) ]
514
531
pub fn decode_without_checksum ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > { split_and_decode ( s) }
515
532
516
533
/// Decode a bech32 string into the raw HRP and the `u5` data.
534
+ #[ cfg( feature = "alloc" ) ]
517
535
fn split_and_decode ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
518
536
// Split at separator and check for two pieces
519
537
let ( raw_hrp, raw_data) = match s. rfind ( SEP ) {
@@ -570,12 +588,14 @@ fn split_and_decode(s: &str) -> Result<(String, Vec<u5>), Error> {
570
588
Ok ( ( hrp_lower, data) )
571
589
}
572
590
591
+ #[ cfg( feature = "alloc" ) ]
573
592
fn verify_checksum ( hrp : & [ u8 ] , data : & [ u5 ] ) -> Option < Variant > {
574
593
let mut exp = hrp_expand ( hrp) ;
575
594
exp. extend_from_slice ( data) ;
576
595
Variant :: from_remainder ( polymod ( & exp) )
577
596
}
578
597
598
+ #[ cfg( feature = "alloc" ) ]
579
599
fn hrp_expand ( hrp : & [ u8 ] ) -> Vec < u5 > {
580
600
let mut v: Vec < u5 > = Vec :: new ( ) ;
581
601
for b in hrp {
@@ -588,6 +608,7 @@ fn hrp_expand(hrp: &[u8]) -> Vec<u5> {
588
608
v
589
609
}
590
610
611
+ #[ cfg( feature = "alloc" ) ]
591
612
fn polymod ( values : & [ u5 ] ) -> u32 {
592
613
let mut chk: u32 = 1 ;
593
614
let mut b: u8 ;
@@ -616,6 +637,7 @@ const CHARSET: [char; 32] = [
616
637
] ;
617
638
618
639
/// Reverse character set. Maps ASCII byte -> CHARSET index on [0,31]
640
+ #[ cfg( feature = "alloc" ) ]
619
641
const CHARSET_REV : [ i8 ; 128 ] = [
620
642
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
621
643
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
@@ -690,6 +712,7 @@ impl std::error::Error for Error {
690
712
/// let base5 = convert_bits(&[0xff], 8, 5, true);
691
713
/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]);
692
714
/// ```
715
+ #[ cfg( feature = "alloc" ) ]
693
716
pub fn convert_bits < T > ( data : & [ T ] , from : u32 , to : u32 , pad : bool ) -> Result < Vec < u8 > , Error >
694
717
where
695
718
T : Into < u8 > + Copy ,
@@ -725,6 +748,7 @@ where
725
748
}
726
749
727
750
#[ cfg( test) ]
751
+ #[ cfg( feature = "alloc" ) ] // Note, all the unit tests currently require an allocator.
728
752
mod tests {
729
753
use super :: * ;
730
754
0 commit comments