scrypto_derive/
lib.rs

1mod ast;
2mod blueprint;
3mod non_fungible_data;
4mod utils;
5
6use proc_macro::TokenStream;
7
8/// Declares a blueprint.
9///
10/// The `blueprint` macro is a convenient way to define a new blueprint. It takes
11/// two arguments:
12/// - A `struct` which defines the structure
13/// - A `impl` which defines the implementation.
14///
15/// This macro will derive the dispatcher method responsible for handling invocation
16/// according to Scrypto ABI.
17///
18/// # Example
19/// ```ignore
20/// use scrypto::prelude::*;
21///
22/// #[blueprint]
23/// mod counter {
24///     struct Counter {
25///         count: u32
26///     }
27///
28///     impl Counter {
29///         pub fn new() -> Component {
30///             Self {
31///                 count: 0
32///             }.instantiate()
33///         }
34///
35///         pub fn get_and_incr(&mut self) -> u32 {
36///             let n = self.count;
37///             self.count += 1;
38///             n
39///         }
40///     }
41/// }
42/// ```
43#[proc_macro_attribute]
44pub fn blueprint(_: TokenStream, input: TokenStream) -> TokenStream {
45    blueprint::handle_blueprint(proc_macro2::TokenStream::from(input))
46        .unwrap_or_else(|err| err.to_compile_error())
47        .into()
48}
49
50/// Derive code that describe a non-fungible data structure.
51///
52/// # Example
53///
54/// ```ignore
55/// use scrypto::prelude::*;
56///
57/// #[derive(NonFungibleData)]
58/// pub struct MyStruct {
59///     pub field_1: u32,
60///     #[mutable]
61///     pub field_2: String,
62/// }
63/// ```
64#[proc_macro_derive(NonFungibleData, attributes(mutable))]
65pub fn non_fungible_data(input: TokenStream) -> TokenStream {
66    non_fungible_data::handle_non_fungible_data(proc_macro2::TokenStream::from(input))
67        .unwrap_or_else(|err| err.to_compile_error())
68        .into()
69}