@@ -12,6 +12,11 @@ use serde_json::json;
12
12
use ipld_core:: cid:: Cid ;
13
13
use ipld_core:: ipld:: Ipld ;
14
14
15
+ #[ cfg( not( feature = "integer-max-i64" ) ) ]
16
+ type Integer = i128 ;
17
+ #[ cfg( feature = "integer-max-i64" ) ]
18
+ type Integer = i64 ;
19
+
15
20
/// This function is to test that all IPLD kinds except the given one errors, when trying to
16
21
/// deserialize to the given Rust type.
17
22
fn error_except < ' de , T > ( _input : T , except : & Ipld )
@@ -98,9 +103,9 @@ fn ipld_deserializer_u8() {
98
103
"Correctly deserialize Ipld::Integer to u8."
99
104
) ;
100
105
101
- let too_large = u8:: deserialize ( Ipld :: Integer ( ( u8:: MAX as i128 ) + 10 ) ) ;
106
+ let too_large = u8:: deserialize ( Ipld :: Integer ( ( u8:: MAX as Integer ) + 10 ) ) ;
102
107
assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
103
- let too_small = u8:: deserialize ( Ipld :: Integer ( ( u8:: MIN as i128 ) - 10 ) ) ;
108
+ let too_small = u8:: deserialize ( Ipld :: Integer ( ( u8:: MIN as Integer ) - 10 ) ) ;
104
109
assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
105
110
}
106
111
@@ -116,9 +121,9 @@ fn ipld_deserializer_u16() {
116
121
"Correctly deserialize Ipld::Integer to u16."
117
122
) ;
118
123
119
- let too_large = u16:: deserialize ( Ipld :: Integer ( ( u16:: MAX as i128 ) + 10 ) ) ;
124
+ let too_large = u16:: deserialize ( Ipld :: Integer ( ( u16:: MAX as Integer ) + 10 ) ) ;
120
125
assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
121
- let too_small = u16:: deserialize ( Ipld :: Integer ( ( u16:: MIN as i128 ) - 10 ) ) ;
126
+ let too_small = u16:: deserialize ( Ipld :: Integer ( ( u16:: MIN as Integer ) - 10 ) ) ;
122
127
assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
123
128
}
124
129
@@ -134,16 +139,17 @@ fn ipld_deserializer_u32() {
134
139
"Correctly deserialize Ipld::Integer to u32."
135
140
) ;
136
141
137
- let too_large = u32:: deserialize ( Ipld :: Integer ( ( u32:: MAX as i128 ) + 10 ) ) ;
142
+ let too_large = u32:: deserialize ( Ipld :: Integer ( ( u32:: MAX as Integer ) + 10 ) ) ;
138
143
assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
139
- let too_small = u32:: deserialize ( Ipld :: Integer ( ( u32:: MIN as i128 ) - 10 ) ) ;
144
+ let too_small = u32:: deserialize ( Ipld :: Integer ( ( u32:: MIN as Integer ) - 10 ) ) ;
140
145
assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
141
146
}
142
147
143
148
#[ test]
149
+ #[ allow( clippy:: unnecessary_fallible_conversions) ]
144
150
fn ipld_deserializer_u64 ( ) {
145
151
let integer = 34567890123u64 ;
146
- let ipld = Ipld :: Integer ( integer. into ( ) ) ;
152
+ let ipld = Ipld :: Integer ( integer. try_into ( ) . unwrap ( ) ) ;
147
153
error_except ( integer, & ipld) ;
148
154
149
155
let deserialized = u64:: deserialize ( ipld) . unwrap ( ) ;
@@ -152,10 +158,13 @@ fn ipld_deserializer_u64() {
152
158
"Correctly deserialize Ipld::Integer to u64."
153
159
) ;
154
160
155
- let too_large = u64:: deserialize ( Ipld :: Integer ( ( u64:: MAX as i128 ) + 10 ) ) ;
156
- assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
157
- let too_small = u64:: deserialize ( Ipld :: Integer ( ( u64:: MIN as i128 ) - 10 ) ) ;
158
- assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
161
+ #[ cfg( not( feature = "integer-max-i64" ) ) ]
162
+ {
163
+ let too_large = u64:: deserialize ( Ipld :: Integer ( ( u64:: MAX as Integer ) + 10 ) ) ;
164
+ assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
165
+ let too_small = u64:: deserialize ( Ipld :: Integer ( ( u64:: MIN as Integer ) - 10 ) ) ;
166
+ assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
167
+ }
159
168
}
160
169
161
170
#[ test]
@@ -170,9 +179,9 @@ fn ipld_deserializer_i8() {
170
179
"Correctly deserialize Ipld::Integer to i8."
171
180
) ;
172
181
173
- let too_large = i8:: deserialize ( Ipld :: Integer ( ( i8:: MAX as i128 ) + 10 ) ) ;
182
+ let too_large = i8:: deserialize ( Ipld :: Integer ( ( i8:: MAX as Integer ) + 10 ) ) ;
174
183
assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
175
- let too_small = i8:: deserialize ( Ipld :: Integer ( ( i8:: MIN as i128 ) - 10 ) ) ;
184
+ let too_small = i8:: deserialize ( Ipld :: Integer ( ( i8:: MIN as Integer ) - 10 ) ) ;
176
185
assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
177
186
}
178
187
@@ -188,9 +197,9 @@ fn ipld_deserializer_i16() {
188
197
"Correctly deserialize Ipld::Integer to i16."
189
198
) ;
190
199
191
- let too_large = i16:: deserialize ( Ipld :: Integer ( ( i16:: MAX as i128 ) + 10 ) ) ;
200
+ let too_large = i16:: deserialize ( Ipld :: Integer ( ( i16:: MAX as Integer ) + 10 ) ) ;
192
201
assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
193
- let too_small = i16:: deserialize ( Ipld :: Integer ( ( i16:: MIN as i128 ) - 10 ) ) ;
202
+ let too_small = i16:: deserialize ( Ipld :: Integer ( ( i16:: MIN as Integer ) - 10 ) ) ;
194
203
assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
195
204
}
196
205
@@ -206,9 +215,9 @@ fn ipld_deserializer_i32() {
206
215
"Correctly deserialize Ipld::Integer to i32."
207
216
) ;
208
217
209
- let too_large = i32:: deserialize ( Ipld :: Integer ( ( i32:: MAX as i128 ) + 10 ) ) ;
218
+ let too_large = i32:: deserialize ( Ipld :: Integer ( ( i32:: MAX as Integer ) + 10 ) ) ;
210
219
assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
211
- let too_small = i32:: deserialize ( Ipld :: Integer ( ( i32:: MIN as i128 ) - 10 ) ) ;
220
+ let too_small = i32:: deserialize ( Ipld :: Integer ( ( i32:: MIN as Integer ) - 10 ) ) ;
212
221
assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
213
222
}
214
223
@@ -224,10 +233,13 @@ fn ipld_deserializer_i64() {
224
233
"Correctly deserialize Ipld::Integer to i64."
225
234
) ;
226
235
227
- let too_large = i64:: deserialize ( Ipld :: Integer ( ( i64:: MAX as i128 ) + 10 ) ) ;
228
- assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
229
- let too_small = i64:: deserialize ( Ipld :: Integer ( ( i64:: MIN as i128 ) - 10 ) ) ;
230
- assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
236
+ #[ cfg( not( feature = "integer-max-i64" ) ) ]
237
+ {
238
+ let too_large = i64:: deserialize ( Ipld :: Integer ( ( i64:: MAX as i128 ) + 10 ) ) ;
239
+ assert ! ( too_large. is_err( ) , "Number must be within range." ) ;
240
+ let too_small = i64:: deserialize ( Ipld :: Integer ( ( i64:: MIN as i128 ) - 10 ) ) ;
241
+ assert ! ( too_small. is_err( ) , "Number must be within range." ) ;
242
+ }
231
243
}
232
244
233
245
#[ test]
0 commit comments