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
use crate::internal::{
    get_eosio_meta_items, Attr, BoolAttr, CRATE_PATH, PRIMARY_KEY,
    SECONDARY_KEY, SINGLETON, TABLE_NAME,
};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{
    parse::{Error as ParseError, Parse, ParseStream, Result as ParseResult},
    Data, DeriveInput, Fields, Generics, Ident, Lit, LitStr, Meta, NestedMeta,
    Path, Type,
};

pub enum DeriveTable {
    Table(Table),
    Singleton(Singleton),
}

pub struct Table {
    name: LitStr,
    ident: Ident,
    generics: Generics,
    primary_key: PrimaryKey,
    secondary_keys: Vec<SecondaryKey>,
    crate_path: Option<Path>,
}

pub struct Singleton {
    name: LitStr,
    ident: Ident,
    generics: Generics,
    crate_path: Option<Path>,
}

pub struct PrimaryKey {
    ident: Ident,
}

pub struct SecondaryKey {
    ident: Ident,
    ty: Type,
    // index: Option<usize>
}

impl SecondaryKey {
    fn by_ident(&self) -> Ident {
        Ident::new(format!("by_{}", self.ident).as_str(), self.ident.span())
    }
}

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

        // Container attributes
        let mut table_name: Attr<LitStr> = Attr::none(TABLE_NAME);
        let mut singleton: BoolAttr = BoolAttr::none(SINGLETON);
        let mut crate_path: Attr<Path> = Attr::none(CRATE_PATH);

        // Get container attributes
        for meta_item in
            input.attrs.iter().flat_map(get_eosio_meta_items).flatten()
        {
            match meta_item {
                // Parse `#[eosio(table_name = "test")]`
                NestedMeta::Meta(Meta::NameValue(m))
                    if m.path == TABLE_NAME =>
                {
                    if let Lit::Str(lit) = m.lit {
                        table_name.set(lit.clone(), lit)?;
                    } else {
                        return Err(ParseError::new_spanned(
                            m,
                            "`#[eosio(table_name = \"...\")]` must use a \
                             string literal",
                        ));
                    }
                }
                // Parse `#[eosio(singleton)]
                NestedMeta::Meta(Meta::Path(word)) if word == SINGLETON => {
                    singleton.set_true(word)?;
                }
                // Parse `#[eosio(crate_path = "crate")]`
                NestedMeta::Meta(Meta::NameValue(m))
                    if m.path == CRATE_PATH =>
                {
                    match m.lit {
                        Lit::Str(string) => {
                            match string.parse_with(Path::parse_mod_style) {
                                Ok(path) => {
                                    crate_path.set(path.clone(), path)?;
                                }
                                Err(_) => {
                                    return Err(ParseError::new_spanned(
                                        m.path,
                                        "`#[eosio(crate_path = \"...\")]` \
                                         received an invalid path",
                                    ))
                                }
                            }
                        }
                        _ => {
                            return Err(ParseError::new_spanned(
                                m.path,
                                "`#[eosio(crate_path = \"...\")]` expected a \
                                 string literal",
                            ))
                        }
                    }
                }
                // Error
                NestedMeta::Meta(meta_item) => {
                    let path = meta_item
                        .path()
                        .into_token_stream()
                        .to_string()
                        .replace(' ', "");
                    return Err(ParseError::new_spanned(
                        meta_item,
                        format!("unknown eosio container attribute `{}`", path),
                    ));
                }
                // Error
                NestedMeta::Lit(lit) => {
                    return Err(ParseError::new_spanned(
                        lit,
                        "unexpected literal in eosio container attribute",
                    ));
                }
            }
        }

        let table_name = match table_name.get() {
            Some(t) => t,
            None => {
                return Err(ParseError::new(
                    input.ident.span(),
                    "`#[eosio(table_name = \"...\")]` must be set when \
                     deriving from `eosio::Table`",
                ))
            }
        };

        // Field attributes
        let mut primary_key: Attr<PrimaryKey> = Attr::none(PRIMARY_KEY);
        let mut secondary_keys: Vec<SecondaryKey> = Vec::new();
        match input.data {
            Data::Struct(data) => match data.fields {
                // Structs with named fields
                Fields::Named(fields) => {
                    for field in fields.named.into_iter() {
                        let mut is_primary = false;
                        let mut is_secondary = false;
                        for field_attr in field
                            .attrs
                            .iter()
                            .flat_map(get_eosio_meta_items)
                            .flatten()
                        {
                            match field_attr {
                                // Parse `#[eosio(primary_key)]`
                                NestedMeta::Meta(Meta::Path(word))
                                    if word == PRIMARY_KEY =>
                                {
                                    is_primary = true;
                                }
                                // Parse `#[eosio(secondary_key)]`
                                NestedMeta::Meta(Meta::Path(word))
                                    if word == SECONDARY_KEY =>
                                {
                                    is_secondary = true;
                                }
                                // Error
                                NestedMeta::Meta(meta_item) => {
                                    let path = meta_item
                                        .path()
                                        .into_token_stream()
                                        .to_string()
                                        .replace(' ', "");
                                    return Err(ParseError::new_spanned(
                                        meta_item,
                                        format!(
                                            "unknown eosio field attribute \
                                             `{}`",
                                            path
                                        ),
                                    ));
                                }
                                // Error
                                NestedMeta::Lit(lit) => {
                                    return Err(ParseError::new_spanned(
                                        lit,
                                        "unexpected literal in eosio field \
                                         attribute",
                                    ));
                                }
                            }
                        }
                        let ident = field.ident.clone().unwrap();
                        match (is_primary, is_secondary) {
                            (true, false) => {
                                primary_key.set(field, PrimaryKey { ident })?
                            }
                            (false, true) => {
                                secondary_keys.push(SecondaryKey {
                                    ident,
                                    ty: field.ty,
                                })
                            }
                            (true, true) => {
                                return Err(ParseError::new_spanned(
                                    field,
                                    "cannot use both `#[eosio(primary_key)]` \
                                     and `#[eosio(secondary_key)]` on the \
                                     same field",
                                ));
                            }
                            (false, false) => (),
                        }
                    }
                }
                // Error
                Fields::Unnamed(fields) => {
                    return Err(ParseError::new_spanned(
                        fields,
                        "deriving `eosio::Table` from structs with unnamed \
                         fields is not currently supported",
                    ));
                }
                // Error
                Fields::Unit => {
                    return Err(ParseError::new(
                        input.ident.span(),
                        "deriving `eosio::Table` from unit structs is not \
                         supported",
                    ));
                }
            },
            // Error
            Data::Enum(_data) => {
                return Err(ParseError::new(
                    input.ident.span(),
                    "deriving `eosio::Table` with enums is not currently \
                     supported",
                ));
            }
            // Error
            Data::Union(_data) => {
                return Err(ParseError::new(
                    input.ident.span(),
                    "deriving `eosio::Table` with unions is not supported",
                ));
            }
        }

        let primary_key = primary_key.get_with_tokens();

        if singleton.get() {
            if let Some((ts, _)) = &primary_key {
                return Err(ParseError::new_spanned(
                    ts,
                    "`#[eosio(primary_key)]` cannot be used with \
                     `#[eosio(singleton)]`",
                ));
            }

            return Ok(DeriveTable::Singleton(Singleton {
                ident: input.ident,
                generics: input.generics,
                name: table_name,
                crate_path: None,
            }));
        }

        let primary_key = match primary_key {
            Some((_, pk)) => pk,
            None => {
                return Err(ParseError::new(
                    input.ident.span(),
                    "`#[eosio(primary_key)]` must be set for a field when \
                     deriving from `eosio::Table`",
                ))
            }
        };

        Ok(DeriveTable::Table(Table {
            name: table_name,
            ident: input.ident,
            generics: input.generics,
            primary_key,
            secondary_keys,
            crate_path: None,
        }))
    }
}

impl ToTokens for Table {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let (impl_generics, ty_generics, where_clause) =
            &self.generics.split_for_impl();

        let mut secondary_keys_expanded = quote!();
        let mut secondary_keys_constructors = quote!();
        let default_path = LitStr::new("::eosio", Span::call_site())
            .parse_with(Path::parse_mod_style)
            .unwrap();
        let eosio = &self.crate_path.as_ref().unwrap_or(&default_path);

        for i in 0..16 {
            match self.secondary_keys.get(i) {
                Some(sk) => {
                    let ident = &sk.ident;
                    secondary_keys_expanded = quote! {
                        #secondary_keys_expanded
                        Some(#eosio::SecondaryKey::from(row.#ident)),
                    };
                    let ty = &sk.ty;
                    let by_ident = sk.by_ident();
                    secondary_keys_constructors = quote! {
                        #secondary_keys_constructors

                        #[inline]
                        pub fn #by_ident<C, S>(code: C, scope: S) -> #eosio::SecondaryTableIndex<#ty, Self>
                        where
                            C: Into<#eosio::AccountName>,
                            S: Into<#eosio::ScopeName>,
                        {
                            #eosio::SecondaryTableIndex::new(code, scope, Self::NAME, #i)
                        }
                    };
                }
                None => {
                    secondary_keys_expanded = quote! {
                        #secondary_keys_expanded
                        None,
                    };
                }
            };
        }

        let table_name = &self.name;
        let name = &self.ident;
        let primary_key = &self.primary_key.ident;

        let expanded = quote! {
            #[automatically_derived]
            impl #impl_generics #eosio::Table for #name #ty_generics #where_clause {
                const NAME: #eosio::TableName = #eosio::TableName::new(#eosio::n!(#table_name));

                type Row = Self;

                #[inline]
                fn primary_key(row: &Self::Row) -> u64 {
                    row.#primary_key.into()
                }

                #[inline]
                fn secondary_keys(row: &Self::Row) -> #eosio::SecondaryKeys {
                    SecondaryKeys::from([
                        #secondary_keys_expanded
                    ])
                }
            }

            #[automatically_derived]
            impl #impl_generics #name #ty_generics #where_clause {
                #secondary_keys_constructors
            }
        };
        expanded.to_tokens(tokens);
    }
}

impl ToTokens for Singleton {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let table_name = &self.name;
        let name = &self.ident;
        // let eosio = &self.crate_path;

        let default_path = LitStr::new("::eosio", Span::call_site())
            .parse_with(Path::parse_mod_style)
            .unwrap();
        let eosio = &self.crate_path.as_ref().unwrap_or(&default_path);
        let (impl_generics, ty_generics, where_clause) =
            &self.generics.split_for_impl();
        let expanded = quote! {
            #[automatically_derived]
            impl #impl_generics #eosio::Table for #name #ty_generics #where_clause {
                const NAME: #eosio::TableName = #eosio::TableName::new(#eosio::n!(#table_name));

                type Row = Self;

                #[inline]
                fn primary_key(_row: &Self::Row) -> u64 {
                    Self::NAME.as_u64()
                }
            }

            #[automatically_derived]
            impl #impl_generics #name #ty_generics #where_clause {
                #[inline]
                pub fn singleton<C, S>(code: C, scope: S) -> ::eosio_cdt::SingletonIndex<Self>
                where
                    C: Into<#eosio::AccountName>,
                    S: Into<#eosio::ScopeName>,
                {
                    ::eosio_cdt::SingletonIndex::new(code, scope)
                }
            }
        };
        expanded.to_tokens(tokens);
    }
}

impl ToTokens for DeriveTable {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::Table(table) => table.to_tokens(tokens),
            Self::Singleton(singleton) => singleton.to_tokens(tokens),
        }
    }
}