nadi_plugin 0.3.0

Macro library to write plugins for nadi system using nadi_core
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
use convert_case::{Case, Casing};
use std::collections::HashMap;

use proc_macro::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{
    parse_macro_input, punctuated::Punctuated, token::Comma, Attribute, Expr, FnArg, Ident, ItemFn,
    ItemMod, Lit, MetaNameValue, Type,
};

fn nadi_struct_name(name: &Ident, suff: &str) -> Ident {
    format_ident!("{}{suff}", name.to_string().to_case(Case::UpperCamel))
}

fn nadi_func_impl(node: bool) -> proc_macro2::TokenStream {
    if node {
        quote! {::nadi_core::functions::NodeFunction}
    } else {
        quote! {::nadi_core::functions::NetworkFunction}
    }
}

/// use it on node function
#[proc_macro_attribute]
pub fn node_func(args: TokenStream, item: TokenStream) -> TokenStream {
    nadi_func(args, item, true)
}

/// use it on network function
#[proc_macro_attribute]
pub fn network_func(args: TokenStream, item: TokenStream) -> TokenStream {
    nadi_func(args, item, false)
}

#[derive(Clone, Copy, Default)]
enum FuncArgType {
    #[default]
    Arg,
    Relaxed,
    Args,
    KwArgs,
}

const FUNC_ARG_ATTRS: [(&str, FuncArgType); 3] = [
    ("args", FuncArgType::Args),
    ("kwargs", FuncArgType::KwArgs),
    ("relaxed", FuncArgType::Relaxed),
];

fn nadi_func(args: TokenStream, item: TokenStream, node: bool) -> TokenStream {
    let args = parse_macro_input!(args with Punctuated<MetaNameValue, Comma>::parse_terminated);
    let default_args: HashMap<Ident, Expr> = args
        .into_iter()
        .map(|p| (p.path.segments.first().unwrap().ident.clone(), p.value))
        .collect();
    let item = parse_macro_input!(item as ItemFn);
    let arg0 = item
        .sig
        .inputs
        .first()
        .expect("Needs at least one parameter");
    let func_args: Vec<(&Ident, &Type, FuncArgType)> = item
        .sig
        .inputs
        .iter()
        .skip(1) // skip first argument, which is probably node or network
        .map(get_fn_arg)
        .collect();

    let func_struct_name = nadi_struct_name(&item.sig.ident, if node { "Node" } else { "Network" });

    let name_func = get_name_func(&item);
    let code_func = get_code_func(&item);
    let help_func = get_help_func(&item, &default_args);
    let call_func = get_call_func(&item, arg0, &func_args, &default_args, &func_struct_name);
    let impl_trait = nadi_func_impl(node);

    let clean_func = clean_function(&item);
    quote! {
        #[derive(Debug)]
        pub struct #func_struct_name;

        impl #impl_trait for #func_struct_name {
                #name_func
        #code_func
        #help_func

            #call_func
        }

        impl  #func_struct_name {
                #clean_func
            }
    }
    .into()
}

/// Clean the function of function argument attributes like #[args], ...
fn clean_function(func: &ItemFn) -> ItemFn {
    let mut func = func.clone();
    for arg in &mut func.sig.inputs {
        match arg {
            syn::FnArg::Typed(ref mut a) => {
                let attrs = std::mem::take(&mut a.attrs);
                let (_, remain): (Vec<_>, Vec<_>) = attrs
                    .into_iter()
                    .partition(|at| FUNC_ARG_ATTRS.iter().any(|a| at.path().is_ident(a.0)));

                a.attrs = remain;
            }
            _ => panic!("unsuported args"),
        }
    }
    func
}

fn get_fn_arg(arg: &FnArg) -> (&Ident, &Type, FuncArgType) {
    match arg {
        syn::FnArg::Typed(arg) => {
            let t: FuncArgType = FUNC_ARG_ATTRS
                .iter()
                .filter(|a| arg.attrs.iter().any(|at| at.path().is_ident(a.0)))
                .map(|a| a.1)
                .next()
                .unwrap_or_default();
            match arg.pat.as_ref() {
                syn::Pat::Ident(i) => (&i.ident, arg.ty.as_ref(), t),
                _ => panic!("Invalid Argument Type for Nadi function"),
            }
        }
        _ => panic!("Invalid Argument Type for Nadi function"),
    }
}

/// this will be on the top level of the mod, will have access to all
/// the functions so it can see all functions and register them
#[proc_macro_attribute]
pub fn nadi_plugin(args: TokenStream, item: TokenStream) -> TokenStream {
    nadi_export_plugin(args, item, true)
}

/// this will be on the top level of the mod, will have access to all
/// the functions so it can see all functions and register them
#[proc_macro_attribute]
pub fn nadi_internal_plugin(args: TokenStream, item: TokenStream) -> TokenStream {
    nadi_export_plugin(args, item, false)
}

fn nadi_export_plugin(_args: TokenStream, item: TokenStream, external: bool) -> TokenStream {
    let item = parse_macro_input!(item as ItemMod);

    let name = &item.ident;
    let name_s = name.to_string();
    let name_mod = nadi_struct_name(name, "Mod");
    let node_funcs = get_nadi_functions(&item, "node_func");
    let network_funcs = get_nadi_functions(&item, "network_func");
    let regis_node_funcs = node_funcs.iter().map(|n| nadi_struct_name(n, "Node")).map(|n| {
        quote! {
            nf.register_node_function(::nadi_core::functions::NodeFunction_TO::from_value(#n, ::abi_stable::sabi_trait::TD_CanDowncast));
        }
    });

    let regis_network_funcs = network_funcs
        .iter()
        .map(|n| nadi_struct_name(n, "Network"))
        .map(|n| {
            quote! {
		nf.register_network_function(::nadi_core::functions::NetworkFunction_TO::from_value(#n, ::abi_stable::sabi_trait::TD_CanDowncast));
            }
        });

    if external {
        quote! {
            #[::abi_stable::export_root_module]
            pub fn get_library() -> ::nadi_core::plugins::NadiExternalPlugin_Ref {
        ::abi_stable::prefix_type::PrefixTypeTrait::leak_into_prefix(
        ::nadi_core::plugins::NadiExternalPlugin {
            register_functions,
            plugin_name,
        })
            }

            #[::abi_stable::sabi_extern_fn]
            fn plugin_name() -> ::abi_stable::std_types::RString {
                #name_s .into()
            }

            #[::abi_stable::sabi_extern_fn]
            fn register_functions(nf: &mut ::nadi_core::functions::NadiFunctions) {

                #(#regis_node_funcs)*

                #(#regis_network_funcs)*
            }

            use #name::*;

            #item
        }
    } else {
        quote! {
            pub struct #name_mod;

        impl ::nadi_core::plugins::NadiPlugin for #name_mod {
            fn name(&self) -> ::abi_stable::std_types::RString {
                #name_s .into()
            }
            fn register(&self, nf: &mut ::nadi_core::functions::NadiFunctions) {

                #(#regis_node_funcs)*

                #(#regis_network_funcs)*
            }
        }

        use #name::*;

            #item
        }
    }
    .into()
}

fn get_nadi_functions<'a>(item: &'a ItemMod, funct: &'_ str) -> Vec<&'a Ident> {
    if let Some((_, cont)) = &item.content {
        cont.iter()
            .filter_map(|c| {
                if let syn::Item::Fn(f) = c {
                    Some(f)
                } else {
                    None
                }
            })
            .filter_map(|f| {
                if f.attrs.iter().any(|a| match &a.meta {
                    syn::Meta::Path(p) => p.is_ident(funct),
                    syn::Meta::List(l) => l.path.is_ident(funct),
                    _ => false,
                }) {
                    Some(&f.sig.ident)
                } else {
                    None
                }
            })
            .collect()
    } else {
        vec![]
    }
}

fn get_name_func(item: &ItemFn) -> proc_macro2::TokenStream {
    let func_name = item.sig.ident.to_string();
    quote! {
            fn name(&self) -> ::abi_stable::std_types::RString {
    #func_name .into()
            }}
}

fn get_code_func(item: &ItemFn) -> proc_macro2::TokenStream {
    let func_code = prettyplease::unparse(&syn::parse2(item.to_token_stream()).unwrap());
    quote! {
            fn code(&self) -> ::abi_stable::std_types::RString {
    #func_code .into()
            }}
}

fn get_call_func(
    item: &ItemFn,
    arg0: &FnArg,
    args: &[(&Ident, &Type, FuncArgType)],
    defaults: &HashMap<Ident, Expr>,
    func_struct_name: &Ident,
) -> proc_macro2::TokenStream {
    let extract_args: Vec<proc_macro2::TokenStream> = args
        .iter()
        .enumerate()
        .map(|(i, (arg, ty, at))| {
	    let arg_name = arg.to_string();
	    let ty_name = ty.to_token_stream().to_string();

            let def = if let Some(val) = defaults.get(arg) {
                quote! {
                #ty :: from ( #val )
                }
            } else {
                quote! {
		    return ::nadi_core::functions::FunctionRet::Error(format!("Argument {} ({} [{}]) is required", #i + 1, #arg_name, #ty_name).into());
                }
            };
	    // HACK again ignoring the path and assuming anything::Option is Option
	    let isopt = ty.to_token_stream().to_string().split('<').next().unwrap_or_default().split("::").last().unwrap_or_default().trim() == "Option";
	    let patterns = if isopt {quote!{
		Some(Ok(v)) => Some(v),
		Some(Err(e)) => return ::nadi_core::functions::FunctionRet::Error(e.into()),
		None => None,
	    }
	    } else { quote!{
		Some(Ok(v)) => v,
		Some(Err(e)) => return ::nadi_core::functions::FunctionRet::Error(e.into()),
		None => {#def},
	    }
	    };
	    match at {
		FuncArgType::Arg =>
		    quote!{
			let #arg : #ty = match ctx.arg_kwarg(#i, #arg_name) {
			    #patterns
			};
		    },
		FuncArgType::Relaxed =>
		    quote!{
			let #arg : #ty = match ctx.arg_kwarg_relaxed(#i, #arg_name) {
			    #patterns
			};
		    },
		FuncArgType::Args => quote! {
		    let #arg: #ty = ctx.args().into();
		},
		FuncArgType::KwArgs => quote! {
		    let #arg: #ty = ctx.kwargs().into();
		},
	    }
        })
        .collect();
    let args_n: Vec<proc_macro2::TokenStream> = args
        .iter()
        .map(|(arg, _, _)| arg.into_token_stream())
        .collect();
    let func_name = &item.sig.ident;
    let arg0_name = get_fn_arg(arg0).0;

    quote! {
    fn call(&self, #arg0, ctx: &::nadi_core::functions::FunctionCtx)
        -> ::nadi_core::functions::FunctionRet {
        #(#extract_args)*

        ::nadi_core::functions::FunctionRet::from(
            #func_struct_name :: #func_name(#arg0_name, #(#args_n),*)
        )
    }
    }
}

// get an expression that can generate function documentation
fn get_help_func(item: &ItemFn, default_args: &HashMap<Ident, Expr>) -> proc_macro2::TokenStream {
    let mut docs = get_doc(&item.attrs);

    let args: Vec<String> = item
        .sig
        .inputs
        .iter()
        .skip(1) // skip first argument, which is probably node or network
        .map(|a| match a {
            syn::FnArg::Typed(a) => {
                // args and kwargs function signature
                if a.attrs.iter().any(|at| at.path().is_ident("args")) {
                    "*args".into()
                } else if a.attrs.iter().any(|at| at.path().is_ident("kwargs")) {
                    "**kwargs".into()
                } else {
                    match a.pat.as_ref() {
                        syn::Pat::Ident(i) => {
                            if default_args.contains_key(&i.ident) {
                                format!(
                                    "{} [{}] = {{{}:?}}",
                                    i.ident,
                                    a.ty.as_ref().into_token_stream(),
                                    i.ident
                                )
                            } else {
                                format!("{} [{}]", i.ident, a.ty.as_ref().into_token_stream())
                            }
                        }
                        _ => panic!("Not supported"),
                    }
                }
            }
            _ => panic!("Not supported"),
        })
        .collect();

    // function signature showing the function name, arguments and
    // their default values
    docs.push("\n# Signature:".into());
    docs.push(format!("{}({})\n", &item.sig.ident, args.join(", ")));
    let docs = docs.join("\n");
    if default_args.is_empty() {
        quote! {
            fn help(&self) -> ::abi_stable::std_types::RString {
        #docs .into()
            }
        }
    } else {
        let values: Vec<proc_macro2::TokenStream> = default_args
            .iter()
            .map(|(k, v)| quote! { let #k = #v; })
            .collect();
        quote! {
            fn help(&self) -> ::abi_stable::std_types::RString {
        #(#values)*
        format!(#docs) .into()
            }
        }
    }
}

/// collect doc attributes
fn get_doc(attrs: &[Attribute]) -> Vec<String> {
    attrs
        .iter()
        .filter(|a| a.path().is_ident("doc"))
        .filter_map(|a| match &a.meta {
            syn::Meta::NameValue(val) => match &val.value {
                Expr::Lit(lit) => match &lit.lit {
                    Lit::Str(c) => {
                        let c = c.value();
                        Some(c.strip_prefix(' ').unwrap_or(c.as_str()).to_string())
                    }
                    _ => None,
                },
                _ => None,
            },
            _ => None,
        })
        .collect()
}