reqwest-builder-derive 0.1.2

Derive macros for reqwest-builder
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Fields, Lit, parse_macro_input};

/// Derive macro for IntoReqwestBuilder
///
/// This macro allows you to define HTTP request properties directly on struct fields:
///
/// # Attributes
///
/// ## Container attributes (on the struct):
/// - `#[request(method = "GET|POST|PUT|DELETE|PATCH")]` - HTTP method (required)
/// - `#[request(path = "/endpoint")]` - Base endpoint path (required)
/// - `#[request(body = "json|form|multipart|none")]` - Body type (optional, defaults to "json")
///
/// ## Field attributes:
/// - `#[path_param]` - Include this field in the URL path (replaces `{field_name}` in path)
/// - `#[query]` - Include this field as a query parameter
/// - `#[query(name = "param_name")]` - Include as query parameter with custom name
/// - `#[header]` - Include this field as a header
/// - `#[header(name = "header_name")]` - Include as header with custom name
/// - `#[body]` - Include this field in the request body (default for unmarked fields)
///
/// # Example
///
/// ```rust
/// use reqwest_builder::{IntoReqwestBuilder, RequestBody};
/// use serde::Serialize;
///
/// #[derive(Serialize, IntoReqwestBuilder)]
/// #[request(method = "POST", path = "/users/{id}/posts")]
/// struct CreatePostRequest {
///     #[path_param]
///     id: u64,
///     
///     #[query]
///     draft: Option<bool>,
///     
///     #[header(name = "Authorization")]
///     auth_token: String,
///     
///     #[header(name = "Content-Type")]
///     content_type: String,
///     
///     // These fields go into the request body
///     title: String,
///     content: String,
/// }
/// ```
#[proc_macro_derive(
    IntoReqwestBuilder,
    attributes(request, path_param, query, header, body)
)]
pub fn derive_into_reqwest_builder(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    match impl_into_reqwest_builder(&input) {
        Ok(output) => output.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

fn impl_into_reqwest_builder(input: &DeriveInput) -> Result<proc_macro2::TokenStream, syn::Error> {
    let name = &input.ident;

    // Parse container attributes
    let container_attrs = parse_container_attributes(&input.attrs)?;
    let method = container_attrs.method;
    let path = container_attrs.path;
    let body_type = container_attrs.body_type;

    // Extract struct fields
    let fields = match &input.data {
        Data::Struct(data_struct) => match &data_struct.fields {
            Fields::Named(fields) => &fields.named,
            _ => {
                return Err(syn::Error::new_spanned(
                    input,
                    "Only named fields are supported",
                ));
            }
        },
        _ => return Err(syn::Error::new_spanned(input, "Only structs are supported")),
    };

    // Analyze fields for different purposes
    let mut path_fields = Vec::new();
    let mut query_fields = Vec::new();
    let mut header_fields = Vec::new();

    for field in fields {
        let field_name = field.ident.as_ref().unwrap();
        let field_attrs = parse_field_attributes(&field.attrs)?;

        match field_attrs.field_type {
            FieldType::Path => {
                path_fields.push(field_name);
            }
            FieldType::Query { name } => {
                let param_name = name.unwrap_or_else(|| field_name.to_string());
                query_fields.push((field_name, param_name));
            }
            FieldType::Header { name } => {
                let header_name = name.unwrap_or_else(|| field_name.to_string());
                header_fields.push((field_name, header_name));
            }
            FieldType::Body => {
                // Body fields are handled automatically by serde serialization
                // We don't need to do anything special for them
            }
        }
    }

    // Generate the endpoint method with path substitution
    let endpoint_impl = generate_endpoint_impl(&path, &path_fields);

    // Generate query params method
    let query_params_impl = generate_query_params_impl(&query_fields);

    // Generate headers method and Headers type
    let (headers_type, headers_impl, headers_struct_name) =
        generate_headers_impl(name, &header_fields);

    // Generate the method implementation
    let method_impl = quote! {
        fn method(&self) -> http::Method {
            #method
        }
    };

    // Generate body type implementation
    let body_impl = quote! {
        fn body(&self) -> ::reqwest_builder::RequestBody {
            #body_type
        }
    };

    Ok(quote! {
        #headers_type

        impl ::reqwest_builder::IntoReqwestBuilder for #name {
            type Headers = #headers_struct_name;

            #method_impl

            #endpoint_impl

            #headers_impl

            #query_params_impl

            #body_impl
        }
    })
}

#[derive(Debug)]
struct ContainerAttributes {
    method: proc_macro2::TokenStream,
    path: String,
    body_type: proc_macro2::TokenStream,
}

#[derive(Debug)]
struct FieldAttributes {
    field_type: FieldType,
}

#[derive(Debug)]
enum FieldType {
    Path,
    Query { name: Option<String> },
    Header { name: Option<String> },
    Body,
}

fn parse_container_attributes(attrs: &[syn::Attribute]) -> Result<ContainerAttributes, syn::Error> {
    let mut method = None;
    let mut path = None;
    let mut body_type = quote! { reqwest_builder::RequestBody::Json }; // Default to JSON

    for attr in attrs {
        if attr.path().is_ident("request") {
            attr.parse_nested_meta(|meta| {
                if meta.path.is_ident("method") {
                    let value: Lit = meta.value()?.parse()?;
                    if let Lit::Str(lit_str) = value {
                        method = Some(match lit_str.value().as_str() {
                            "GET" => quote! { http::Method::GET },
                            "POST" => quote! { http::Method::POST },
                            "PUT" => quote! { http::Method::PUT },
                            "DELETE" => quote! { http::Method::DELETE },
                            "PATCH" => quote! { http::Method::PATCH },
                            "HEAD" => quote! { http::Method::HEAD },
                            "OPTIONS" => quote! { http::Method::OPTIONS },
                            other => {
                                return Err(
                                    meta.error(format!("Unsupported HTTP method: {}", other))
                                );
                            }
                        });
                    }
                } else if meta.path.is_ident("path") {
                    let value: Lit = meta.value()?.parse()?;
                    if let Lit::Str(lit_str) = value {
                        path = Some(lit_str.value());
                    }
                } else if meta.path.is_ident("body") {
                    let value: Lit = meta.value()?.parse()?;
                    if let Lit::Str(lit_str) = value {
                        body_type = match lit_str.value().as_str() {
                            "json" => quote! { reqwest_builder::RequestBody::Json },
                            "form" => quote! { reqwest_builder::RequestBody::Form },
                            "multipart" => quote! { reqwest_builder::RequestBody::Multipart },
                            "none" => quote! { reqwest_builder::RequestBody::None },
                            other => {
                                return Err(meta.error(format!("Unsupported body type: {}", other)));
                            }
                        };
                    }
                }
                Ok(())
            })?;
        }
    }

    let method = method
        .ok_or_else(|| syn::Error::new_spanned(&attrs[0], "Missing required 'method' attribute"))?;
    let path = path
        .ok_or_else(|| syn::Error::new_spanned(&attrs[0], "Missing required 'path' attribute"))?;

    Ok(ContainerAttributes {
        method,
        path,
        body_type,
    })
}

fn parse_field_attributes(attrs: &[syn::Attribute]) -> Result<FieldAttributes, syn::Error> {
    for attr in attrs {
        if attr.path().is_ident("path_param") {
            return Ok(FieldAttributes {
                field_type: FieldType::Path,
            });
        } else if attr.path().is_ident("query") {
            let mut name = None;

            // Try to parse nested meta if the attribute has arguments
            let _ = attr.parse_nested_meta(|meta| {
                if meta.path.is_ident("name") {
                    let value: Lit = meta.value()?.parse()?;
                    if let Lit::Str(lit_str) = value {
                        name = Some(lit_str.value());
                    }
                }
                Ok(())
            });

            return Ok(FieldAttributes {
                field_type: FieldType::Query { name },
            });
        } else if attr.path().is_ident("header") {
            let mut name = None;

            // Try to parse nested meta if the attribute has arguments
            let _ = attr.parse_nested_meta(|meta| {
                if meta.path.is_ident("name") {
                    let value: Lit = meta.value()?.parse()?;
                    if let Lit::Str(lit_str) = value {
                        name = Some(lit_str.value());
                    }
                }
                Ok(())
            });

            return Ok(FieldAttributes {
                field_type: FieldType::Header { name },
            });
        } else if attr.path().is_ident("body") {
            return Ok(FieldAttributes {
                field_type: FieldType::Body,
            });
        }
    }

    // Default to body field if no attribute is specified
    Ok(FieldAttributes {
        field_type: FieldType::Body,
    })
}

fn generate_endpoint_impl(path: &str, path_fields: &[&syn::Ident]) -> proc_macro2::TokenStream {
    if path_fields.is_empty() {
        quote! {
            fn endpoint(&self) -> String {
                #path.to_string()
            }
        }
    } else {
        let mut endpoint_code = quote! {
            let mut endpoint = #path.to_string();
        };

        for field in path_fields {
            let field_name_str = field.to_string();
            let placeholder = format!("{{{}}}", field_name_str);

            endpoint_code.extend(quote! {
                endpoint = endpoint.replace(#placeholder, &self.#field.to_string());
            });
        }

        endpoint_code.extend(quote! {
            endpoint
        });

        quote! {
            fn endpoint(&self) -> String {
                #endpoint_code
            }
        }
    }
}

fn generate_query_params_impl(query_fields: &[(&syn::Ident, String)]) -> proc_macro2::TokenStream {
    if query_fields.is_empty() {
        quote! {
            fn query_params(&self) -> Option<std::collections::HashMap<String, String>> {
                None
            }
        }
    } else {
        let param_insertions: Vec<_> = query_fields
            .iter()
            .map(|(field, param_name)| {
                quote! {
                    // Handle query parameters - this works for both Option and non-Option types
                    let field_ref = &self.#field;
                    reqwest_builder::query_param_helper(field_ref, #param_name, &mut params);
                }
            })
            .collect();

        quote! {
            fn query_params(&self) -> Option<std::collections::HashMap<String, String>> {
                let mut params = std::collections::HashMap::new();
                #(#param_insertions)*
                if params.is_empty() {
                    None
                } else {
                    Some(params)
                }
            }
        }
    }
}

fn generate_headers_impl(
    struct_name: &syn::Ident,
    header_fields: &[(&syn::Ident, String)],
) -> (
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
) {
    let headers_struct_name = quote::format_ident!("{}Headers", struct_name);

    if header_fields.is_empty() {
        let headers_type = quote! {
            #[derive(serde::Serialize, Clone)]
            pub struct #headers_struct_name;
        };

        let headers_impl = quote! {
            fn headers(&self) -> Option<Self::Headers> {
                None
            }
        };

        return (headers_type, headers_impl, quote! { #headers_struct_name });
    }

    let header_struct_fields: Vec<_> = header_fields
        .iter()
        .map(|(field, header_name)| {
            let field_type = quote! { String };
            quote! {
                #[serde(rename = #header_name)]
                pub #field: #field_type
            }
        })
        .collect();

    let headers_type = quote! {
        #[derive(serde::Serialize, Clone)]
        pub struct #headers_struct_name {
            #(#header_struct_fields),*
        }
    };

    let header_assignments: Vec<_> = header_fields
        .iter()
        .map(|(field, _)| {
            quote! {
                #field: self.#field.to_string()
            }
        })
        .collect();

    let headers_impl = quote! {
        fn headers(&self) -> Option<Self::Headers> {
            Some(#headers_struct_name {
                #(#header_assignments),*
            })
        }
    };

    (headers_type, headers_impl, quote! { #headers_struct_name })
}