wtx_macros/
lib.rs

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