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