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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
use std::marker::PhantomData;

use pyo3::types::{
    PyAnyMethods, PyDict, PyDictMethods, PyList, PyMapping, PySequence, PyString, PyTuple,
    PyTupleMethods,
};
use pyo3::{Bound, IntoPy, PyAny, PyResult, Python, ToPyObject};
use serde::{ser, Serialize};

use crate::error::{PythonizeError, Result};

// TODO: move 'py lifetime into builder once GATs are available in MSRV
/// Trait for types which can represent a Python mapping
pub trait PythonizeMappingType<'py> {
    /// Builder type for Python mappings
    type Builder;

    /// Create a builder for a Python mapping
    fn builder(py: Python<'py>, len: Option<usize>) -> PyResult<Self::Builder>;

    /// Adds the key-value item to the mapping being built
    fn push_item(
        builder: &mut Self::Builder,
        key: Bound<'py, PyAny>,
        value: Bound<'py, PyAny>,
    ) -> PyResult<()>;

    /// Build the Python mapping
    fn finish(builder: Self::Builder) -> PyResult<Bound<'py, PyMapping>>;
}

// TODO: move 'py lifetime into builder once GATs are available in MSRV
/// Trait for types which can represent a Python mapping and have a name
pub trait PythonizeNamedMappingType<'py> {
    /// Builder type for Python mappings with a name
    type Builder;

    /// Create a builder for a Python mapping with a name
    fn builder(py: Python<'py>, len: usize, name: &'static str) -> PyResult<Self::Builder>;

    /// Adds the field to the named mapping being built
    fn push_field(
        builder: &mut Self::Builder,
        name: Bound<'py, PyString>,
        value: Bound<'py, PyAny>,
    ) -> PyResult<()>;

    /// Build the Python mapping
    fn finish(builder: Self::Builder) -> PyResult<Bound<'py, PyMapping>>;
}

/// Trait for types which can represent a Python sequence
pub trait PythonizeListType: Sized {
    /// Constructor
    fn create_sequence<T, U>(
        py: Python,
        elements: impl IntoIterator<Item = T, IntoIter = U>,
    ) -> PyResult<Bound<PySequence>>
    where
        T: ToPyObject,
        U: ExactSizeIterator<Item = T>;
}

// TODO: remove 'py lifetime once GATs are available in MSRV
/// Custom types for serialization
pub trait PythonizeTypes<'py> {
    /// Python map type (should be representable as python mapping)
    type Map: PythonizeMappingType<'py>;
    /// Python (struct-like) named map type (should be representable as python mapping)
    type NamedMap: PythonizeNamedMappingType<'py>;
    /// Python sequence type (should be representable as python sequence)
    type List: PythonizeListType;
}

impl<'py> PythonizeMappingType<'py> for PyDict {
    type Builder = Bound<'py, Self>;

    fn builder(py: Python<'py>, _len: Option<usize>) -> PyResult<Self::Builder> {
        Ok(Self::new_bound(py))
    }

    fn push_item(
        builder: &mut Self::Builder,
        key: Bound<'py, PyAny>,
        value: Bound<'py, PyAny>,
    ) -> PyResult<()> {
        builder.set_item(key, value)
    }

    fn finish(builder: Self::Builder) -> PyResult<Bound<'py, PyMapping>> {
        Ok(builder.into_mapping())
    }
}

/// Adapter type to use an unnamed mapping type, i.e. one that implements
/// [`PythonizeMappingType`], as a named mapping type, i.e. one that implements
/// [`PythonizeNamedMappingType`]. The adapter simply drops the provided name.
///
/// This adapter is commonly applied to use the same unnamed mapping type for
/// both [`PythonizeTypes::Map`] and [`PythonizeTypes::NamedMap`] while only
/// implementing [`PythonizeMappingType`].
pub struct PythonizeUnnamedMappingAdapter<'py, T: PythonizeMappingType<'py>> {
    _unnamed: T,
    _marker: PhantomData<&'py ()>,
}

impl<'py, T: PythonizeMappingType<'py>> PythonizeNamedMappingType<'py>
    for PythonizeUnnamedMappingAdapter<'py, T>
{
    type Builder = <T as PythonizeMappingType<'py>>::Builder;

    fn builder(py: Python<'py>, len: usize, _name: &'static str) -> PyResult<Self::Builder> {
        <T as PythonizeMappingType>::builder(py, Some(len))
    }

    fn push_field(
        builder: &mut Self::Builder,
        name: Bound<'py, PyString>,
        value: Bound<'py, PyAny>,
    ) -> PyResult<()> {
        <T as PythonizeMappingType>::push_item(builder, name.into_any(), value)
    }

    fn finish(builder: Self::Builder) -> PyResult<Bound<'py, PyMapping>> {
        <T as PythonizeMappingType>::finish(builder)
    }
}

impl PythonizeListType for PyList {
    fn create_sequence<T, U>(
        py: Python,
        elements: impl IntoIterator<Item = T, IntoIter = U>,
    ) -> PyResult<Bound<PySequence>>
    where
        T: ToPyObject,
        U: ExactSizeIterator<Item = T>,
    {
        Ok(PyList::new_bound(py, elements)
            .into_any()
            .downcast_into()
            .unwrap())
    }
}

impl PythonizeListType for PyTuple {
    fn create_sequence<T, U>(
        py: Python,
        elements: impl IntoIterator<Item = T, IntoIter = U>,
    ) -> PyResult<Bound<PySequence>>
    where
        T: ToPyObject,
        U: ExactSizeIterator<Item = T>,
    {
        Ok(PyTuple::new_bound(py, elements).into_sequence())
    }
}

pub struct PythonizeDefault;

impl<'py> PythonizeTypes<'py> for PythonizeDefault {
    type Map = PyDict;
    type NamedMap = PythonizeUnnamedMappingAdapter<'py, PyDict>;
    type List = PyList;
}

/// Attempt to convert the given data into a Python object
pub fn pythonize<'py, T>(py: Python<'py>, value: &T) -> Result<Bound<'py, PyAny>>
where
    T: ?Sized + Serialize,
{
    value.serialize(Pythonizer::new(py))
}

/// Attempt to convert the given data into a Python object.
/// Also uses custom mapping python class for serialization.
pub fn pythonize_custom<'py, P, T>(py: Python<'py>, value: &T) -> Result<Bound<'py, PyAny>>
where
    T: ?Sized + Serialize,
    P: PythonizeTypes<'py>,
{
    value.serialize(Pythonizer::custom::<P>(py))
}

/// A structure that serializes Rust values into Python objects
#[derive(Clone, Copy)]
pub struct Pythonizer<'py, P> {
    py: Python<'py>,
    _types: PhantomData<P>,
}

impl<'py, P> From<Python<'py>> for Pythonizer<'py, P> {
    fn from(py: Python<'py>) -> Self {
        Self {
            py,
            _types: PhantomData,
        }
    }
}

impl<'py> Pythonizer<'py, PythonizeDefault> {
    /// Creates a serializer to convert data into a Python object using the default mapping class
    pub fn new(py: Python<'py>) -> Self {
        Self::from(py)
    }

    /// Creates a serializer to convert data into a Python object using a custom mapping class
    pub fn custom<P>(py: Python<'py>) -> Pythonizer<'py, P> {
        Pythonizer::from(py)
    }
}

#[doc(hidden)]
pub struct PythonCollectionSerializer<'py, P> {
    items: Vec<Bound<'py, PyAny>>,
    py: Python<'py>,
    _types: PhantomData<P>,
}

#[doc(hidden)]
pub struct PythonTupleVariantSerializer<'py, P> {
    name: &'static str,
    variant: &'static str,
    inner: PythonCollectionSerializer<'py, P>,
}

#[doc(hidden)]
pub struct PythonStructVariantSerializer<'py, P: PythonizeTypes<'py>> {
    name: &'static str,
    variant: &'static str,
    inner: PythonStructDictSerializer<'py, P>,
}

#[doc(hidden)]
pub struct PythonStructDictSerializer<'py, P: PythonizeTypes<'py>> {
    py: Python<'py>,
    builder: <P::NamedMap as PythonizeNamedMappingType<'py>>::Builder,
    _types: PhantomData<P>,
}

#[doc(hidden)]
pub struct PythonMapSerializer<'py, P: PythonizeTypes<'py>> {
    py: Python<'py>,
    builder: <P::Map as PythonizeMappingType<'py>>::Builder,
    key: Option<Bound<'py, PyAny>>,
    _types: PhantomData<P>,
}

impl<'py, P: PythonizeTypes<'py>> ser::Serializer for Pythonizer<'py, P> {
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;
    type SerializeSeq = PythonCollectionSerializer<'py, P>;
    type SerializeTuple = PythonCollectionSerializer<'py, P>;
    type SerializeTupleStruct = PythonCollectionSerializer<'py, P>;
    type SerializeTupleVariant = PythonTupleVariantSerializer<'py, P>;
    type SerializeMap = PythonMapSerializer<'py, P>;
    type SerializeStruct = PythonStructDictSerializer<'py, P>;
    type SerializeStructVariant = PythonStructVariantSerializer<'py, P>;

    fn serialize_bool(self, v: bool) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_i8(self, v: i8) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_i16(self, v: i16) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_i32(self, v: i32) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_i64(self, v: i64) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_u8(self, v: u8) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_u16(self, v: u16) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_u32(self, v: u32) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_u64(self, v: u64) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_f32(self, v: f32) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_f64(self, v: f64) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_char(self, v: char) -> Result<Bound<'py, PyAny>> {
        self.serialize_str(&v.to_string())
    }

    fn serialize_str(self, v: &str) -> Result<Bound<'py, PyAny>> {
        Ok(PyString::new_bound(self.py, v).into_any())
    }

    fn serialize_bytes(self, v: &[u8]) -> Result<Bound<'py, PyAny>> {
        Ok(v.into_py(self.py).into_bound(self.py))
    }

    fn serialize_none(self) -> Result<Bound<'py, PyAny>> {
        Ok(self.py.None().into_bound(self.py))
    }

    fn serialize_some<T>(self, value: &T) -> Result<Bound<'py, PyAny>>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(self)
    }

    fn serialize_unit(self) -> Result<Bound<'py, PyAny>> {
        self.serialize_none()
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<Bound<'py, PyAny>> {
        self.serialize_none()
    }

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        variant: &'static str,
    ) -> Result<Bound<'py, PyAny>> {
        self.serialize_str(variant)
    }

    fn serialize_newtype_struct<T>(
        self,
        _name: &'static str,
        value: &T,
    ) -> Result<Bound<'py, PyAny>>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(self)
    }

    fn serialize_newtype_variant<T>(
        self,
        name: &'static str,
        _variant_index: u32,
        variant: &'static str,
        value: &T,
    ) -> Result<Bound<'py, PyAny>>
    where
        T: ?Sized + Serialize,
    {
        let mut m = P::NamedMap::builder(self.py, 1, name)?;
        P::NamedMap::push_field(
            &mut m,
            PyString::new_bound(self.py, variant),
            value.serialize(self)?,
        )?;
        Ok(P::NamedMap::finish(m)?.into_any())
    }

    fn serialize_seq(self, len: Option<usize>) -> Result<PythonCollectionSerializer<'py, P>> {
        let items = match len {
            Some(len) => Vec::with_capacity(len),
            None => Vec::new(),
        };
        Ok(PythonCollectionSerializer {
            items,
            py: self.py,
            _types: PhantomData,
        })
    }

    fn serialize_tuple(self, len: usize) -> Result<PythonCollectionSerializer<'py, P>> {
        Ok(PythonCollectionSerializer {
            items: Vec::with_capacity(len),
            py: self.py,
            _types: PhantomData,
        })
    }

    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        len: usize,
    ) -> Result<PythonCollectionSerializer<'py, P>> {
        self.serialize_tuple(len)
    }

    fn serialize_tuple_variant(
        self,
        name: &'static str,
        _variant_index: u32,
        variant: &'static str,
        len: usize,
    ) -> Result<PythonTupleVariantSerializer<'py, P>> {
        let inner = self.serialize_tuple(len)?;
        Ok(PythonTupleVariantSerializer {
            name,
            variant,
            inner,
        })
    }

    fn serialize_map(self, len: Option<usize>) -> Result<PythonMapSerializer<'py, P>> {
        Ok(PythonMapSerializer {
            builder: P::Map::builder(self.py, len)?,
            key: None,
            py: self.py,
            _types: PhantomData,
        })
    }

    fn serialize_struct(
        self,
        name: &'static str,
        len: usize,
    ) -> Result<PythonStructDictSerializer<'py, P>> {
        Ok(PythonStructDictSerializer {
            py: self.py,
            builder: P::NamedMap::builder(self.py, len, name)?,
            _types: PhantomData,
        })
    }

    fn serialize_struct_variant(
        self,
        name: &'static str,
        _variant_index: u32,
        variant: &'static str,
        len: usize,
    ) -> Result<PythonStructVariantSerializer<'py, P>> {
        Ok(PythonStructVariantSerializer {
            name,
            variant,
            inner: PythonStructDictSerializer {
                py: self.py,
                builder: P::NamedMap::builder(self.py, len, variant)?,
                _types: PhantomData,
            },
        })
    }
}

impl<'py, P: PythonizeTypes<'py>> ser::SerializeSeq for PythonCollectionSerializer<'py, P> {
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;

    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        self.items.push(pythonize_custom::<P, _>(self.py, value)?);
        Ok(())
    }

    fn end(self) -> Result<Bound<'py, PyAny>> {
        let instance = P::List::create_sequence(self.py, self.items)?;
        Ok(instance.to_object(self.py).into_bound(self.py))
    }
}

impl<'py, P: PythonizeTypes<'py>> ser::SerializeTuple for PythonCollectionSerializer<'py, P> {
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;

    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        ser::SerializeSeq::serialize_element(self, value)
    }

    fn end(self) -> Result<Bound<'py, PyAny>> {
        Ok(PyTuple::new_bound(self.py, self.items).into_any())
    }
}

impl<'py, P: PythonizeTypes<'py>> ser::SerializeTupleStruct for PythonCollectionSerializer<'py, P> {
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;

    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        ser::SerializeSeq::serialize_element(self, value)
    }

    fn end(self) -> Result<Bound<'py, PyAny>> {
        ser::SerializeTuple::end(self)
    }
}

impl<'py, P: PythonizeTypes<'py>> ser::SerializeTupleVariant
    for PythonTupleVariantSerializer<'py, P>
{
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;

    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        ser::SerializeSeq::serialize_element(&mut self.inner, value)
    }

    fn end(self) -> Result<Bound<'py, PyAny>> {
        let mut m = P::NamedMap::builder(self.inner.py, 1, self.name)?;
        P::NamedMap::push_field(
            &mut m,
            PyString::new_bound(self.inner.py, self.variant),
            ser::SerializeTuple::end(self.inner)?,
        )?;
        Ok(P::NamedMap::finish(m)?.into_any())
    }
}

impl<'py, P: PythonizeTypes<'py>> ser::SerializeMap for PythonMapSerializer<'py, P> {
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;

    fn serialize_key<T>(&mut self, key: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        self.key = Some(pythonize_custom::<P, _>(self.py, key)?);
        Ok(())
    }

    fn serialize_value<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        P::Map::push_item(
            &mut self.builder,
            self.key
                .take()
                .expect("serialize_value should always be called after serialize_key"),
            pythonize_custom::<P, _>(self.py, value)?,
        )?;
        Ok(())
    }

    fn end(self) -> Result<Bound<'py, PyAny>> {
        Ok(P::Map::finish(self.builder)?.into_any())
    }
}

impl<'py, P: PythonizeTypes<'py>> ser::SerializeStruct for PythonStructDictSerializer<'py, P> {
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;

    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        P::NamedMap::push_field(
            &mut self.builder,
            PyString::new_bound(self.py, key),
            pythonize_custom::<P, _>(self.py, value)?,
        )?;
        Ok(())
    }

    fn end(self) -> Result<Bound<'py, PyAny>> {
        Ok(P::NamedMap::finish(self.builder)?.into_any())
    }
}

impl<'py, P: PythonizeTypes<'py>> ser::SerializeStructVariant
    for PythonStructVariantSerializer<'py, P>
{
    type Ok = Bound<'py, PyAny>;
    type Error = PythonizeError;

    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        P::NamedMap::push_field(
            &mut self.inner.builder,
            PyString::new_bound(self.inner.py, key),
            pythonize_custom::<P, _>(self.inner.py, value)?,
        )?;
        Ok(())
    }

    fn end(self) -> Result<Bound<'py, PyAny>> {
        let v = P::NamedMap::finish(self.inner.builder)?;
        let mut m = P::NamedMap::builder(self.inner.py, 1, self.name)?;
        P::NamedMap::push_field(
            &mut m,
            PyString::new_bound(self.inner.py, self.variant),
            v.into_any(),
        )?;
        Ok(P::NamedMap::finish(m)?.into_any())
    }
}

#[cfg(test)]
mod test {
    use super::pythonize;
    use maplit::hashmap;
    use pyo3::prelude::*;
    use pyo3::pybacked::PyBackedStr;
    use pyo3::types::{PyBytes, PyDict};
    use serde::Serialize;

    fn test_ser<T>(src: T, expected: &str)
    where
        T: Serialize,
    {
        Python::with_gil(|py| -> PyResult<()> {
            let obj = pythonize(py, &src)?;

            let locals = PyDict::new_bound(py);
            locals.set_item("obj", obj)?;

            py.run_bound(
                "import json; result = json.dumps(obj, separators=(',', ':'))",
                None,
                Some(&locals),
            )?;
            let result = locals.get_item("result")?.unwrap();
            let result = result.extract::<PyBackedStr>()?;

            assert_eq!(result, expected);
            assert_eq!(serde_json::to_string(&src).unwrap(), expected);

            Ok(())
        })
        .unwrap();
    }

    #[test]
    fn test_empty_struct() {
        #[derive(Serialize)]
        struct Empty;

        test_ser(Empty, "null");
    }

    #[test]
    fn test_struct() {
        #[derive(Serialize)]
        struct Struct {
            foo: String,
            bar: usize,
        }

        test_ser(
            Struct {
                foo: "foo".to_string(),
                bar: 5,
            },
            r#"{"foo":"foo","bar":5}"#,
        );
    }

    #[test]
    fn test_nested_struct() {
        #[derive(Serialize)]
        struct Foo {
            name: String,
            bar: Bar,
        }

        #[derive(Serialize)]
        struct Bar {
            name: String,
        }

        test_ser(
            Foo {
                name: "foo".to_string(),
                bar: Bar {
                    name: "bar".to_string(),
                },
            },
            r#"{"name":"foo","bar":{"name":"bar"}}"#,
        )
    }

    #[test]
    fn test_tuple_struct() {
        #[derive(Serialize)]
        struct TupleStruct(String, usize);

        test_ser(TupleStruct("foo".to_string(), 5), r#"["foo",5]"#);
    }

    #[test]
    fn test_tuple() {
        test_ser(("foo", 5), r#"["foo",5]"#);
    }

    #[test]
    fn test_vec() {
        test_ser(vec![1, 2, 3], r#"[1,2,3]"#);
    }

    #[test]
    fn test_map() {
        test_ser(hashmap! {"foo" => "foo"}, r#"{"foo":"foo"}"#);
    }

    #[test]
    fn test_enum_unit_variant() {
        #[derive(Serialize)]
        enum E {
            Empty,
        }

        test_ser(E::Empty, r#""Empty""#);
    }

    #[test]
    fn test_enum_tuple_variant() {
        #[derive(Serialize)]
        enum E {
            Tuple(i32, String),
        }

        test_ser(E::Tuple(5, "foo".to_string()), r#"{"Tuple":[5,"foo"]}"#);
    }

    #[test]
    fn test_enum_newtype_variant() {
        #[derive(Serialize)]
        enum E {
            NewType(String),
        }

        test_ser(E::NewType("foo".to_string()), r#"{"NewType":"foo"}"#);
    }

    #[test]
    fn test_enum_struct_variant() {
        #[derive(Serialize)]
        enum E {
            Struct { foo: String, bar: usize },
        }

        test_ser(
            E::Struct {
                foo: "foo".to_string(),
                bar: 5,
            },
            r#"{"Struct":{"foo":"foo","bar":5}}"#,
        );
    }

    #[test]
    fn test_integers() {
        #[derive(Serialize)]
        struct Integers {
            a: i8,
            b: i16,
            c: i32,
            d: i64,
            e: u8,
            f: u16,
            g: u32,
            h: u64,
        }

        test_ser(
            Integers {
                a: 1,
                b: 2,
                c: 3,
                d: 4,
                e: 5,
                f: 6,
                g: 7,
                h: 8,
            },
            r#"{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8}"#,
        )
    }

    #[test]
    fn test_floats() {
        #[derive(Serialize)]
        struct Floats {
            a: f32,
            b: f64,
        }

        test_ser(Floats { a: 1.0, b: 2.0 }, r#"{"a":1.0,"b":2.0}"#)
    }

    #[test]
    fn test_char() {
        #[derive(Serialize)]
        struct Char {
            a: char,
        }

        test_ser(Char { a: 'a' }, r#"{"a":"a"}"#)
    }

    #[test]
    fn test_bool() {
        test_ser(true, "true");
        test_ser(false, "false");
    }

    #[test]
    fn test_none() {
        #[derive(Serialize)]
        struct S;

        test_ser((), "null");
        test_ser(S, "null");

        test_ser(Some(1), "1");
        test_ser(None::<i32>, "null");
    }

    #[test]
    fn test_bytes() {
        // serde treats &[u8] as a sequence of integers due to lack of specialization
        test_ser(b"foo", "[102,111,111]");

        Python::with_gil(|py| {
            assert!(pythonize(py, serde_bytes::Bytes::new(b"foo"))
                .expect("bytes will always serialize successfully")
                .eq(&PyBytes::new_bound(py, b"foo"))
                .expect("bytes will always compare successfully"));
        });
    }
}