http_provider_macro/lib.rs
1//! Generate HTTP client methods from endpoint definitions.
2//!
3//! ```ignore
4//! use http_provider_macro::api_client;
5//! use serde::{Deserialize, Serialize};
6//!
7//! #[derive(Serialize, Deserialize)]
8//! struct User {
9//! id: u32,
10//! name: String,
11//! }
12//!
13//! api_client!(
14//! UserApi,
15//! {
16//! {
17//! path: "/users",
18//! method: GET,
19//! res: Vec<User>,
20//! },
21//! {
22//! path: "/users/{id}",
23//! method: GET,
24//! path_params: UserPath,
25//! res: User,
26//! }
27//! }
28//! );
29//!
30//! #[derive(Serialize)]
31//! struct UserPath {
32//! id: u32,
33//! }
34//!
35//! // Usage
36//! let client = UserApi::new(reqwest::Url::parse("https://api.example.com")?, Some(30));
37//! let users = client.get_users().await?;
38//! let user = client.get_users_by_id(&UserPath { id: 1 }).await?;
39//! ```
40
41extern crate proc_macro;
42
43use crate::expanders::ApiClientExpander;
44use crate::input::ApiClientInput;
45use syn::parse_macro_input;
46
47mod error;
48mod expanders;
49mod input;
50
51fn expand_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
52 let input = parse_macro_input!(input as ApiClientInput);
53 match ApiClientExpander::new(input).expand() {
54 Ok(tokens) => tokens.into(),
55 Err(err) => err.into_compile_error().into(),
56 }
57}
58
59#[proc_macro]
60pub fn api_client(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
61 expand_macro(input)
62}
63
64#[proc_macro]
65pub fn http_provider(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
66 expand_macro(input)
67}