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};
5
6/// This attribute macro should be used when you create an entrypoint of a Rocal application.
7///
8/// ```rust
9/// use rocal::config;
10///
11/// #[rocal::main]
12/// fn app() {}
13/// ```
14///
15#[proc_macro_attribute]
16pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
17    start_app(item.into()).into()
18}
19
20/// This attribute macro should be used when you create an action of a controller.
21///
22/// ```rust
23/// use crate::views::root_view::RootView;
24/// use rocal::rocal_core::traits::{Controller, SharedRouter};
25///
26/// pub struct RootController {
27///     router: SharedRouter,
28///     view: RootView,
29/// }
30///
31/// impl Controller for RootController {
32///     type View = RootView;
33///     fn new(router: SharedRouter, view: Self::View) -> Self {
34///         RootController { router, view }
35///     }
36/// }
37///
38/// impl RootController {
39///     #[rocal::action]
40///     pub fn index(&self) {
41///         self.view.index();
42///     }
43/// }
44/// ```
45///
46#[proc_macro_attribute]
47pub fn action(_: TokenStream, item: TokenStream) -> TokenStream {
48    build_action(item.into()).into()
49}
50
51/// This function-like macro sets up application routing.
52///
53/// ```rust
54/// route! {
55///     get "/" => { controller: RootController , action: index , view: RootView },
56///     post "/users" => { controller: UsersController, action: create, view: UserView}
57/// }
58///
59/// ```
60#[proc_macro]
61pub fn route(item: TokenStream) -> TokenStream {
62    build_route(item.into()).into()
63}
64
65/// This function-like macro makes `static CONFIG` which contains app_id, a connection of an embedded database, and sync server endpoint URL.
66///
67/// ```rust
68/// config! {
69///     app_id: "a917e367-3484-424d-9302-f09bdaf647ae" ,
70///     sync_server_endpoint: "http://127.0.0.1:3000/presigned-url" ,
71///     database_directory_name: "local" ,
72///     database_file_name: "local.sqlite3"
73/// }
74///
75#[proc_macro]
76pub fn config(item: TokenStream) -> TokenStream {
77    build_config(item.into()).into()
78}
79
80/// This function-like macro allows users to set a path where migration files are supposed to be.
81///
82/// ```rust
83/// migrate!("db/migrations");
84/// ```
85#[proc_macro]
86pub fn migrate(item: TokenStream) -> TokenStream {
87    run_migration(item.into()).into()
88}