serde_python_derive/
lib.rs

1extern crate proc_macro;
2#[macro_use]
3extern crate quote;
4extern crate syn;
5
6use proc_macro::TokenStream;
7
8#[proc_macro_derive(Python)]
9pub fn serde_python(input: TokenStream) -> TokenStream {
10    // Parse the string representation
11    let ast = syn::parse(input).unwrap();
12
13    // Build the impl
14    impl_serde_python(&ast).into()
15}
16
17fn impl_serde_python(ast: &syn::DeriveInput) -> quote::Tokens {
18    let name = &ast.ident;
19    quote! {
20        impl serde_python::cpython::ToPyObject for #name {
21            type ObjectType = serde_python::cpython::PyObject;
22
23            fn to_py_object(&self, py: serde_python::cpython::Python) -> Self::ObjectType {
24                use serde_python::serde::Serialize;
25                self.serialize(serde_python::PyObjectSerializer::new(py)).unwrap()
26            }
27        }
28    }
29}