http_provider_macro/lib.rs
1//! Generate HTTP client methods from endpoint definitions.
2//!
3//! ```ignore
4//! use http_provider_macro::http_provider;
5//! use serde::{Deserialize, Serialize};
6//!
7//! #[derive(Serialize, Deserialize)]
8//! struct User {
9//! id: u32,
10//! name: String,
11//! }
12//!
13//! http_provider!(
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::HttpProviderExpander;
44use crate::input::HttpProviderInput;
45use syn::parse_macro_input;
46
47mod error;
48mod expanders;
49mod input;
50
51#[proc_macro]
52pub fn http_provider(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
53 let input = parse_macro_input!(input as HttpProviderInput);
54 match HttpProviderExpander::new(input).expand() {
55 Ok(tokens) => tokens.into(),
56 Err(err) => err.to_compile_error().into(),
57 }
58}