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
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::{
    parse::{Parse, ParseStream, Result as ParseResult},
    token::Comma,
    DeriveInput, Error, Ident, LitStr,
};

pub struct Table {
    input: DeriveInput,
    args: TableArgs,
}

impl Table {
    pub const fn new(args: TableArgs, input: DeriveInput) -> Self {
        Self { args, input }
    }
}

pub struct TableArgs {
    name: LitStr,
    is_singleton: bool,
}

impl Parse for TableArgs {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        let name = input.parse::<LitStr>()?;

        if input.parse::<Comma>().is_ok() {
            let ident = input.parse::<Ident>()?;
            if ident == "singleton" {
                Ok(Self {
                    name,
                    is_singleton: true,
                })
            } else {
                Err(Error::new(ident.span(), "expected `singleton`"))
            }
        } else {
            Ok(Self {
                name,
                is_singleton: false,
            })
        }
    }
}

impl ToTokens for Table {
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        let input = &self.input;
        let name = &self.args.name;
        let expanded = quote! {
            #[derive(
                Clone,
                Debug,
                eosio::NumBytes,
                eosio::Read,
                eosio::Write,
                eosio::Table,
                PartialEq,
                PartialOrd
            )]
        };
        let expanded = if self.args.is_singleton {
            quote! {
                #expanded
                #[eosio(table_name = #name, singleton)]
            }
        } else {
            quote! {
                #expanded
                #[eosio(table_name = #name)]
            }
        };
        let expanded = quote! {
            #expanded
            #input
        };
        expanded.to_tokens(tokens);
    }
}