clientix_codegen/lib.rs
1mod client;
2mod method;
3mod utils;
4mod attributes;
5mod derive;
6
7use proc_macro::TokenStream;
8use quote::quote;
9use syn::{parse_macro_input, DeriveInput, ItemStruct};
10use clientix_core::prelude::reqwest::Method;
11use crate::client::parse_client;
12use crate::method::parse_header;
13use crate::method::parse_method;
14use crate::derive::request_args::RequestArgsCompiler;
15
16/**
17A procedural macro for building an HTTP client. It includes the following attributes:
18- url - the base part of the client’s URL, e.g. http://localhost:8080
19- path - an additional part of the URL path that precedes method paths
20- async - if true, the client is asynchronous; otherwise, it is blocking
21*/
22#[proc_macro_attribute]
23pub fn clientix(attrs: TokenStream, item: TokenStream) -> TokenStream {
24 // TODO: научить clientix работать со структурными объектам, а не только с trait
25 parse_client(item, attrs)
26}
27
28/**
29A procedural macro for building an HTTP GET method of trait. It includes the following attributes:
30- path - a part of the URL path (String)
31- consumes - content type for request, support: application/json, application/xml, application/x-www-form-urlencoded (String)
32- produces - accept type for response, support: application/json, application/xml, application/x-www-form-urlencoded (String)
33
34GET method supports argument macros:
35- #[segment] - maps method arguments to path segments and header placeholders (simple types, String)
36- #[query] - maps method arguments to query parameters (simple types, String)
37- #[header] - maps method arguments to request headers (simple types, String)
38- #[body] - maps method arguments to request body (object implemented #[data_transfer])
39
40*/
41#[proc_macro_attribute]
42pub fn get(attrs: TokenStream, item: TokenStream) -> TokenStream {
43 // TODO: научить get работать с независимыми функциями и со структурными методами (блок impl)
44 parse_method(Method::GET, item, attrs)
45}
46
47/**
48A procedural macro for building an HTTP POST method of trait. It includes the following attributes:
49- path - a part of the URL path (String)
50- consumes - content type for request, support: application/json, application/xml, application/x-www-form-urlencoded (String)
51- produces - accept type for response, support: application/json, application/xml, application/x-www-form-urlencoded (String)
52
53POST method supports argument macros:
54- #[segment] - maps method arguments to path segments and header placeholders (simple types, String)
55- #[query] - maps method arguments to query parameters (simple types, String)
56- #[header] - maps method arguments to request headers (simple types, String)
57- #[body] - maps method arguments to request body (object implemented #[data_transfer])
58
59RequestBody must implement the #[data_transfer] macro.
60*/
61#[proc_macro_attribute]
62pub fn post(attrs: TokenStream, item: TokenStream) -> TokenStream {
63 // TODO: научить post работать с независимыми функциями и со структурными методами (блок impl)
64 parse_method(Method::POST, item, attrs)
65}
66
67/**
68A procedural macro for building an HTTP PUT method of trait. It includes the following attributes:
69- path - a part of the URL path (String)
70- consumes - content type for request, support: application/json, application/xml, application/x-www-form-urlencoded (String)
71- produces - accept type for response, support: application/json, application/xml, application/x-www-form-urlencoded (String)
72
73PUT method supports argument macros:
74- #[segment] - maps method arguments to path segments and header placeholders (simple types, String)
75- #[query] - maps method arguments to query parameters (simple types, String)
76- #[header] - maps method arguments to request headers (simple types, String)
77- #[body] - maps method arguments to request body (object implemented #[data_transfer])
78
79RequestBody must implement the #[data_transfer] macro.
80*/
81#[proc_macro_attribute]
82pub fn put(attrs: TokenStream, item: TokenStream) -> TokenStream {
83 // TODO: научить put работать с независимыми функциями и со структурными методами (блок impl)
84 parse_method(Method::PUT, item, attrs)
85}
86
87/**
88A procedural macro for building an HTTP DELETE method of trait. It includes the following attributes:
89- path - a part of the URL path (String)
90- consumes - content type for request, support: application/json, application/xml, application/x-www-form-urlencoded (String)
91- produces - accept type for response, support: application/json, application/xml, application/x-www-form-urlencoded (String)
92
93DELETE method supports argument macros:
94- #[segment] - maps method arguments to path segments and header placeholders (simple types, String)
95- #[query] - maps method arguments to query parameters (simple types, String)
96- #[header] - maps method arguments to request headers (simple types, String)
97- #[body] - maps method arguments to request body (object implemented #[data_transfer])
98
99*/
100#[proc_macro_attribute]
101pub fn delete(attrs: TokenStream, item: TokenStream) -> TokenStream {
102 // TODO: научить delete работать с независимыми функциями и со структурными методами (блок impl)
103 parse_method(Method::DELETE, item, attrs)
104}
105
106/**
107A procedural macro for building an HTTP HEAD method of trait. It includes the following attributes:
108- path - a part of the URL path (String)
109- consumes - content type for request, support: application/json, application/xml, application/x-www-form-urlencoded (String)
110- produces - accept type for response, support: application/json, application/xml, application/x-www-form-urlencoded (String)
111
112HEAD method supports argument macros:
113- #[segment] - maps method arguments to path segments and header placeholders (simple types, String)
114- #[query] - maps method arguments to query parameters (simple types, String)
115- #[header] - maps method arguments to request headers (simple types, String)
116- #[body] - maps method arguments to request body (object implemented #[data_transfer])
117
118*/
119#[proc_macro_attribute]
120pub fn head(attrs: TokenStream, item: TokenStream) -> TokenStream {
121 // TODO: научить head работать с независимыми функциями и со структурными методами (блок impl)
122 parse_method(Method::HEAD, item, attrs)
123}
124
125/**
126A procedural macro for building an HTTP PATCH method of trait. It includes the following attributes:
127- path - a part of the URL path (String)
128- consumes - content type for request, support: application/json, application/xml, application/x-www-form-urlencoded (String)
129- produces - accept type for response, support: application/json, application/xml, application/x-www-form-urlencoded (String)
130
131PATCH method supports argument macros:
132- #[segment] - maps method arguments to path segments and header placeholders (simple types, String)
133- #[query] - maps method arguments to query parameters (simple types, String)
134- #[header] - maps method arguments to request headers (simple types, String)
135- #[body] - maps method arguments to request body (object implemented #[data_transfer])
136
137*/
138#[proc_macro_attribute]
139pub fn patch(attrs: TokenStream, item: TokenStream) -> TokenStream {
140 // TODO: научить patch работать с независимыми функциями и со структурными методами (блок impl)
141 parse_method(Method::PATCH, item, attrs)
142}
143
144/**
145A procedural macro for adding HTTP headers to a request. It includes the following attributes:
146- name - HTTP header name (String)
147- value - HTTP header value (String)
148- sensitive - sensitive HTTP header value (true/false)
149
150It also supports filling #[segment] into header values.
151*/
152#[proc_macro_attribute]
153pub fn header(attrs: TokenStream, item: TokenStream) -> TokenStream {
154 parse_header(item, attrs)
155}
156
157/**
158A procedural macro for generating DTO objects.
159*/
160#[proc_macro_attribute]
161pub fn data_transfer(_: TokenStream, item: TokenStream) -> TokenStream {
162 let item = parse_macro_input!(item as ItemStruct);
163 let vis = item.vis.clone();
164 let ident = item.ident.clone();
165 let fields = item.fields.clone();
166 let attrs = item.attrs.clone();
167
168 TokenStream::from(quote! {
169 #(#attrs)*
170 #[derive(clientix::prelude::serde::Serialize, clientix::prelude::serde::Deserialize, Clone, Debug)]
171 #[serde(crate = "clientix::prelude::serde")]
172 #vis struct #ident #fields
173 })
174}
175
176#[proc_macro_attribute]
177pub fn request_args(_: TokenStream, item: TokenStream) -> TokenStream {
178 let item = parse_macro_input!(item as ItemStruct);
179 let vis = item.vis.clone();
180 let ident = item.ident.clone();
181 let fields = item.fields.clone();
182 let attrs = item.attrs.clone();
183
184 TokenStream::from(quote! {
185 #(#attrs)*
186 #[derive(clientix::RequestArgs, Clone, Debug)]
187 #vis struct #ident #fields
188 })
189}
190
191#[proc_macro_derive(RequestArgs, attributes(segment, query, header, body))]
192pub fn request_args_derive(item: TokenStream) -> TokenStream {
193 let derive_input = parse_macro_input!(item as DeriveInput);
194 let request_args = RequestArgsCompiler::parse(derive_input).compile();
195 TokenStream::from(request_args)
196}