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
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use rustsynth::core::{CoreCreationFlags, CoreRef};
use syn::{self, Ident};

/// Derive macro generating an impl of the trait `OwnedMap`.
#[proc_macro_derive(OwnedMap)]
pub fn owned_map_derive(input: TokenStream) -> TokenStream {
    // Construct a representation of Rust code as a syntax tree
    // that we can manipulate
    let ast = syn::parse(input).unwrap();

    // Build the trait implementation
    impl_map_macro(&ast)
}

fn impl_map_macro(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let fields: Vec<Ident> = match &ast.data {
        syn::Data::Struct(ds) => match &ds.fields {
            syn::Fields::Named(named) => named
                .named
                .iter()
                .map(|x| x.ident.clone().unwrap())
                .collect(),
            _ => panic!("Must have named fields"),
        },
        _ => panic!("Must be a data struct"),
    };
    let gen = quote! {
        impl OwnedMap for #name {
            fn to_map<'elem>(self) -> rustsynth::map::OwnedMap<'elem> {
                let mut map = rustsynth::map::OwnedMap::new();
                #(
                    map.set(stringify!(#fields), &self.#fields).unwrap();
                )*
                map
            }
        }
    };
    gen.into()
}

/// Initilizes the autoloaded plugins
///
/// # Example
///
/// ```
/// init_plugins!();
///
/// let clip = Plugins::ffms2::Source(&mycore, "./demo.mp4".to_owned()).get_node("clip").unwrap();
/// ```
#[proc_macro]
pub fn init_plugins(_input: TokenStream) -> TokenStream {
    let core = CoreRef::new(CoreCreationFlags::NONE);
    let plugins = core.plugins();
    let token_vec: Vec<proc_macro2::TokenStream> = plugins
        .map(|x| {
            let namespace = Ident::new(x.namespace().unwrap(), Span::call_site());
            let func_vec: Vec<proc_macro2::TokenStream> = x
                .functions()
                .map(|y| {
                    let name = syn::parse_str::<Ident>(y.name.unwrap()).unwrap_or_else(|_| syn::parse_str::<Ident>(&(y.name.unwrap().to_owned() + "_")).expect("error"));

                    let args = y
                        .arguments
                        .unwrap();
                    let args_split: Vec<Vec<&str>>  = args
                        .split(";")
                        .map(|z| z.split(":").collect::<Vec<&str>>())
                        .collect();
                    let args_vec = parse_arguments(&args_split);
                    let arg_names: Vec<Ident> = args_split.iter().filter(|x| x.len() == 2).map(|x| {
                        syn::parse_str::<Ident>(x[0]).unwrap_or_else(|_| {
                            syn::parse_str::<Ident>(&(x[0].to_owned() + "_")).expect("error")
                        })
                    }).collect();
                    quote! {
                        pub fn #name<'core>(core: &'core rustsynth::core::CoreRef<'core>, #(#args_vec),*) -> rustsynth::map::OwnedMap<'core> {
                            let p = core.plugin_by_namespace(stringify!(#namespace)).unwrap();
                            let mut in_args = rustsynth::map::OwnedMap::new();
                            #(
                                in_args.set(stringify!(#arg_names), &#arg_names).expect(("Cannot set ".to_owned() + stringify!(#arg_names)).as_str());
                            )*
                            p.invoke(stringify!(#name), &in_args)
                        }
                    }
                })
                .collect();
            quote! {
                pub mod #namespace {
                    #(
                        #func_vec
                    )*
                }
            }
        })
        .collect();
    let gen = quote! {
        #[allow(non_snake_case)]
        pub mod Plugins {
            #(
                #token_vec
            )*
        }
    };
    unsafe { core.free_core() };
    gen.into()
}

fn parse_arguments(input: &Vec<Vec<&str>>) -> Vec<proc_macro2::TokenStream> {
    input
        .iter()
        .filter(|x| x.len() == 2)
        .map(|x| {
            let x0 = syn::parse_str::<Ident>(x[0]).unwrap_or_else(|_| {
                syn::parse_str::<Ident>(&(x[0].to_owned() + "_")).expect("error")
            });
            match x[1] {
                "vnode" => {
                    quote! {
                        #x0: rustsynth::node::Node<'core>
                    }
                }
                "int" => {
                    quote! {
                        #x0: i64
                    }
                }
                "data" => {
                    quote! {
                        #x0: String
                    }
                }
                //y => {
                //    quote! {
                //        #x0: #y
                //    }
                //}
                _ => {
                    quote! {
                        #x0: i64
                    }
                }
            }
        })
        .collect()
}