Skip to main content

wtx_macros/
lib.rs

1//! WTX - Macros
2
3#![expect(clippy::too_many_lines, reason = "Unimportant")]
4
5mod client_api_framework;
6mod db;
7mod error;
8mod executor;
9mod from_records;
10mod from_vars;
11mod http;
12mod misc;
13mod table;
14
15use error::Error;
16
17type Result<T> = core::result::Result<T, Error>;
18
19/// API
20///
21/// Creates types referring an API and its possible de-serializers/serializers or transport
22/// variants.
23#[proc_macro_attribute]
24pub fn api(
25  attrs: proc_macro::TokenStream,
26  item: proc_macro::TokenStream,
27) -> proc_macro::TokenStream {
28  match client_api_framework::api::api(attrs, item) {
29    Err(err) => syn::Error::from(err).to_compile_error().into(),
30    Ok(elem) => elem,
31  }
32}
33
34/// Connection Auxiliary
35#[proc_macro_derive(ConnAux)]
36pub fn conn_aux(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
37  match http::conn_aux(item) {
38    Err(err) => syn::Error::from(err).to_compile_error().into(),
39    Ok(elem) => elem,
40  }
41}
42
43/// Allows the execution of asynchronous database tests using the runtime provided by `WTX`.
44///
45/// This macro automatically creates an isolated environment with migrations and seeds
46#[proc_macro_attribute]
47pub fn db(
48  attrs: proc_macro::TokenStream,
49  item: proc_macro::TokenStream,
50) -> proc_macro::TokenStream {
51  match db::db(attrs, item) {
52    Err(err) => syn::Error::from(err).to_compile_error().into(),
53    Ok(elem) => elem,
54  }
55}
56
57/// From records
58#[proc_macro_derive(FromRecords, attributes(from_records))]
59pub fn from_records(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
60  match from_records::from_records(item) {
61    Err(err) => syn::Error::from(err).to_compile_error().into(),
62    Ok(elem) => elem,
63  }
64}
65
66/// Implements the `FromVars` trait.
67#[proc_macro_derive(FromVars, attributes(from_vars))]
68pub fn from_vars(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
69  match from_vars::from_vars(item) {
70    Err(err) => syn::Error::from(err).to_compile_error().into(),
71    Ok(elem) => elem,
72  }
73}
74
75/// Allows the execution of asynchronous programs using the runtime provided by `WTX`.
76#[proc_macro_attribute]
77pub fn main(_: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream {
78  executor::main(item)
79}
80
81/// Package
82///
83/// A framework-like attribute placed in inline modules that creates all the mandatory elements
84/// and optional elements related to `wtx::pkg::Package`.
85///
86/// ```rust
87/// struct SomeApi;
88///
89/// #[wtx_macros::pkg(data_format(json_rpc("SomeEndpoint")), id(SomeApiId))]
90/// mod pkg {
91///   #[pkg::req_data]
92///   pub struct SomeEndpointReq<'string> {
93///     ping: &'string str,
94///   }
95///
96///   #[pkg::res_data]
97///   pub struct SomeEndpointRes {
98///     pong: String,
99///   }
100/// }
101/// ```
102#[proc_macro_attribute]
103pub fn pkg(
104  attr: proc_macro::TokenStream,
105  item: proc_macro::TokenStream,
106) -> proc_macro::TokenStream {
107  match client_api_framework::pkg::pkg(attr, item) {
108    Err(err) => syn::Error::from(err).to_compile_error().into(),
109    Ok(elem) => elem,
110  }
111}
112
113/// Generates table fields separated by commas
114#[proc_macro_derive(Table)]
115pub fn table(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
116  match table::table(item) {
117    Err(err) => syn::Error::from(err).to_compile_error().into(),
118    Ok(elem) => elem,
119  }
120}
121
122/// Allows the execution of asynchronous tests using the runtime provided by `WTX`.
123#[proc_macro_attribute]
124pub fn test(_: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream {
125  executor::test(item)
126}