Skip to main content

laravel_rust_macros/
lib.rs

1//! Laravel for Rust's procedural macros for the container, configuration, the console, and tests.
2//!
3//! Use these macros through the `laravel` crate, which re-exports and documents them:
4//!
5//! - `laravel::embed!`, `#[laravel::once]` and `#[laravel::test]`
6//! - `laravel::config::Section` and `#[laravel::config::section]`
7//! - `#[laravel::console::command]`
8//! - `#[laravel::container::singleton]` and `#[laravel::container::bind]`
9//! - `laravel::errors::Status`
10//! - `#[laravel::feature::flag]`
11//! - `#[laravel::schedule::tasks]`
12
13mod command;
14mod embed;
15mod feature;
16mod once;
17mod schedule;
18mod section;
19mod service;
20mod status;
21mod testing;
22
23use laravel_rust_macro_support::{self as syntax, NoArguments, attribute, expand};
24use proc_macro::TokenStream;
25
26#[proc_macro_attribute]
27pub fn command(args: TokenStream, input: TokenStream) -> TokenStream {
28    attribute(
29        args,
30        input,
31        "`#[command]` goes on an async function, or on the impl of a command struct",
32        command::expand,
33    )
34}
35
36#[proc_macro_attribute]
37pub fn flag(args: TokenStream, input: TokenStream) -> TokenStream {
38    attribute(
39        args,
40        input,
41        "`#[flag]` goes on the function that resolves a feature",
42        feature::expand,
43    )
44}
45
46#[proc_macro_derive(Status, attributes(status))]
47pub fn status(input: TokenStream) -> TokenStream {
48    expand(input, status::expand)
49}
50
51#[proc_macro_derive(Section, attributes(env, section))]
52pub fn section(input: TokenStream) -> TokenStream {
53    expand(input, section::expand)
54}
55
56#[proc_macro_attribute]
57pub fn config_section(args: TokenStream, input: TokenStream) -> TokenStream {
58    attribute(
59        args,
60        input,
61        "`#[config::section]` goes on a function, such as `fn cache(env: &Env) -> Cache`",
62        |NoArguments, function| section::expand_function(function),
63    )
64}
65
66#[proc_macro_attribute]
67pub fn schedule_tasks(args: TokenStream, input: TokenStream) -> TokenStream {
68    attribute(
69        args,
70        input,
71        "`#[schedule::tasks]` goes on a function, such as `fn schedule(schedule: &mut Schedule)`",
72        |NoArguments, function| schedule::expand(function),
73    )
74}
75
76#[proc_macro_attribute]
77pub fn singleton(args: TokenStream, input: TokenStream) -> TokenStream {
78    attribute(
79        args,
80        input,
81        "`#[singleton]` goes on an async function that returns the service, or on a struct",
82        |args, item| service::expand(args, item, service::Lifetime::Singleton),
83    )
84}
85
86#[proc_macro_attribute]
87pub fn bind(args: TokenStream, input: TokenStream) -> TokenStream {
88    attribute(
89        args,
90        input,
91        "`#[bind]` goes on an async function that returns the service, or on a struct",
92        |args, item| service::expand(args, item, service::Lifetime::Transient),
93    )
94}
95
96#[proc_macro_attribute]
97pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {
98    attribute(
99        args,
100        input,
101        "`#[laravel::once]` goes on a function",
102        |NoArguments, function| once::expand(function),
103    )
104}
105
106#[proc_macro_attribute]
107pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
108    attribute(
109        args,
110        input,
111        "`#[laravel::test]` goes on a test function",
112        testing::expand,
113    )
114}
115
116#[proc_macro]
117pub fn embed(input: TokenStream) -> TokenStream {
118    expand(input, embed::expand)
119}