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
use quote::{quote, ToTokens};

use crate::attrs::partition_attributes;

/// Odra module struct.
///
/// Wraps up [syn::ItemStruct].
pub struct ModuleStruct {
    is_instantiable: bool,
    item: syn::ItemStruct
}

impl From<syn::ItemStruct> for ModuleStruct {
    fn from(item: syn::ItemStruct) -> Self {
        let (_, other_attrs) = partition_attributes(item.attrs).unwrap();
        Self {
            is_instantiable: true,
            item: syn::ItemStruct {
                attrs: other_attrs,
                ..item
            }
        }
    }
}

impl ToTokens for ModuleStruct {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let item_struct = &self.item;
        let span = item_struct.ident.span();
        let instance = match &self.is_instantiable {
            true => quote::quote_spanned!(span => #[derive(odra::Instance)]),
            false => quote!()
        };
        tokens.extend(quote! {
            #instance
            #item_struct
        });
    }
}