marshal-rs 2.0.2

Blazingly fast Ruby-lang's Marshal implementation in Rust.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#![allow(clippy::approx_constant)]
use marshal_rs::{HashMap, Object, Value, ValueType, load, load_binary};

#[test]
#[should_panic]
fn invalid_marshal_version() {
    load(b"\x04\x090", None).unwrap();
}

#[test]
fn null() {
    assert_eq!(load(b"\x04\x080", None).unwrap(), Value::new());
}

#[test]
fn boolean() {
    assert_eq!(load(b"\x04\x08T", None).unwrap(), Value::bool(true));
    assert_eq!(load(b"\x04\x08F", None).unwrap(), Value::bool(false));
}

#[test]
fn fixnum_positive() {
    assert_eq!(load(b"\x04\x08i\0", None).unwrap(), Value::int(0));
    assert_eq!(load(b"\x04\x08i\x0A", None).unwrap(), Value::int(5));
    assert_eq!(
        load(b"\x04\x08i\x02\x2C\x01", None).unwrap(),
        Value::int(300)
    );
    assert_eq!(
        load(b"\x04\x08i\x03\x70\x11\x01", None).unwrap(),
        Value::int(70000)
    );
    assert_eq!(
        load(b"\x04\x08i\x04\0\0\0\x01", None).unwrap(),
        Value::int(16777216)
    );
}

#[test]
fn fixnum_negative() {
    assert_eq!(load(b"\x04\x08i\xF6", None).unwrap(), Value::int(-5));
    assert_eq!(
        load(b"\x04\x08i\xFE\xD4\xFE", None).unwrap(),
        Value::int(-300)
    );
    assert_eq!(
        load(b"\x04\x08i\xFD\x90\xEE\xFE", None).unwrap(),
        Value::int(-70000)
    );
    assert_eq!(
        load(b"\x04\x08i\xFD\0\0\0", None).unwrap(),
        Value::int(-16777216)
    );
}

#[test]
fn bignum_positive() {
    let json = Value::bigint("36893488147419103232");
    assert_eq!(
        load(b"\x04\x08l+\n\0\0\0\0\0\0\0\0\x02\0", None).unwrap(),
        json
    );

    let json = Value::bigint("73786976294838206464");
    assert_eq!(
        load(b"\x04\x08l+\n\0\0\0\0\0\0\0\0\x04\0", None).unwrap(),
        json
    );

    let json = Value::bigint("147573952589676412928");
    assert_eq!(
        load(b"\x04\x08l+\n\0\0\0\0\0\0\0\0\x08\0", None).unwrap(),
        json
    );
}

#[test]
fn bignum_negative() {
    let json = Value::bigint("-36893488147419103232");
    assert_eq!(
        load(b"\x04\x08l-\n\0\0\0\0\0\0\0\0\x02\0", None).unwrap(),
        json
    );

    let json = Value::bigint("-73786976294838206464");
    assert_eq!(
        load(b"\x04\x08l-\n\0\0\0\0\0\0\0\0\x04\0", None).unwrap(),
        json
    );

    let json = Value::bigint("-147573952589676412928");
    assert_eq!(
        load(b"\x04\x08l-\n\0\0\0\0\0\0\0\0\x08\0", None).unwrap(),
        json
    );
}

#[test]
fn float() {
    let float = Value::float("0");
    assert_eq!(load(b"\x04\x08f\x06\x30", None).unwrap(), float);

    let float = Value::float("-0");
    assert_eq!(load(b"\x04\x08f\x07-0", None).unwrap(), float);

    let float = Value::float("3.14159");
    assert_eq!(
        load(b"\x04\x08f\x0C\x33\x2E\x31\x34\x31\x35\x39", None).unwrap(),
        float
    );

    let float = Value::float("-2.71828");
    assert_eq!(
        load(b"\x04\x08f\x0D\x2D\x32\x2E\x37\x31\x38\x32\x38", None).unwrap(),
        float
    );

    let float = Value::float("nan");
    assert_eq!(load(b"\x04\x08f\x08nan", None).unwrap(), float);

    let float = Value::float("inf");
    assert_eq!(load(b"\x04\x08f\x08inf", None).unwrap(), float);

    let float = Value::float("-inf");
    assert_eq!(load(b"\x04\x08f\t-inf", None).unwrap(), float);
}

#[test]
fn string_utf8() {
    assert_eq!(
        load(b"\x04\x08I\"\x11Short string\x06:\x06ET", None).unwrap(),
        Value::string("Short string")
    );

    assert_eq!(
        load(
            b"\x04\x08I\"\x01\xdcLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong string\x06:\x06ET",
            None
        ).unwrap(),
        Value::string("Long string".repeat(20)),
    );
}

#[test]
fn string_nonutf8() {
    assert_eq!(
        load(
            b"\x04\x08I\"\x0b\xBA\xBA\xD7\xD6\xC4\xDA\x06:\rencoding\"\x08GBK",
            None
        )
        .unwrap(),
        Value::string("汉字内")
    );
}

#[test]
fn string_binary() {
    let json = Value::bytes("Short string".as_bytes());
    assert_eq!(
        load_binary(b"\x04\x08I\"\x11Short string\x06:\x06ET", None).unwrap(),
        json
    );

    let json = Value::bytes("Long string".repeat(20).as_bytes());
    assert_eq!(
        load_binary(
            b"\x04\x08I\"\x01\xdcLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong string\x06:\x06ET",
            None
        ).unwrap(),
        json,
    );
}

#[test]
#[should_panic]
fn invalid_string() {
    // length of string is 4, which is equal to 0x09, but 0x10 length is passed
    load(b"\x04\x08\"\x10\xf0(\x8c(", None).unwrap();
}

#[test]
fn links() {
    let first_float = Value::float("0.1");
    let second_float = Value::float("0.2");
    let third_float = Value::float("0.3");

    let json = vec![
        Value::array(vec![
            first_float.clone(),
            first_float.clone(),
            first_float.clone(),
        ]),
        Value::array(vec![
            second_float.clone(),
            second_float.clone(),
            second_float.clone(),
        ]),
        Value::array(vec![
            third_float.clone(),
            third_float.clone(),
            third_float.clone(),
        ]),
    ];

    assert_eq!(
        load(
            b"\x04\x08[\x08[\x08f\x080.1@\x07@\x07[\x08f\x080.2@\x09@\x09[\x08f\x080.3@\x0b@\x0b",
            None
        )
        .unwrap(),
        Value::array(json)
    );
}

#[test]
fn array() {
    let integer_key = Value::int(5);
    let map = HashMap::from_iter([(integer_key, Value::int(6))]);
    let map_json = Value::from(ValueType::HashMap(map));

    let float_value = Value::float("3");

    let inner_array = vec![Value::int(4)];
    let inner_array_value = Value::array(inner_array);
    let array = vec![
        Value::int(1),
        Value::string("two"),
        float_value,
        inner_array_value,
        map_json,
    ];
    let json = Value::array(array);

    assert_eq!(
        load(
            b"\x04\x08[\x0ai\x06I\"\x08two\x06:\x06ETf\x063[\x06i\x09{\x06i\x0ai\x0b",
            None
        )
        .unwrap(),
        json
    );
}

#[test]
fn hash() {
    let mut object = Value::object(Object::new());
    object.set_class("Object".into());

    let map = HashMap::from_iter([
        (Value::int(1), Value::string("one")),
        (Value::string("two"), Value::int(2)),
        (object, Value::null()),
    ]);
    let map = Value::from(ValueType::HashMap(map));

    assert_eq!(
        load(
            b"\x04\x08{\x08i\x06I\"\x08one\x06:\x06ETI\"\x08two\x06;\0Ti\x07o:\x0bObject\x000",
            None
        )
        .unwrap(),
        map
    );

    let default_symbol = Value::symbol("__ruby_default__");
    let map = Value::from(ValueType::HashMap(HashMap::from_iter([(
        default_symbol,
        Value::string("default"),
    )])));

    assert_eq!(
        load(b"\x04\x08}\0I\"\x0cdefault\x06:\x06ET", None).unwrap(),
        map
    );
}

#[test]
fn ruby_struct() {
    let age_key = Value::symbol("age");
    let name_key = Value::symbol("name");
    let map = HashMap::from_iter([
        (name_key, Value::string("Alice")),
        (age_key, Value::int(30)),
    ]);

    let mut json = Value::rstruct(map);
    json.set_class("Person".to_owned());

    assert_eq!(
        load(
            b"\x04\x08S:\x0bPerson\x07:\x09nameI\"\x0aAlice\x06:\x06ET:\x08agei#",
            None
        )
        .unwrap(),
        json
    );
}

#[test]
fn object() {
    let mut json = Value::object(Object::from_iter([(
        "@data".into(),
        Value::string("object data"),
    )]));
    json.set_class("CustomObject".into());

    assert_eq!(
        load(
            b"\x04\x08o:\x11CustomObject\x06:\x0a@dataI\"\x10object data\x06:\x06ET",
            None
        )
        .unwrap(),
        json
    );
}

#[test]
fn custom_marshal() {
    let mut json = Value::hash(HashMap::from_iter([(
        Value::symbol("data"),
        Value::string("Important Data"),
    )]));
    json.set_class("CustomDumpClass".into());
    json.set_user_marshal(true);

    assert_eq!(
        load(b"\x04\x08U:\x14CustomDumpClass{\x06:\tdataI\"\x13Important Data\x06:\x06ET", None ).unwrap(),
        json
    )
}

#[test]
fn extended_object() {
    let mut json = Value::bytes("I am a string".as_bytes());
    json.add_extension("MyModule".into());

    assert_eq!(
        load_binary(
            b"\x04\x08Ie:\rMyModule\"\x12I am a string\x06:\x06ET",
            None
        )
        .unwrap(),
        json
    );
}

#[test]
fn module() {
    let mut json = Value::module();
    json.set_class("MyModule".into());
    json.set_old_module(false);

    assert_eq!(load(b"\x04\x08m\rMyModule", None).unwrap(), json)
}

#[test]
fn regexp_with_encoding() {
    let json = Value::regexp("/aboba.*/ix");
    assert_eq!(
        load(b"\x04\x08I/\x0caboba.*\x03\x06:\x06EF", None).unwrap(),
        json
    )
}

#[test]
fn regexp_without_encoding() {
    let json = Value::regexp("/aboba.*/ix");
    assert_eq!(load(b"\x04\x08/\x0caboba.*\x03", None).unwrap(), json)
}

#[test]
fn custom_dump_and_load() {
    let mut json = Value::string("terces");
    json.set_class("CustomObject".into());
    json.set_user_defined(true);

    assert_eq!(
        load(b"\x04\x08Iu:\x11CustomObject\x0bterces\x06:\x06ET", None)
            .unwrap(),
        json
    );
}

#[test]
fn array_subclass() {
    let mut json = Value::array([Value::int(1), Value::int(2), Value::int(3)]);
    json.set_class("MyArray".into());
    json.set_user_class(true);

    assert_eq!(
        load(b"\x04\x08C:\x0cMyArray[\x08i\x06i\x07i\x08", None).unwrap(),
        json
    );
}

#[test]
fn string_subclass() {
    let mut json = Value::string("hello");
    json.set_class("MyString".into());
    json.set_user_class(true);

    assert_eq!(
        load(b"\x04\x08IC:\rMyString\"\nhello\x06:\x06ET", None).unwrap(),
        json
    );
}

#[test]
fn regexp_subclass() {
    let mut json = Value::regexp("/foo.*bar/");
    json.set_class("MyRegexp".into());
    json.set_user_class(true);

    assert_eq!(
        load(b"\x04\x08IC:\rMyRegexp/\rfoo.*bar\x00\x06:\x06EF", None).unwrap(),
        json
    );
}

#[test]
fn hash_subclass() {
    let a_key = Value::symbol("a");
    let b_key = Value::symbol("b");
    let map =
        HashMap::from_iter([(a_key, Value::int(1)), (b_key, Value::int(2))]);

    let mut json = Value::hash(map);
    json.set_class("MyHash".into());
    json.set_user_class(true);

    assert_eq!(
        load(b"\x04\x08C:\x0bMyHash{\x07:\x06ai\x06:\x06bi\x07", None).unwrap(),
        json
    );
}