seraphic_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
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
use core::panic;

use darling::FromDeriveInput;
use proc_macro::{self, TokenStream};
use quote::{format_ident, quote};
use syn::{parse_macro_input, Data, DataEnum, DataStruct, DeriveInput, TypePath};

// https://github.com/imbolc/rust-derive-macro-guide
#[derive(FromDeriveInput, Default)]
#[darling(default, attributes(rpc_request))]
struct Opts {
    // formatted "type:variant"
    namespace: String,
    response: Option<String>,
}

#[proc_macro_derive(RpcRequest, attributes(rpc_request))]
pub fn derive_rpc_req(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input);
    let opts = Opts::from_derive_input(&input).expect("Wrong options");
    let DeriveInput { ident, data, .. } = input;
    match data {
        syn::Data::Struct(DataStruct { fields, .. }) => {
            let name = format!("{ident}");
            let name_no_suffix = name
                .strip_suffix("Request")
                .expect("make sure to put 'Request' at the end of your struct name");
            // let struct_name = format_ident!("{}", name_no_suffix);
            let response_struct_name = match opts.response {
                Some(res) => format_ident!("{}", res),
                None => format_ident!("{}Response", name_no_suffix),
            };
            let first_char = name_no_suffix
                .chars()
                .next()
                .unwrap()
                .to_owned()
                .to_lowercase();
            let method = format!("{first_char}{}", &name_no_suffix[1..]);

            let mut from_json_body = quote! {};
            let mut create_self_body = quote! {};

            for f in fields {
                let id = f.ident.unwrap();
                let json_name = format_ident!("{}_json", id);
                let id_string = format!("{id}");
                let not_exist = format!("field '{id_string}' does not exist");
                let not_deserialize = format!("field '{id_string}' does not implement deserialize");
                from_json_body = quote! {
                    #from_json_body
                    let #json_name =  json.get(#id_string).ok_or(#not_exist)?.to_owned();
                    let #id = serde_json::from_value(#json_name).map_err(|_|#not_deserialize)?;
                };

                create_self_body = quote! {
                    #create_self_body
                    #id,
                }
            }

            let create_self = quote! {
                Ok(Self {
                    #create_self_body
                })
            };

            let from_json = quote! {
              fn try_from_json(json: &serde_json::Value) -> seraphic::MainResult<Self> {
                    #from_json_body
                    #create_self
              }
            };

            let method_name = quote! {
                fn method()-> &'static str {
                    #method
                }
            };

            let ns = opts.namespace;
            let (ns_type, ns_var) = ns
                .split_once(':')
                .expect("expected namespace attribute to have a ':'");

            let ns_type_id = format_ident!("{ns_type}");
            let namespace = quote! {
                fn namespace() -> Namespace {
                     Self::Namespace::try_from_str(#ns_var).unwrap()

                }
            };

            let output = quote! {
                impl RpcResponse for #response_struct_name {}
                impl RpcRequest for #ident {
                    type Response = #response_struct_name;
                    type Namespace = #ns_type_id;
                    #from_json
                    #method_name
                    #namespace
                }
            };

            output.into()
        }
        _ => {
            panic!("cannot derive this on anything but a struct")
        }
    }
}

#[proc_macro_derive(RpcRequestWrapper)]
pub fn derive_req_wrapper(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input);
    let DeriveInput { ident, data, .. } = input;
    match data {
        Data::Enum(DataEnum { variants, .. }) => {
            let mut into_rpc_req_body = quote! {};
            let mut from_rpc_req_body = quote! {};
            for v in variants {
                let id = v.ident;
                let enum_typ = match v.fields {
                    syn::Fields::Unnamed(t) => match t.unnamed.iter().next().cloned().unwrap().ty {
                        syn::Type::Path(TypePath { path, .. }) => {
                            path.segments.iter().next().unwrap().ident.clone()
                        }
                        other => panic!("Expected type path as unnamed variant, got: {other:#?}"),
                    },
                    _ => panic!("only unnamed struct variants supported"),
                };
                let not_rpc_request = format!("variant {id} does not implement RpcRequest");
                into_rpc_req_body = quote! {
                    #into_rpc_req_body
                    Self::#id(rq) => rq.into_rpc_request(id).expect(#not_rpc_request),
                };

                from_rpc_req_body = quote! {
                    #from_rpc_req_body
                    if let Some(req) = #enum_typ::try_from_request(&req)? {
                        return Ok(Self::#id(req));
                    }
                };
            }

            let into_rpc_req = quote! {
                fn into_rpc_request(self, id: impl ToString) -> seraphic::socket::Request {
                    match self {
                        #into_rpc_req_body
                    }
                }
            };

            let from_rpc_req = quote! {
                fn try_from_rpc_req(req: seraphic::socket::Request) -> seraphic::MainResult<Self> {
                    #from_rpc_req_body
                    Err("Could not get request".into())
                }
            };

            let output = quote! {
                impl RpcRequestWrapper for #ident {
                    #into_rpc_req
                    #from_rpc_req

                }
            };
            output.into()
        }
        _ => {
            panic!("cannot derive this on anything but an enum")
        }
    }
}

#[proc_macro_derive(RpcNamespace)]
pub fn derive_namespace(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input);
    let DeriveInput { ident, data, .. } = input;
    match data {
        Data::Enum(DataEnum { variants, .. }) => {
            let mut from_str_body = quote! {};
            let mut as_ref_body = quote! {};
            let mut my_str_consts = quote! {};
            for v in variants {
                let id = v.ident;
                let id_str = format!("{id}");
                let const_id = format_ident!("{}", id_str.to_uppercase());
                let const_val = id_str.to_lowercase();
                my_str_consts = quote! {
                    #my_str_consts
                    const #const_id: &str = #const_val;
                };
                from_str_body = quote! {
                    #from_str_body
                    Self::#const_id => Some(Self::#id),
                };
                as_ref_body = quote! {
                    #as_ref_body
                    Self::#id => Self::#const_id,
                };
            }

            let as_str = quote! {
                fn as_str(&self)-> &str {
                    match self {
                        #as_ref_body
                    }
                }
            };

            let try_from = quote! {
                fn try_from_str(str: &str) -> Option<Self> {
                    match str {
                        #from_str_body
                        o => None,
                    }
                }
            };

            let output = quote! {
                impl #ident {
                    #my_str_consts
                }
                impl RpcNamespace for #ident {
                    #as_str
                    #try_from
                }
            };

            output.into()
        }
        _ => {
            panic!("cannot derive this on anything but an enum")
        }
    }
}