rocal_macro/lib.rs
1#![doc = include_str!("../README.md")]
2
3use proc_macro::TokenStream;
4use rocal_core::{build_action, build_config, build_route, run_migration, start_app};
5use rocal_ui::build_ui;
6
7/// This attribute macro should be used when you create an entrypoint of a Rocal application.
8///
9/// ```rust
10/// use rocal::config;
11///
12/// #[rocal::main]
13/// fn app() {}
14/// ```
15///
16#[proc_macro_attribute]
17pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
18 start_app(item.into()).into()
19}
20
21/// This attribute macro should be used when you create an action of a controller.
22///
23/// ```rust
24/// use crate::views::root_view::RootView;
25/// use rocal::rocal_core::traits::{Controller, SharedRouter};
26///
27/// pub struct RootController {
28/// router: SharedRouter,
29/// view: RootView,
30/// }
31///
32/// impl Controller for RootController {
33/// type View = RootView;
34/// fn new(router: SharedRouter, view: Self::View) -> Self {
35/// RootController { router, view }
36/// }
37/// }
38///
39/// impl RootController {
40/// #[rocal::action]
41/// pub fn index(&self) {
42/// self.view.index();
43/// }
44/// }
45/// ```
46///
47#[proc_macro_attribute]
48pub fn action(_: TokenStream, item: TokenStream) -> TokenStream {
49 build_action(item.into()).into()
50}
51
52/// This function-like macro sets up application routing.
53///
54/// ```rust
55/// route! {
56/// get "/" => { controller: RootController , action: index , view: RootView },
57/// post "/users" => { controller: UsersController, action: create, view: UserView}
58/// }
59///
60/// ```
61#[proc_macro]
62pub fn route(item: TokenStream) -> TokenStream {
63 build_route(item.into()).into()
64}
65
66/// This function-like macro makes `static CONFIG` which contains app_id, a connection of an embedded database, and sync server endpoint URL.
67///
68/// ```rust
69/// config! {
70/// app_id: "a917e367-3484-424d-9302-f09bdaf647ae" ,
71/// sync_server_endpoint: "http://127.0.0.1:3000/presigned-url" ,
72/// database_directory_name: "local" ,
73/// database_file_name: "local.sqlite3"
74/// }
75/// ```
76#[proc_macro]
77pub fn config(item: TokenStream) -> TokenStream {
78 build_config(item.into()).into()
79}
80
81/// This function-like macro allows users to set a path where migration files are supposed to be.
82///
83/// ```rust
84/// migrate!("db/migrations");
85/// ```
86#[proc_macro]
87pub fn migrate(item: TokenStream) -> TokenStream {
88 run_migration(item.into()).into()
89}
90
91/// This function-like macro generates code to produce HTML string.
92///
93/// ```rust
94/// view! {
95/// <div class="container">
96/// <h1 class="title">{"Hello, World!"}</h1>
97/// if true {
98/// <p>{"This is how you can use this macro"}</p>
99/// } else {
100/// <p>{"Even you can use if-else condition control"}</p>
101/// }
102/// for item in items {
103/// <p>{{ item.id }}{"Maybe, you also want to use for-loop."}</p>
104/// }
105/// </div>
106/// }
107/// ```
108#[proc_macro]
109pub fn view(item: TokenStream) -> TokenStream {
110 build_ui(item.into()).into()
111}