catalyzer_macros/lib.rs
1//! Macros for the Catalyzer framework.
2
3#[allow(unused_extern_crates)]
4extern crate proc_macro;
5
6use proc_macro2::TokenStream as T;
7use proc_macro::TokenStream;
8
9mod main_func;
10mod routes;
11mod app;
12
13/// Marks a function as the main entry point for the application.
14///
15/// # Example
16///
17/// ```rust
18/// # use catalyzer::*;
19/// #[main]
20/// fn main() {
21/// // Your code here (can be both sync and async)
22/// }
23/// ```
24#[proc_macro_attribute]
25pub fn main(cfg: TokenStream, input: TokenStream) -> TokenStream {
26 main_func::main(cfg.into(), input.into()).into()
27}
28
29/// A shortcut for creating an [`App`] instance.
30///
31/// [`App`]: struct.App.html
32///
33/// # Example
34///
35/// ```rust
36/// # use catalyzer::*;
37/// #[main]
38/// fn main() {
39/// App![index]
40/// .bind("0.0.0.0:3000")?
41/// .launch()
42/// }
43///
44/// #[get("/")]
45/// fn index() {
46/// "Hello, world!"
47/// }
48/// ```
49#[proc_macro]
50#[allow(non_snake_case)]
51pub fn App(input: TokenStream) -> TokenStream {
52 app::app(input.into()).into()
53}
54
55macro_rules! routes {
56 ($(
57 $(#[$attr:meta])*
58 @$method:ident
59 )+) => ($(
60 $(#[$attr])*
61 #[proc_macro_attribute]
62 pub fn $method(cfg: TokenStream, input: TokenStream) -> TokenStream {
63 routes::$method(cfg.into(), input.into()).into()
64 }
65 )+)
66}
67
68routes!(
69 /// A route handler for the `GET` method.
70 @get
71 /// A route handler for the `POST` method.
72 @post
73 /// A route handler for the `PUT` method.
74 @put
75 /// A route handler for the `DELETE` method.
76 @delete
77 /// A route handler for the `PATCH` method.
78 @patch
79 /// A route handler for the `HEAD` method.
80 @head
81 /// A route handler for the `OPTIONS` method.
82 @options
83 /// A route handler for the `TRACE` method.
84 @trace
85);