compiler-core
erlang-term-format
···
864
864
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
865
865
866
866
[[package]]
867
867
+
name = "erlang-term-format"
868
868
+
version = "1.0.0"
869
869
+
dependencies = [
870
870
+
"num-bigint",
871
871
+
"num-traits",
872
872
+
"pretty_assertions",
873
873
+
]
874
874
+
875
875
+
[[package]]
867
876
name = "errno"
868
877
version = "0.3.8"
869
878
source = "registry+https://github.com/rust-lang/crates.io-index"
···
17
17
"hexpm",
18
18
"pretty-arena",
19
19
"format",
20
20
+
"erlang-term-format",
20
21
]
21
22
22
23
# common dependencies
···
81
82
ignore = "0"
82
83
# Parsing of arbitrary width int values
83
84
num-bigint = { version = "0.4.6", features = ["serde"] }
85
85
+
num-traits = "0.2.19"
84
86
# Unicode grapheme traversal
85
87
unicode-segmentation = "1.13.2"
86
88
# Checksums
···
37
37
id-arena = "2"
38
38
# Bijective (bi-directional) hashmap
39
39
bimap = { version = "0.6.3", features = ["serde"] }
40
40
-
num-traits = "0.2.19"
41
40
# Encryption
42
41
age = { version = "0.11", features = ["armor"] }
43
42
radix_trie = "0.3"
···
50
49
indexmap = "2.12.1"
51
50
52
51
num-bigint.workspace = true
52
52
+
num-traits.workspace = true
53
53
async-trait.workspace = true
54
54
base16.workspace = true
55
55
camino = { workspace = true, features = ["serde1"] }
···
1
1
+
[package]
2
2
+
name = "erlang-term-format"
3
3
+
version = "1.0.0"
4
4
+
edition = "2024"
5
5
+
6
6
+
[dependencies]
7
7
+
num-bigint.workspace = true
8
8
+
num-traits.workspace = true
9
9
+
10
10
+
[dev-dependencies]
11
11
+
pretty_assertions.workspace = true
···
1
1
+
use num_bigint::{BigInt, Sign};
2
2
+
3
3
+
#[cfg(test)]
4
4
+
#[macro_use]
5
5
+
extern crate pretty_assertions;
6
6
+
7
7
+
/// A data structure used to encode values into the Erlang Term Format:
8
8
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html.
9
9
+
///
10
10
+
/// Hello!!
11
11
+
///
12
12
+
pub struct Etf {
13
13
+
bytes: Vec<u8>,
14
14
+
}
15
15
+
16
16
+
#[must_use]
17
17
+
pub struct List {
18
18
+
size_index: usize,
19
19
+
used: bool,
20
20
+
}
21
21
+
22
22
+
impl Drop for List {
23
23
+
fn drop(&mut self) {
24
24
+
assert!(self.used, "list not closed");
25
25
+
}
26
26
+
}
27
27
+
28
28
+
impl Etf {
29
29
+
pub fn new() -> Self {
30
30
+
Self { bytes: vec![131] }
31
31
+
}
32
32
+
33
33
+
/// Get the binary representation of the data structure built so far.
34
34
+
pub fn into_vec(self) -> Vec<u8> {
35
35
+
self.bytes
36
36
+
}
37
37
+
38
38
+
fn push(&mut self, byte: u8) {
39
39
+
self.bytes.push(byte);
40
40
+
}
41
41
+
42
42
+
fn extend(&mut self, bytes: impl IntoIterator<Item = u8>) {
43
43
+
self.bytes.extend(bytes);
44
44
+
}
45
45
+
46
46
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_integer_ext
47
47
+
fn small_integer(&mut self, value: u8) {
48
48
+
self.push(97);
49
49
+
self.push(value);
50
50
+
}
51
51
+
52
52
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#integer_ext
53
53
+
fn integer(&mut self, value: i32) {
54
54
+
self.push(98);
55
55
+
self.extend(value.to_be_bytes());
56
56
+
}
57
57
+
58
58
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#new_float_ext
59
59
+
fn new_float(&mut self, value: f64) {
60
60
+
self.push(70);
61
61
+
self.extend(value.to_be_bytes());
62
62
+
}
63
63
+
64
64
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_tuple_ext
65
65
+
pub fn small_tuple(&mut self, arity: u8) {
66
66
+
self.push(104);
67
67
+
self.push(arity);
68
68
+
}
69
69
+
70
70
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#large_tuple_ext
71
71
+
pub fn large_tuple(&mut self, arity: u32) {
72
72
+
self.push(105);
73
73
+
self.extend(arity.to_be_bytes());
74
74
+
}
75
75
+
76
76
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#nil_ext
77
77
+
fn nil(&mut self) {
78
78
+
self.push(106);
79
79
+
}
80
80
+
81
81
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#list_ext
82
82
+
fn start_list(&mut self) -> List {
83
83
+
self.push(108);
84
84
+
let size_index = self.bytes.len();
85
85
+
self.push(0);
86
86
+
self.push(0);
87
87
+
self.push(0);
88
88
+
self.push(0);
89
89
+
List {
90
90
+
size_index,
91
91
+
used: false,
92
92
+
}
93
93
+
}
94
94
+
95
95
+
fn end_list(&mut self, mut list: List, items: u32) {
96
96
+
self.nil();
97
97
+
self.bytes[list.size_index..list.size_index + 4].copy_from_slice(&items.to_be_bytes());
98
98
+
list.used = true;
99
99
+
}
100
100
+
101
101
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#binary_ext
102
102
+
fn binary(&mut self, bytes: Vec<u8>) {
103
103
+
self.push(109);
104
104
+
self.extend((bytes.len() as u32).to_be_bytes());
105
105
+
self.extend(bytes);
106
106
+
}
107
107
+
108
108
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_big_ext
109
109
+
fn small_big(&mut self, number: BigInt) {
110
110
+
let (sign, bytes) = number.to_bytes_le();
111
111
+
self.push(110);
112
112
+
self.push(bytes.len() as u8);
113
113
+
match sign {
114
114
+
Sign::NoSign | Sign::Plus => self.push(0),
115
115
+
Sign::Minus => self.push(1),
116
116
+
}
117
117
+
self.extend(bytes);
118
118
+
}
119
119
+
120
120
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#large_big_ext
121
121
+
fn large_big(&mut self, number: BigInt) {
122
122
+
let (sign, bytes) = number.to_bytes_le();
123
123
+
self.push(111);
124
124
+
self.extend((bytes.len() as u32).to_be_bytes());
125
125
+
match sign {
126
126
+
Sign::NoSign | Sign::Plus => self.push(0),
127
127
+
Sign::Minus => self.push(1),
128
128
+
}
129
129
+
self.extend(bytes);
130
130
+
}
131
131
+
132
132
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#small_atom_utf8_ext
133
133
+
fn small_atom_utf8(&mut self, name: &str) {
134
134
+
self.push(119);
135
135
+
self.push(name.len() as u8);
136
136
+
self.extend(name.bytes());
137
137
+
}
138
138
+
139
139
+
/// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html#atom_utf8_ext
140
140
+
fn atom_utf8(&mut self, name: &str) {
141
141
+
self.push(118);
142
142
+
self.extend((name.len() as u16).to_be_bytes());
143
143
+
self.extend(name.bytes());
144
144
+
}
145
145
+
}
146
146
+
147
147
+
#[cfg(test)]
148
148
+
mod tests {
149
149
+
use std::{ops::Neg, str::FromStr};
150
150
+
151
151
+
use num_bigint::BigInt;
152
152
+
153
153
+
use crate::Etf;
154
154
+
155
155
+
#[test]
156
156
+
fn small_atom() {
157
157
+
let mut etf = Etf::new();
158
158
+
etf.small_atom_utf8("atom");
159
159
+
assert_eq!(etf.into_vec(), [131, 119, 4, 97, 116, 111, 109])
160
160
+
}
161
161
+
162
162
+
#[test]
163
163
+
fn small_atom_utf8() {
164
164
+
let mut etf = Etf::new();
165
165
+
etf.small_atom_utf8("ksiąskę");
166
166
+
assert_eq!(
167
167
+
etf.into_vec(),
168
168
+
[131, 119, 9, 107, 115, 105, 196, 133, 115, 107, 196, 153]
169
169
+
)
170
170
+
}
171
171
+
172
172
+
#[test]
173
173
+
fn atom() {
174
174
+
let mut etf = Etf::new();
175
175
+
etf.atom_utf8(&"ą".repeat(128));
176
176
+
assert_eq!(
177
177
+
etf.into_vec(),
178
178
+
[
179
179
+
131, 118, 1, 0, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
180
180
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
181
181
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
182
182
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
183
183
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
184
184
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
185
185
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
186
186
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
187
187
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
188
188
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
189
189
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
190
190
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
191
191
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
192
192
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
193
193
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
194
194
+
133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196, 133, 196,
195
195
+
133, 196, 133
196
196
+
]
197
197
+
)
198
198
+
}
199
199
+
200
200
+
#[test]
201
201
+
fn small_integer() {
202
202
+
let mut etf = Etf::new();
203
203
+
etf.small_integer(1);
204
204
+
assert_eq!(etf.into_vec(), [131, 97, 1]);
205
205
+
}
206
206
+
207
207
+
#[test]
208
208
+
fn integer() {
209
209
+
let mut etf = Etf::new();
210
210
+
etf.integer(-2);
211
211
+
assert_eq!(etf.into_vec(), [131, 98, 255, 255, 255, 254]);
212
212
+
}
213
213
+
214
214
+
#[test]
215
215
+
fn integer_2() {
216
216
+
let mut etf = Etf::new();
217
217
+
etf.integer(1048576);
218
218
+
assert_eq!(etf.into_vec(), [131, 98, 0, 16, 0, 0]);
219
219
+
}
220
220
+
221
221
+
#[test]
222
222
+
fn float() {
223
223
+
let mut etf = Etf::new();
224
224
+
etf.new_float(1.2);
225
225
+
assert_eq!(etf.into_vec(), [131, 70, 63, 243, 51, 51, 51, 51, 51, 51])
226
226
+
}
227
227
+
228
228
+
#[test]
229
229
+
fn small_big() {
230
230
+
let mut etf = Etf::new();
231
231
+
232
232
+
etf.small_big(BigInt::from(123123123123123123 as i64));
233
233
+
assert_eq!(
234
234
+
etf.into_vec(),
235
235
+
[131, 110, 8, 0, 179, 243, 99, 1, 212, 107, 181, 1]
236
236
+
);
237
237
+
}
238
238
+
239
239
+
#[test]
240
240
+
fn negative_small_big() {
241
241
+
let mut etf = Etf::new();
242
242
+
etf.small_big(BigInt::from(-123123123123123123 as i64));
243
243
+
assert_eq!(
244
244
+
etf.into_vec(),
245
245
+
[131, 110, 8, 1, 179, 243, 99, 1, 212, 107, 181, 1]
246
246
+
);
247
247
+
}
248
248
+
249
249
+
#[test]
250
250
+
fn empty_list() {
251
251
+
let mut etf = Etf::new();
252
252
+
etf.nil();
253
253
+
assert_eq!(etf.into_vec(), [131, 106]);
254
254
+
}
255
255
+
256
256
+
#[test]
257
257
+
fn proper_list_with_a_single_item() {
258
258
+
let mut etf = Etf::new();
259
259
+
let list = etf.start_list();
260
260
+
etf.small_integer(1);
261
261
+
etf.end_list(list, 1);
262
262
+
assert_eq!(etf.into_vec(), [131, 108, 0, 0, 0, 1, 97, 1, 106])
263
263
+
}
264
264
+
265
265
+
#[test]
266
266
+
fn proper_list() {
267
267
+
let mut etf = Etf::new();
268
268
+
let list = etf.start_list();
269
269
+
etf.small_integer(1);
270
270
+
etf.small_tuple(0);
271
271
+
etf.small_integer(2);
272
272
+
etf.end_list(list, 3);
273
273
+
assert_eq!(
274
274
+
etf.into_vec(),
275
275
+
[131, 108, 0, 0, 0, 3, 97, 1, 104, 0, 97, 2, 106]
276
276
+
)
277
277
+
}
278
278
+
279
279
+
#[test]
280
280
+
fn empty_binary() {
281
281
+
let mut etf = Etf::new();
282
282
+
etf.binary(vec![]);
283
283
+
assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 0])
284
284
+
}
285
285
+
#[test]
286
286
+
fn binary() {
287
287
+
let mut etf = Etf::new();
288
288
+
etf.binary(vec![1, 2, 3]);
289
289
+
assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 3, 1, 2, 3])
290
290
+
}
291
291
+
292
292
+
#[test]
293
293
+
fn small_empty_tuple() {
294
294
+
let mut etf = Etf::new();
295
295
+
etf.small_tuple(0);
296
296
+
assert_eq!(etf.into_vec(), [131, 104, 0])
297
297
+
}
298
298
+
299
299
+
#[test]
300
300
+
fn small_single_item_tuple() {
301
301
+
let mut etf = Etf::new();
302
302
+
etf.small_tuple(1);
303
303
+
etf.small_integer(1);
304
304
+
assert_eq!(etf.into_vec(), [131, 104, 1, 97, 1])
305
305
+
}
306
306
+
307
307
+
#[test]
308
308
+
fn small_tuple() {
309
309
+
let mut etf = Etf::new();
310
310
+
etf.small_tuple(3);
311
311
+
etf.new_float(1.1);
312
312
+
etf.integer(-1);
313
313
+
etf.small_integer(11);
314
314
+
315
315
+
assert_eq!(
316
316
+
etf.into_vec(),
317
317
+
[
318
318
+
131, 104, 3, 70, 63, 241, 153, 153, 153, 153, 153, 154, 98, 255, 255, 255, 255, 97,
319
319
+
11
320
320
+
]
321
321
+
)
322
322
+
}
323
323
+
324
324
+
#[test]
325
325
+
fn small_nested_tuple() {
326
326
+
let mut etf = Etf::new();
327
327
+
etf.small_tuple(2);
328
328
+
329
329
+
etf.small_tuple(2);
330
330
+
etf.small_integer(1);
331
331
+
etf.small_integer(11);
332
332
+
333
333
+
etf.small_tuple(1);
334
334
+
etf.new_float(1.1);
335
335
+
336
336
+
assert_eq!(
337
337
+
etf.into_vec(),
338
338
+
[
339
339
+
131, 104, 2, 104, 2, 97, 1, 97, 11, 104, 1, 70, 63, 241, 153, 153, 153, 153, 153,
340
340
+
154
341
341
+
]
342
342
+
)
343
343
+
}
344
344
+
345
345
+
#[test]
346
346
+
fn large_tuple() {
347
347
+
let mut etf = Etf::new();
348
348
+
etf.large_tuple(500);
349
349
+
for _ in 1..=500 {
350
350
+
etf.small_integer(1);
351
351
+
}
352
352
+
353
353
+
assert_eq!(
354
354
+
etf.into_vec(),
355
355
+
[
356
356
+
131, 105, 0, 0, 1, 244, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
357
357
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
358
358
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
359
359
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
360
360
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
361
361
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
362
362
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
363
363
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
364
364
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
365
365
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
366
366
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
367
367
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
368
368
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
369
369
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
370
370
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
371
371
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
372
372
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
373
373
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
374
374
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
375
375
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
376
376
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
377
377
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
378
378
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
379
379
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
380
380
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
381
381
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
382
382
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
383
383
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
384
384
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
385
385
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
386
386
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
387
387
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
388
388
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
389
389
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
390
390
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
391
391
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
392
392
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
393
393
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
394
394
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
395
395
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
396
396
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97,
397
397
+
1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1, 97, 1
398
398
+
]
399
399
+
)
400
400
+
}
401
401
+
402
402
+
#[test]
403
403
+
fn large_big() {
404
404
+
let mut etf = Etf::new();
405
405
+
etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap());
406
406
+
assert_eq!(
407
407
+
etf.into_vec(),
408
408
+
[
409
409
+
131, 111, 0, 0, 2, 111, 0, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
410
410
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
411
411
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
412
412
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
413
413
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
414
414
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
415
415
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
416
416
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
417
417
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
418
418
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
419
419
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147,
420
420
+
190, 32, 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97,
421
421
+
156, 59, 26, 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217,
422
422
+
244, 127, 22, 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140,
423
423
+
187, 169, 145, 218, 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155,
424
424
+
60, 1, 158, 242, 71, 1, 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133,
425
425
+
199, 37, 61, 61, 94, 35, 52, 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64,
426
426
+
238, 18, 222, 214, 221, 45, 236, 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2,
427
427
+
156, 28, 29, 102, 103, 8, 227, 146, 128, 117, 183, 231, 113, 20, 148, 190, 253, 95,
428
428
+
114, 208, 224, 186, 125, 28, 74, 117, 131, 197, 189, 238, 208, 96, 253, 134, 27,
429
429
+
108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 129, 196, 241, 71, 237, 83, 136, 127,
430
430
+
61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 147, 198, 3, 37, 170, 175, 29, 43,
431
431
+
85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 16, 159, 117, 150, 91, 90, 121,
432
432
+
11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 5, 126, 101, 113, 247, 14,
433
433
+
130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 105, 77, 243, 58, 158,
434
434
+
223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 101, 6, 176, 22, 182,
435
435
+
89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 130, 115, 157, 80,
436
436
+
21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 32, 199, 44,
437
437
+
85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 85, 122,
438
438
+
148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 99,
439
439
+
154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47,
440
440
+
134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247,
441
441
+
169, 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95,
442
442
+
65, 114, 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5,
443
443
+
172, 107, 216, 222, 165, 242, 106, 222, 22, 158, 50, 13
444
444
+
]
445
445
+
);
446
446
+
}
447
447
+
448
448
+
#[test]
449
449
+
fn negative_large_big() {
450
450
+
let mut etf = Etf::new();
451
451
+
etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap().neg());
452
452
+
assert_eq!(
453
453
+
etf.into_vec(),
454
454
+
[
455
455
+
131, 111, 0, 0, 2, 111, 1, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
456
456
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
457
457
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
458
458
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
459
459
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
460
460
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
461
461
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
462
462
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
463
463
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
464
464
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28,
465
465
+
199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 113, 28, 199, 1, 169, 242, 20, 147,
466
466
+
190, 32, 128, 194, 36, 247, 163, 33, 20, 52, 44, 99, 21, 192, 66, 245, 84, 128, 97,
467
467
+
156, 59, 26, 49, 215, 19, 60, 165, 65, 99, 28, 91, 91, 132, 187, 191, 48, 27, 217,
468
468
+
244, 127, 22, 229, 191, 10, 184, 4, 38, 105, 182, 90, 90, 141, 23, 179, 148, 140,
469
469
+
187, 169, 145, 218, 132, 199, 136, 139, 1, 228, 120, 218, 220, 103, 20, 164, 155,
470
470
+
60, 1, 158, 242, 71, 1, 60, 69, 6, 13, 244, 52, 42, 222, 129, 25, 127, 251, 133,
471
471
+
199, 37, 61, 61, 94, 35, 52, 38, 9, 114, 0, 173, 92, 189, 59, 8, 14, 253, 129, 64,
472
472
+
238, 18, 222, 214, 221, 45, 236, 250, 62, 255, 237, 241, 149, 145, 143, 93, 41, 2,
473
473
+
156, 28, 29, 102, 103, 8, 227, 146, 128, 117, 183, 231, 113, 20, 148, 190, 253, 95,
474
474
+
114, 208, 224, 186, 125, 28, 74, 117, 131, 197, 189, 238, 208, 96, 253, 134, 27,
475
475
+
108, 58, 63, 97, 183, 248, 49, 174, 205, 42, 129, 196, 241, 71, 237, 83, 136, 127,
476
476
+
61, 225, 107, 199, 146, 30, 212, 53, 215, 141, 147, 198, 3, 37, 170, 175, 29, 43,
477
477
+
85, 98, 73, 198, 113, 9, 121, 149, 228, 107, 200, 16, 159, 117, 150, 91, 90, 121,
478
478
+
11, 5, 27, 25, 110, 69, 194, 57, 21, 91, 173, 149, 40, 5, 126, 101, 113, 247, 14,
479
479
+
130, 154, 249, 36, 143, 183, 129, 4, 174, 108, 164, 122, 95, 105, 77, 243, 58, 158,
480
480
+
223, 166, 79, 103, 131, 161, 110, 57, 79, 214, 142, 141, 39, 101, 6, 176, 22, 182,
481
481
+
89, 247, 229, 80, 244, 24, 62, 101, 128, 174, 253, 86, 233, 138, 130, 115, 157, 80,
482
482
+
21, 83, 187, 222, 43, 19, 105, 97, 230, 7, 167, 25, 6, 69, 207, 149, 32, 199, 44,
483
483
+
85, 27, 223, 133, 46, 234, 3, 59, 113, 228, 246, 29, 157, 4, 83, 207, 234, 85, 122,
484
484
+
148, 64, 125, 49, 48, 123, 226, 213, 154, 212, 102, 6, 154, 91, 67, 242, 232, 99,
485
485
+
154, 238, 73, 72, 122, 170, 27, 110, 83, 110, 146, 159, 87, 87, 163, 151, 196, 47,
486
486
+
134, 198, 170, 197, 189, 176, 82, 247, 130, 95, 159, 217, 228, 52, 214, 63, 247,
487
487
+
169, 152, 216, 23, 182, 142, 26, 218, 248, 47, 161, 238, 180, 122, 54, 239, 38, 95,
488
488
+
65, 114, 128, 93, 233, 71, 177, 101, 2, 236, 244, 88, 249, 104, 210, 4, 249, 5,
489
489
+
172, 107, 216, 222, 165, 242, 106, 222, 22, 158, 50, 13
490
490
+
]
491
491
+
);
492
492
+
}
493
493
+
}