rquickjs-serde 0.5.0

Serde support for rquickjs
Documentation
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
#![cfg_attr(not(test), no_std)]

extern crate alloc;

use rquickjs::{Ctx, Value};
use serde::Serialize;
use serde::de::DeserializeOwned;

#[doc(inline)]
pub use crate::de::Deserializer;
#[doc(inline)]
pub use crate::err::{Error, Result};
#[doc(inline)]
pub use crate::ser::Serializer;

pub mod de;
pub mod err;
pub mod ser;
#[cfg(test)]
mod test;
mod utils;

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER#description
pub const MAX_SAFE_INTEGER: i64 = 2_i64.pow(53) - 1;

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER#description
pub const MIN_SAFE_INTEGER: i64 = -MAX_SAFE_INTEGER;

/// Convert a `T` into `rquickjs::Value`
///
/// # Example
///
/// ```
/// use std::error::Error;
///
/// use serde::Serialize;
/// use rquickjs::{Runtime, Context};
///
/// #[derive(Serialize)]
/// struct User {
///     fingerprint: String,
///     location: String,
/// }
///
/// fn serialize_to_value() -> Result<(), Box<dyn Error>> {
///     let u = User {
///         fingerprint: "0xF9BA143B95FF6D82".to_owned(),
///         location: "Menlo Park, CA".to_owned(),
///     };
///
///     let rt = Runtime::new().unwrap();
///     let ctx = Context::full(&rt).unwrap();
///
///     ctx.with(|ctx| {
///         let v = rquickjs_serde::to_value(ctx, u).unwrap();
///         let obj = v.into_object().unwrap();
///
///         let fingerprint: String = obj.get("fingerprint").unwrap();
///         assert_eq!(fingerprint, "0xF9BA143B95FF6D82");
///
///         let location: String = obj.get("location").unwrap();
///         assert_eq!(location, "Menlo Park, CA");
///     });
///
///     Ok(())
/// }
/// #
/// # serialize_to_value().unwrap();
/// ```
#[inline]
pub fn to_value<T>(context: Ctx<'_>, value: T) -> Result<Value<'_>>
where
    T: Serialize,
{
    let mut serializer = Serializer::from_context(context)?;
    value.serialize(&mut serializer)?;
    Ok(serializer.value)
}

/// Interpret a `rquickjs::Value` as an instance of type `T`.
///
/// # Example
///
/// ```
/// use std::error::Error;
///
/// use serde::Deserialize;
/// use rquickjs::{Runtime, Context, Value};
///
/// #[derive(Deserialize, Debug)]
/// struct User {
///     fingerprint: String,
///     location: String,
/// }
///
/// fn deserialize_from_value() -> Result<(), Box<dyn Error>> {
///     let rt = Runtime::new().unwrap();
///     let ctx = Context::full(&rt).unwrap();
///
///     let v = ctx.with(|ctx| {
///          ctx.eval::<Value<'_>, _>("var a = {fingerprint: '0xF9BA143B95FF6D82', location: 'Menlo Park, CA'};").unwrap();
///          let val = ctx.globals().get("a").unwrap();
///          let u: User = rquickjs_serde::from_value(val).unwrap();
///          u
///     });
///
///     assert_eq!(v.fingerprint, "0xF9BA143B95FF6D82");
///     assert_eq!(v.location, "Menlo Park, CA");
///
///     Ok(())
/// }
/// #
/// # deserialize_from_value().unwrap();
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the Value does not match the
/// structure expected by `T`, for example if `T` is a struct type but the Value
/// contains something other than a JS Object. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the JS Object or some number is too big to fit in the expected primitive
/// type.
#[inline]
pub fn from_value<T>(value: Value) -> Result<T>
where
    T: DeserializeOwned,
{
    let mut deserializer = Deserializer::from(value);
    T::deserialize(&mut deserializer)
}

/// Interpret a `rquickjs::Value` as an instance of type `T` in strict mode.
///
/// Strict mode will stick to the behaviour of JSON.stringify.
///
/// See [from_value] for more information about the deserialization.
pub fn from_value_strict<T>(value: Value) -> Result<T>
where
    T: DeserializeOwned,
{
    let mut deserializer = Deserializer::from(value).with_strict();
    T::deserialize(&mut deserializer)
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use quickcheck::quickcheck;
    use serde::de::DeserializeOwned;
    use serde::{Deserialize, Serialize};

    use crate::de::Deserializer as ValueDeserializer;
    use crate::err::Result;
    use crate::ser::Serializer as ValueSerializer;
    use crate::test::Runtime;
    use crate::{MAX_SAFE_INTEGER, MIN_SAFE_INTEGER};

    quickcheck! {
        fn test_str(expected: String) -> Result<bool> {
            let actual = do_roundtrip::<_, String>(&expected);
            Ok(expected == actual)
        }

        fn test_u8(expected: u8) -> Result<bool> {
            let actual = do_roundtrip::<_, u8>(&expected);
            Ok(expected == actual)
        }

        fn test_u16(expected: u16) -> Result<bool> {
            let actual = do_roundtrip::<_, u16>(&expected);
            Ok(expected == actual)
        }

        fn test_f32(expected: f32) -> quickcheck::TestResult {
            if expected.is_nan() {
                return quickcheck::TestResult::discard();
            }

            let actual = do_roundtrip::<_, f32>(&expected);
            quickcheck::TestResult::from_bool(expected == actual)
        }

        fn test_i32(expected: i32) -> Result<bool> {
            let actual = do_roundtrip::<_, i32>(&expected);
            Ok(expected == actual)
        }

        // This test is not representative of what is happening in the real world. Since we are transcoding
        // from msgpack, only values greather than or equal to u32::MAX would be serialized as `BigInt`. Any other values would
        // be serialized as a `number`.
        //
        // See https://github.com/3Hren/msgpack-rust/blob/aa3c4a77b2b901fe73a555c615b92773b40905fc/rmp/src/encode/sint.rs#L170.
        //
        // This test works here since we are explicitly calling serialize_i64 and deserialize_i64.
        fn test_i64(expected: i64) -> Result<bool> {
            if (MIN_SAFE_INTEGER..=MAX_SAFE_INTEGER).contains(&expected) {
                let actual = do_roundtrip::<_, i64>(&expected);
                Ok(expected == actual)
            } else {
                let expected_f64 = expected as f64;
                let actual = do_roundtrip::<_, f64>(&expected_f64);
                Ok(expected_f64 == actual)
            }
        }

        fn test_u32(expected: u32) -> Result<bool> {
            let actual = do_roundtrip::<_, u32>(&expected);
            Ok(expected == actual)
        }

        // This test is not representative of what is happening in the real world. Since we are transcoding
        // from msgpack, only values larger than i64::MAX would be serialized as BigInt. Any other values would
        // be serialized as a number.
        //
        // See https://github.com/3Hren/msgpack-rust/blob/aa3c4a77b2b901fe73a555c615b92773b40905fc/rmp/src/encode/sint.rs#L170.
        //
        // This test works here since we are explicitly calling serialize_u64 and deserialize_u64.
        fn test_u64(expected: u64) -> Result<bool> {
            if expected <= MAX_SAFE_INTEGER as u64 {
                let actual = do_roundtrip::<_, u64>(&expected);
                Ok(expected == actual)
            } else {
                let expected_f64 = expected as f64;
                let actual = do_roundtrip::<_, f64>(&expected_f64);
                Ok(expected_f64 == actual)
            }
        }

        fn test_bool(expected: bool) -> Result<bool> {
            let actual = do_roundtrip::<_, bool>(&expected);
            Ok(expected == actual)
        }
    }

    #[test]
    fn test_map() {
        let mut expected = BTreeMap::<String, String>::new();
        expected.insert("foo".to_string(), "bar".to_string());
        expected.insert("hello".to_string(), "world".to_string());

        let actual = do_roundtrip::<_, BTreeMap<String, String>>(&expected);

        assert_eq!(expected, actual);
    }

    #[test]
    fn test_struct_into_map() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct MyObject {
            foo: String,
            bar: u32,
        }
        let expected = MyObject {
            foo: "hello".to_string(),
            bar: 1337,
        };

        let actual = do_roundtrip::<_, MyObject>(&expected);

        assert_eq!(expected, actual);
    }

    #[test]
    fn test_nested_maps() {
        let mut expected = BTreeMap::<String, BTreeMap<String, String>>::new();
        let mut a = BTreeMap::new();
        a.insert("foo".to_string(), "bar".to_string());
        a.insert("hello".to_string(), "world".to_string());
        let mut b = BTreeMap::new();
        b.insert("toto".to_string(), "titi".to_string());
        expected.insert("aaa".to_string(), a);
        expected.insert("bbb".to_string(), b);

        let actual = do_roundtrip::<_, BTreeMap<String, BTreeMap<String, String>>>(&expected);

        assert_eq!(expected, actual);
    }

    #[test]
    fn test_nested_structs_into_maps() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct MyObjectB {
            toto: String,
            titi: i32,
        }

        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct MyObjectA {
            foo: String,
            bar: u32,
            b: MyObjectB,
        }
        let expected = MyObjectA {
            foo: "hello".to_string(),
            bar: 1337,
            b: MyObjectB {
                toto: "world".to_string(),
                titi: -42,
            },
        };

        let actual = do_roundtrip::<_, MyObjectA>(&expected);

        assert_eq!(expected, actual);
    }

    #[test]
    fn test_sequence() {
        let expected = vec!["hello".to_string(), "world".to_string()];

        let actual = do_roundtrip::<_, Vec<String>>(&expected);

        assert_eq!(expected, actual);
    }

    #[test]
    fn test_nested_sequences() {
        let mut expected = Vec::new();
        let a = vec!["foo".to_string(), "bar".to_string()];
        let b = vec!["toto".to_string(), "tata".to_string()];
        expected.push(a);
        expected.push(b);

        let actual = do_roundtrip::<_, Vec<Vec<String>>>(&expected);

        assert_eq!(expected, actual);
    }

    #[test]
    fn test_sanity() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct MyObject {
            a: u8,
            b: u16,
            c: u32,
            d: u64,
            e: i8,
            f: i16,
            g: i32,
            h: i64,
            i: f32,
            j: f64,
            k: String,
            l: bool,
            m: BTreeMap<String, u32>,
            n: Vec<u32>,
            o: BTreeMap<String, BTreeMap<String, u32>>,
            p: Vec<Vec<u32>>,
            bb: MyObjectB,
        }

        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct MyObjectB {
            a: u32,
            cc: MyObjectC,
        }

        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct MyObjectC {
            a: Vec<u32>,
            b: BTreeMap<String, u32>,
        }

        let mut cc_b = BTreeMap::new();
        cc_b.insert("a".to_string(), 123);
        cc_b.insert("b".to_string(), 456);
        let cc = MyObjectC {
            a: vec![1337, 42],
            b: cc_b,
        };

        let bb = MyObjectB { a: 789, cc };

        let mut m = BTreeMap::new();
        m.insert("a".to_string(), 123);
        m.insert("b".to_string(), 456);
        m.insert("c".to_string(), 789);

        let mut oo = BTreeMap::new();
        oo.insert("e".to_string(), 123);

        let mut o = BTreeMap::new();
        o.insert("d".to_string(), oo);

        let expected = MyObject {
            a: u8::MAX,
            b: u16::MAX,
            c: u32::MAX,
            d: MAX_SAFE_INTEGER as u64,
            e: i8::MAX,
            f: i16::MAX,
            g: i32::MAX,
            h: MIN_SAFE_INTEGER,
            i: f32::MAX,
            j: f64::MAX,
            k: "hello world".to_string(),
            l: true,
            m,
            n: vec![1, 2, 3, 4, 5],
            o,
            p: vec![vec![1, 2], vec![3, 4, 5]],
            bb,
        };

        let actual = do_roundtrip::<_, MyObject>(&expected);

        assert_eq!(expected, actual);
    }

    fn do_roundtrip<E, A>(expected: &E) -> A
    where
        E: Serialize,
        A: DeserializeOwned,
    {
        let rt = Runtime::default();
        rt.context().with(|cx| {
            let mut serializer = ValueSerializer::from_context(cx).unwrap();
            expected.serialize(&mut serializer).unwrap();
            let mut deserializer = ValueDeserializer::from(serializer.value);
            A::deserialize(&mut deserializer).unwrap()
        })
    }
}