@@ -112,10 +112,6 @@ impl SubstValue {
112
112
Ok ( self . clone ( ) )
113
113
}
114
114
SubstValue :: String ( s) => {
115
- // To be consistent with the template, interpret this as
116
- // an integer and convert it to a big-endian byte array.
117
- // See template::hjson::BigUintVisitor.
118
-
119
115
// Unless the string starts with '0x', expect a decimal string.
120
116
let ( radix, s) = s
121
117
. strip_prefix ( "0x" )
@@ -124,26 +120,19 @@ impl SubstValue {
124
120
. with_context ( || format ! ( "cannot parse {s} as an integer" ) ) ?;
125
121
let bytes = val. to_bytes_be ( ) ;
126
122
ensure ! (
127
- size == 0 || bytes. len( ) = = size,
123
+ size == 0 || bytes. len( ) < = size,
128
124
"expected an integer that fits on {size} bytes but it uses {} bytes" ,
129
125
bytes. len( )
130
126
) ;
131
127
Ok ( SubstValue :: ByteArray ( bytes) )
132
128
}
133
129
SubstValue :: Int32 ( x) => {
134
- // TODO: make this check smarter (ie 0xff could fit on byte but is considered to use 4)
135
130
let bigint = x. to_bigint ( ) . expect ( "cannot convert a i32 to BigInt" ) ;
136
- let mut bytes = bigint. to_signed_bytes_be ( ) ;
131
+ let bytes = bigint. to_signed_bytes_be ( ) ;
137
132
ensure ! (
138
133
size == 0 || bytes. len( ) <= size,
139
134
"expected an integer that fits on {size} bytes but it uses 4 bytes"
140
135
) ;
141
- // We need to pad to the right size.
142
- let pad = if * x < 0 { 0xff } else { 0x00 } ;
143
- while bytes. len ( ) < size {
144
- // Pad to preserve sign.
145
- bytes. insert ( 0 , pad) ;
146
- }
147
136
Ok ( SubstValue :: ByteArray ( bytes) )
148
137
}
149
138
_ => bail ! ( "cannot parse value {self:?} as an integer" ) ,
@@ -587,55 +576,35 @@ mod tests {
587
576
fn parse_integers ( ) {
588
577
// Big-endian integer.
589
578
let byte_array = SubstValue :: ByteArray ( vec ! [ 0x3f , 0x2e , 0x1d , 0x0c ] ) ;
590
- // Size 0 means any size.
591
- assert_eq ! (
592
- byte_array
593
- . parse( & VariableType :: Integer { size: 0 } )
594
- . unwrap( ) ,
595
- byte_array
596
- ) ;
597
- assert_eq ! (
598
- byte_array
599
- . parse( & VariableType :: Integer { size: 4 } )
600
- . unwrap( ) ,
601
- byte_array
602
- ) ;
603
- // Size does not need not match exactly.
604
- assert_eq ! (
605
- byte_array
606
- . parse( & VariableType :: Integer { size: 5 } )
607
- . unwrap( ) ,
608
- byte_array
609
- ) ;
610
- assert ! ( byte_array
611
- . parse( & VariableType :: Integer { size: 3 } )
612
- . is_err( ) ) ;
613
-
579
+ // Strings: hexdecimal and decimal.
580
+ let byte_array_str_hex = SubstValue :: String ( "0x3f2e1d0c" . to_string ( ) ) ;
581
+ let byte_array_str_dec = SubstValue :: String ( "1059986700" . to_string ( ) ) ;
582
+ // Fixed-size integer.
614
583
let byte_array_int = SubstValue :: Int32 ( 0x3f2e1d0c ) ;
615
- // Size 0 means any size.
616
- assert_eq ! (
617
- byte_array_int
618
- . parse ( & VariableType :: Integer { size : 0 } )
619
- . unwrap ( ) ,
620
- byte_array
621
- ) ;
622
- assert_eq ! (
623
- byte_array_int
624
- . parse( & VariableType :: Integer { size: 4 } )
625
- . unwrap ( ) ,
626
- byte_array
627
- ) ;
628
- assert ! ( byte_array_int
629
- . parse ( & VariableType :: Integer { size : 3 } )
630
- . is_err ( ) ) ;
631
- // A bigger size is acceptable and it should be padded .
632
- let byte_array_pad = SubstValue :: ByteArray ( vec ! [ 0x00 , 0x3f , 0x2e , 0x1d , 0x0c ] ) ;
633
- assert_eq ! (
634
- byte_array_int
635
- . parse ( & VariableType :: Integer { size : 5 } )
636
- . unwrap ( ) ,
637
- byte_array_pad
638
- ) ;
584
+
585
+ for val in [
586
+ & byte_array ,
587
+ & byte_array_int ,
588
+ & byte_array_str_hex ,
589
+ & byte_array_str_dec ,
590
+ ] {
591
+ // Size 0 means any size.
592
+ assert_eq ! (
593
+ val . parse( & VariableType :: Integer { size: 0 } ) . unwrap ( ) ,
594
+ byte_array
595
+ ) ;
596
+ assert_eq ! (
597
+ val . parse ( & VariableType :: Integer { size : 4 } ) . unwrap ( ) ,
598
+ byte_array
599
+ ) ;
600
+ // Size does not need not match exactly .
601
+ assert_eq ! (
602
+ val . parse ( & VariableType :: Integer { size : 5 } ) . unwrap ( ) ,
603
+ byte_array
604
+ ) ;
605
+ // Too small size in an error.
606
+ assert ! ( val . parse ( & VariableType :: Integer { size : 3 } ) . is_err ( ) ) ;
607
+ }
639
608
}
640
609
641
610
/// Test parsing of strings.
0 commit comments