Skip to main content

runar_lang_macros/
lib.rs

1//! Proc-macro crate for Rúnar smart contract attributes.
2//!
3//! - `#[runar::contract]` — strips `#[readonly]` field annotations (since Rust
4//!   doesn't allow attribute macros on fields) and passes the struct through.
5//! - `#[runar::methods(Name)]` — identity macro for impl blocks.
6//! - `#[public]` — identity macro marking a spending entry point.
7
8use proc_macro::TokenStream;
9
10/// Marks a struct as a Rúnar smart contract.
11///
12/// Strips `#[readonly]` annotations from fields so the struct compiles.
13/// The Rúnar compiler parses these annotations with its own parser.
14#[proc_macro_attribute]
15pub fn contract(_attr: TokenStream, item: TokenStream) -> TokenStream {
16    // Strip #[readonly] from the token stream since Rust doesn't allow
17    // proc_macro_attribute on struct fields.
18    let src = item.to_string();
19    let cleaned = src.replace("#[readonly]", "").replace("# [readonly]", "");
20    cleaned.parse().expect("failed to parse struct after stripping #[readonly]")
21}
22
23/// Marks a struct as a stateful Rúnar smart contract.
24#[proc_macro_attribute]
25pub fn stateful_contract(_attr: TokenStream, item: TokenStream) -> TokenStream {
26    contract(TokenStream::new(), item)
27}
28
29/// Marks an impl block as containing Rúnar contract methods.
30#[proc_macro_attribute]
31pub fn methods(_attr: TokenStream, item: TokenStream) -> TokenStream {
32    item
33}
34
35/// Marks a method as a public spending entry point.
36#[proc_macro_attribute]
37pub fn public(_attr: TokenStream, item: TokenStream) -> TokenStream {
38    item
39}