dessert_derive/
lib.rs

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
//! # Dessert-Derive
//! 
//! Provide derive macros for the `desert` crate, which provide a simpler interface to implement
//! custom SerDe `Serialize` and `Deserialize` traits.
//!

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use syn::Meta::{List};

use proc_macro::TokenStream;



fn impl_viadmacro(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = ast.ident;

    let mut qname: syn::Ident = syn::Ident::from("nothing");
    let mut tname: syn::Ident = syn::Ident::from("temp");

    for attr in ast.attrs.iter() {
        if let Some(List(list)) = attr.interpret_meta() {
            for tpl in list.nested.iter(){
                match tpl {
                   &syn::NestedMeta::Meta(syn::Meta::Word(ins)) => {tname = ins;},
                   _ => ()
                };
            }
        }


        let &syn::Path{ref segments, ..} = &attr.path;
        {
            for tpl in segments.iter(){
                let &syn::PathSegment{ref ident, ..} = tpl;
                if ident.to_string() == "via"{
                    qname = tname.to_owned();
                }
            }
        }



    }

    let impl_block = quote! {
            impl ViaDeserialize for #name { };
    };


    let impl_block_2 = quote! {
            impl<'de> serde::Deserialize<'de> for #name
            //where
            //    #qname: Into<#name>,
            //    #qname: _serde::Deserialize<'de>,
            {
                 fn deserialize<D>(deserializer: D) -> Result<#name, D::Error>
                 where
                    D: _serde::Deserializer<'de>
                 {
                    match #qname::deserialize(deserializer) {
                        Ok(x) => Ok(#name::from(x)),
                        Err(r) => Err(r),
                    }
                 }
            };
            
    };

    
    quote! {
        #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
        const _WAHT: () = {
            extern crate serde as _serde;
            #impl_block_2
            #impl_block
        };
    }

}


fn impl_viasmacro(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = ast.ident;

    let mut qname: syn::Ident = syn::Ident::from("nothing");
    let mut tname: syn::Ident = syn::Ident::from("temp");

    for attr in ast.attrs.iter() {
        if let Some(List(list)) = attr.interpret_meta() {
            for tpl in list.nested.iter(){
                match tpl {
                   &syn::NestedMeta::Meta(syn::Meta::Word(ins)) => {tname = ins;},
                   _ => ()
                };
            }
        }


        let &syn::Path{ref segments, ..} = &attr.path;
        {
            for tpl in segments.iter(){
                let &syn::PathSegment{ref ident, ..} = tpl;
                if ident.to_string() == "via"{
                    qname = tname.to_owned();
                }
            }
        }



    }

    let impl_block = quote! {
            impl ViaSerialize for #name { };
    };


    let where_clause = quote!{
        where #qname: _serde::Serialize,
              #name: Into<#qname>,
              #name: Clone
    };


    // quote does not seem to like where clauses for whaever reason.
    //println!("WHERE CLAUSE {:?} ", where_clause);

    let impl_block_2 = quote! {

        impl _serde::Serialize for #name
        #where_clause
        {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: _serde::Serializer,
            {
                let n:#name = self.clone();
                let dn:#qname = n.into();
                dn.serialize(serializer)
            }
        }


            
    };

    
    quote! {
        #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
        const _SERIALISE: () = {
            extern crate serde as _serde;
            #impl_block_2
            #impl_block
        };
    }

}

/// This function is responsible for taking a TokenStream 
/// and generate the appropriate code to derive `ViaDeserialize`.
/// use the `#[derive(ViaDeserialize)]`
#[proc_macro_derive(ViaDeserialize, attributes(via))]
pub fn viad_macro(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    let gen = impl_viadmacro(&ast);
    gen.into()
}


/// This function is responsible for taking a TokenStream 
/// and generate the appropriate code to derive `ViaSerialize`.
/// use the `#[derive(ViaSerialize)]`
#[proc_macro_derive(ViaSerialize, attributes(via))]
pub fn vias_macro(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    let gen = impl_viasmacro(&ast);
    gen.into()
}