ruma_api_macros/
lib.rs

1#![doc(html_favicon_url = "https://www.ruma.io/favicon.ico")]
2#![doc(html_logo_url = "https://www.ruma.io/images/logo.png")]
3//! A procedural macro for easily generating [ruma-api]-compatible endpoints.
4//!
5//! This crate should never be used directly; instead, use it through the
6//! re-exports in ruma-api. Also note that for technical reasons, the
7//! `ruma_api!` macro is only documented in ruma-api, not here.
8//!
9//! [ruma-api]: https://github.com/ruma/ruma/tree/main/ruma-api
10
11#![recursion_limit = "256"]
12
13use proc_macro::TokenStream;
14use syn::{parse_macro_input, DeriveInput};
15
16mod api;
17mod attribute;
18mod auth_scheme;
19mod request;
20mod response;
21mod util;
22mod version;
23
24use api::Api;
25use request::expand_derive_request;
26use response::expand_derive_response;
27
28#[proc_macro]
29pub fn ruma_api(input: TokenStream) -> TokenStream {
30    let api = parse_macro_input!(input as Api);
31    api.expand_all().into()
32}
33
34/// Internal helper taking care of the request-specific parts of `ruma_api!`.
35#[proc_macro_derive(Request, attributes(ruma_api))]
36pub fn derive_request(input: TokenStream) -> TokenStream {
37    let input = parse_macro_input!(input as DeriveInput);
38    expand_derive_request(input).unwrap_or_else(syn::Error::into_compile_error).into()
39}
40
41/// Internal helper taking care of the response-specific parts of `ruma_api!`.
42#[proc_macro_derive(Response, attributes(ruma_api))]
43pub fn derive_response(input: TokenStream) -> TokenStream {
44    let input = parse_macro_input!(input as DeriveInput);
45    expand_derive_response(input).unwrap_or_else(syn::Error::into_compile_error).into()
46}
47
48/// A derive macro that generates no code, but registers the ruma_api attribute so both
49/// `#[ruma_api(...)]` and `#[cfg_attr(..., ruma_api(...))]` are accepted on the type, its fields
50/// and (in case the input is an enum) variants fields.
51#[doc(hidden)]
52#[proc_macro_derive(_FakeDeriveRumaApi, attributes(ruma_api))]
53pub fn fake_derive_ruma_api(_input: TokenStream) -> TokenStream {
54    TokenStream::new()
55}