macros: Merge ruma-api-macros into ruma-macros
This commit is contained in:
parent
b7ce34b4d7
commit
0d49715f29
1
.github/CODEOWNERS
vendored
1
.github/CODEOWNERS
vendored
@ -1,5 +1,4 @@
|
||||
* @jplatte
|
||||
/crates/ruma-api-macros/ @ruma/macro-owners
|
||||
/crates/ruma-common/ @iinuwa
|
||||
/crates/ruma-federation-api/ @DevinR528
|
||||
/crates/ruma-identifiers/ @iinuwa
|
||||
|
2
.github/workflows/docs.yml
vendored
2
.github/workflows/docs.yml
vendored
@ -30,7 +30,7 @@ jobs:
|
||||
RUSTDOCFLAGS: "--enable-index-page -Zunstable-options --cfg docsrs"
|
||||
with:
|
||||
command: doc
|
||||
args: --no-deps --workspace --exclude ruma-api-macros --exclude ruma-macros --exclude ruma-identifiers-validation --exclude xtask --all-features -Zrustdoc-map
|
||||
args: --no-deps --workspace --exclude ruma-macros --exclude ruma-identifiers-validation --exclude xtask --all-features -Zrustdoc-map
|
||||
- name: Deploy to docs branch
|
||||
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
|
||||
uses: JamesIves/github-pages-deploy-action@4.1.4
|
||||
|
@ -1,172 +0,0 @@
|
||||
Since version 0.15.1 of ruma-api, ruma-api-macros is versioned in lockstep with ruma-api. Since
|
||||
ruma-api-macros cannot be used independently anyway, it no longer maintains a separate change log or
|
||||
its own version. Instead, refer to ruma-api's change log for changes in versions 0.15.1 and above.
|
||||
|
||||
# 0.12.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Update code generation to match the changes in ruma-api 0.15.0
|
||||
|
||||
# 0.11.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Use `TryFrom<&str>` instead of serde_json for path segment deserialization
|
||||
|
||||
# 0.10.1
|
||||
|
||||
Improvements:
|
||||
|
||||
* Derive `Debug` for `Incoming` types
|
||||
|
||||
# 0.10.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Update code generation to match the changes in ruma-api 0.13.0
|
||||
|
||||
# 0.9.1
|
||||
|
||||
Improvements:
|
||||
|
||||
* Add `#[ruma_api(raw_body)]` attribute to `ruma_api!`'s grammar
|
||||
* This attribute is used to bypass (de)serialization for endpoints where the HTTP request /
|
||||
response is arbitrary data rather than some JSON structure
|
||||
|
||||
# 0.9.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Updated code generation to match the changes in ruma-api 0.12.0
|
||||
|
||||
New features:
|
||||
|
||||
* Added a derive macro for the new `Outgoing` trait from ruma-api
|
||||
|
||||
# 0.8.2
|
||||
|
||||
Bug fixes:
|
||||
|
||||
* Fix handling of `request` / `response` blocks containing fields with serde attributes ([#31][])
|
||||
|
||||
[#31]: https://github.com/ruma/ruma-api/pull/31
|
||||
|
||||
# 0.8.1
|
||||
|
||||
Improvements:
|
||||
|
||||
* Add spans to almost every error that can come up in `ruma_api!`
|
||||
* Add a new field kind: `#[ruma_api(query_map)]` ([#30][])
|
||||
* This allows endpoints that have a dynamic set of query parameters to be implemented
|
||||
* For details see the documentation of `ruma_api!`
|
||||
* Add more sanity checks
|
||||
* No multiple `#[ruma_api(body)]` fields in one request / response definition
|
||||
* No multiple field kind declarations `#[ruma_api(body|query|path)]` on one field
|
||||
* No (newtype) body fields in GET endpoints
|
||||
* Lots of refactoring of the internals
|
||||
|
||||
[#30]: https://github.com/ruma/ruma-api/pull/30
|
||||
|
||||
# 0.7.1
|
||||
|
||||
Bug fixes:
|
||||
|
||||
* Removed unnecessary dependency on ruma-api 0.9.0.
|
||||
|
||||
# 0.7.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Updated to ruma-api 0.10.0.
|
||||
|
||||
Improvements:
|
||||
|
||||
* ruma-api now re-exports the `ruma_api` macro from ruma-api-macros. Downstream crates no longer need to depend on ruma-api-macros directly.
|
||||
* The code generated by the `ruma_api` macro now refers to external dependencies via re-exports in ruma-api, so it is no longer necessary to add them to the dependencies of downstream crates directly.
|
||||
* The ruma-api and ruma-api-macros repositories have been merged into one Cargo workspace for easier dependency management and development.
|
||||
|
||||
# 0.6.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Updated to ruma-api 0.9.0.
|
||||
|
||||
# 0.5.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Updated to ruma-api 0.8.0.
|
||||
|
||||
Improvements:
|
||||
|
||||
* Generated documentation now includes the names and descriptions of API endpoints.
|
||||
* Remove unidiomatic use of `Tokens::append_all` from the `quote` crate.
|
||||
* ruma-api-macros now uses clippy to improve code quality.
|
||||
|
||||
# 0.4.0
|
||||
|
||||
Improvements:
|
||||
|
||||
* ruma-api-macros now runs on stable Rust, requiring version 1.34 or higher.
|
||||
* Updated all dependencies for upstream improvements.
|
||||
|
||||
# 0.3.1
|
||||
|
||||
Improved:
|
||||
|
||||
* Code updated to use Rust 2018 idioms.
|
||||
|
||||
Bug fixes:
|
||||
|
||||
* The crate will now compile when serde's `derive` feature is enabled.
|
||||
|
||||
# 0.3.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* The procedural macro now uses hyper's `Body` type instead of `Vec<u8>`. This may prove to be a temporary change as ideally we want ruma-api-macros to be agnostic to HTTP client library.
|
||||
|
||||
Improvements:
|
||||
|
||||
* Updated to the latest versions of all dependencies.
|
||||
* Improved error reporting for the procedural macro.
|
||||
* Conversions between this crate's request and response types and the http crate's request and response types are now bidirectional.
|
||||
* Request and response types now implement `Clone`.
|
||||
|
||||
# 0.2.2
|
||||
|
||||
Improvements:
|
||||
|
||||
* Updated to proc-macro2 0.4 for compatibility with the latest nightly Rust.
|
||||
|
||||
Bug fixes:
|
||||
|
||||
* Attributes that don't affect the macro are now ignored instead of causing a panic.
|
||||
* Added missing commas in request query struct initialization that was causing a syntax error.
|
||||
* Fixed stripping of serde attributes that was causing them to leak through and trigger a custom attribute error.
|
||||
* Fixed creation of requests with an empty body that were not correctly using a `Vec<u8>` as the body type.
|
||||
|
||||
# 0.2.1
|
||||
|
||||
Version 0.2.1 was yanked from crates.io due to a dependency issue. Changes since version 0.2.0 are in the release notes for version 0.2.2.
|
||||
|
||||
# 0.2.0
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* The dependency on the `hyper` crate has been removed. The macro now uses types from the `http` crate. The macro is also compatible with the forthcoming version 0.12 of `hyper`.
|
||||
* The `method` field in the `metadata` block is now written as the name of an associated constant from `http::Method`, e.g. `GET`.
|
||||
* HTTP headers are now specified in request and response blocks using `String` as the type, and the name of the constant of the header from `http::header` in the field's attributes. For example:
|
||||
``` rust
|
||||
#[ruma_api(header = "CONTENT_TYPE")]
|
||||
pub content_type: String
|
||||
```
|
||||
|
||||
Improvements:
|
||||
|
||||
* The macro is built using version 0.13 of the `syn` crate.
|
||||
|
||||
# 0.1.0
|
||||
|
||||
Initial release.
|
@ -1,20 +0,0 @@
|
||||
[package]
|
||||
categories = ["api-bindings", "web-programming"]
|
||||
description = "A procedural macro for generating ruma-api Endpoints."
|
||||
homepage = "https://www.ruma.io/"
|
||||
keywords = ["matrix", "chat", "messaging", "ruma"]
|
||||
license = "MIT"
|
||||
name = "ruma-api-macros"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/ruma/ruma"
|
||||
version = "0.20.0"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro-crate = "1.0.0"
|
||||
proc-macro2 = "1.0.24"
|
||||
quote = "1.0.8"
|
||||
syn = { version = "1.0.57", features = ["full", "visit"] }
|
@ -1,59 +0,0 @@
|
||||
# ruma-api-macros
|
||||
|
||||
**ruma-api-macros** provides a procedural macro for easily generating [ruma-api]-compatible API endpoints.
|
||||
You define the endpoint's metadata, request fields, and response fields, and the macro generates all the necessary types and implements all the necessary traits.
|
||||
|
||||
[ruma-api]: https://github.com/ruma/ruma/tree/main/ruma-api
|
||||
|
||||
## Usage
|
||||
|
||||
This crate is not meant to be used directly; instead, you can use it through the re-exports in ruma-api.
|
||||
|
||||
Here is an example that shows most of the macro's functionality:
|
||||
|
||||
```rust
|
||||
pub mod some_endpoint {
|
||||
use ruma_api::ruma_api;
|
||||
|
||||
ruma_api! {
|
||||
metadata: {
|
||||
description: "Does something.",
|
||||
method: GET, // An `http::Method` constant. No imports required.
|
||||
name: "some_endpoint",
|
||||
path: "/_matrix/some/endpoint/:baz", // Variable path components start with a colon.
|
||||
rate_limited: false,
|
||||
authentication: None,
|
||||
}
|
||||
|
||||
request: {
|
||||
// With no attribute on the field, it will be put into the body of the request.
|
||||
pub foo: String,
|
||||
|
||||
// This value will be put into the "Content-Type" HTTP header.
|
||||
#[ruma_api(header = CONTENT_TYPE)]
|
||||
pub content_type: String
|
||||
|
||||
// This value will be put into the query string of the request's URL.
|
||||
#[ruma_api(query)]
|
||||
pub bar: String,
|
||||
|
||||
// This value will be inserted into the request's URL in place of the
|
||||
// ":baz" path component.
|
||||
#[ruma_api(path)]
|
||||
pub baz: String,
|
||||
}
|
||||
|
||||
response: {
|
||||
// This value will be extracted from the "Content-Type" HTTP header.
|
||||
#[ruma_api(header = CONTENT_TYPE)]
|
||||
pub content_type: String
|
||||
|
||||
// With no attribute on the field, it will be extracted from the body of the response.
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
// An error can also be specified or defaults to `ruma_api::error::MatrixError`.
|
||||
error: crate::Error
|
||||
}
|
||||
}
|
||||
```
|
@ -1,55 +0,0 @@
|
||||
#![doc(html_favicon_url = "https://www.ruma.io/favicon.ico")]
|
||||
#![doc(html_logo_url = "https://www.ruma.io/images/logo.png")]
|
||||
//! A procedural macro for easily generating [ruma-api]-compatible endpoints.
|
||||
//!
|
||||
//! This crate should never be used directly; instead, use it through the
|
||||
//! re-exports in ruma-api. Also note that for technical reasons, the
|
||||
//! `ruma_api!` macro is only documented in ruma-api, not here.
|
||||
//!
|
||||
//! [ruma-api]: https://github.com/ruma/ruma/tree/main/ruma-api
|
||||
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{parse_macro_input, DeriveInput};
|
||||
|
||||
mod api;
|
||||
mod attribute;
|
||||
mod auth_scheme;
|
||||
mod request;
|
||||
mod response;
|
||||
mod util;
|
||||
mod version;
|
||||
|
||||
use api::Api;
|
||||
use request::expand_derive_request;
|
||||
use response::expand_derive_response;
|
||||
|
||||
#[proc_macro]
|
||||
pub fn ruma_api(input: TokenStream) -> TokenStream {
|
||||
let api = parse_macro_input!(input as Api);
|
||||
api.expand_all().into()
|
||||
}
|
||||
|
||||
/// Internal helper taking care of the request-specific parts of `ruma_api!`.
|
||||
#[proc_macro_derive(Request, attributes(ruma_api))]
|
||||
pub fn derive_request(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
expand_derive_request(input).unwrap_or_else(syn::Error::into_compile_error).into()
|
||||
}
|
||||
|
||||
/// Internal helper taking care of the response-specific parts of `ruma_api!`.
|
||||
#[proc_macro_derive(Response, attributes(ruma_api))]
|
||||
pub fn derive_response(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
expand_derive_response(input).unwrap_or_else(syn::Error::into_compile_error).into()
|
||||
}
|
||||
|
||||
/// A derive macro that generates no code, but registers the ruma_api attribute so both
|
||||
/// `#[ruma_api(...)]` and `#[cfg_attr(..., ruma_api(...))]` are accepted on the type, its fields
|
||||
/// and (in case the input is an enum) variants fields.
|
||||
#[doc(hidden)]
|
||||
#[proc_macro_derive(_FakeDeriveRumaApi, attributes(ruma_api))]
|
||||
pub fn fake_derive_ruma_api(_input: TokenStream) -> TokenStream {
|
||||
TokenStream::new()
|
||||
}
|
@ -25,8 +25,8 @@ server = []
|
||||
bytes = "1.0.1"
|
||||
http = "0.2.2"
|
||||
percent-encoding = "2.1.0"
|
||||
ruma-api-macros = { version = "=0.20.0", path = "../ruma-api-macros" }
|
||||
ruma-identifiers = { version = "0.22.0", path = "../ruma-identifiers" }
|
||||
ruma-macros = { version = "=0.1.0", path = "../ruma-macros" }
|
||||
ruma-serde = { version = "0.6.0", path = "../ruma-serde" }
|
||||
serde = { version = "1.0.118", features = ["derive"] }
|
||||
serde_json = "1.0.61"
|
||||
|
@ -128,7 +128,7 @@ use ruma_identifiers::UserId;
|
||||
///
|
||||
/// ```
|
||||
/// pub mod some_endpoint {
|
||||
/// use ruma_api_macros::ruma_api;
|
||||
/// use ruma_api::ruma_api;
|
||||
///
|
||||
/// ruma_api! {
|
||||
/// metadata: {
|
||||
@ -164,7 +164,7 @@ use ruma_identifiers::UserId;
|
||||
/// }
|
||||
///
|
||||
/// pub mod newtype_body_endpoint {
|
||||
/// use ruma_api_macros::ruma_api;
|
||||
/// use ruma_api::ruma_api;
|
||||
/// use serde::{Deserialize, Serialize};
|
||||
///
|
||||
/// #[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
@ -195,17 +195,17 @@ use ruma_identifiers::UserId;
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub use ruma_api_macros::ruma_api;
|
||||
pub use ruma_macros::ruma_api;
|
||||
|
||||
pub mod error;
|
||||
/// This module is used to support the generated code from ruma-api-macros.
|
||||
/// This module is used to support the generated code from ruma-macros.
|
||||
/// It is not considered part of ruma-api's public API.
|
||||
#[doc(hidden)]
|
||||
pub mod exports {
|
||||
pub use bytes;
|
||||
pub use http;
|
||||
pub use percent_encoding;
|
||||
pub use ruma_api_macros;
|
||||
pub use ruma_macros;
|
||||
pub use ruma_serde;
|
||||
pub use serde;
|
||||
pub use serde_json;
|
||||
|
@ -21,4 +21,4 @@ proc-macro-crate = "1.0.0"
|
||||
proc-macro2 = "1.0.24"
|
||||
quote = "1.0.8"
|
||||
ruma-identifiers-validation = { version = "0.7.0", path = "../ruma-identifiers-validation", default-features = false }
|
||||
syn = { version = "1.0.57", features = ["extra-traits", "full"] }
|
||||
syn = { version = "1.0.57", features = ["extra-traits", "full", "visit"] }
|
||||
|
@ -1,4 +1,6 @@
|
||||
//! Details of the `ruma_api` procedural macro.
|
||||
//! Methods and types for generating [ruma-api] endpoints.
|
||||
//!
|
||||
//! [ruma-api]: https://github.com/ruma/ruma/tree/main/ruma-api
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
@ -8,12 +10,17 @@ use syn::{
|
||||
Attribute, Field, Token, Type,
|
||||
};
|
||||
|
||||
mod metadata;
|
||||
mod request;
|
||||
mod response;
|
||||
use self::{api_metadata::Metadata, api_request::Request, api_response::Response};
|
||||
|
||||
use self::{metadata::Metadata, request::Request, response::Response};
|
||||
use crate::util;
|
||||
mod api_metadata;
|
||||
mod api_request;
|
||||
mod api_response;
|
||||
mod attribute;
|
||||
mod auth_scheme;
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
mod util;
|
||||
mod version;
|
||||
|
||||
mod kw {
|
||||
use syn::custom_keyword;
|
@ -7,7 +7,7 @@ use syn::{
|
||||
Ident, LitBool, LitStr, Token,
|
||||
};
|
||||
|
||||
use crate::{auth_scheme::AuthScheme, util, version::MatrixVersionLiteral};
|
||||
use super::{auth_scheme::AuthScheme, util, version::MatrixVersionLiteral};
|
||||
|
||||
mod kw {
|
||||
syn::custom_keyword!(metadata);
|
@ -9,8 +9,11 @@ use syn::{
|
||||
Lifetime, Token,
|
||||
};
|
||||
|
||||
use super::{kw, metadata::Metadata};
|
||||
use crate::util::{all_cfgs, extract_cfg};
|
||||
use super::{
|
||||
api_metadata::Metadata,
|
||||
kw,
|
||||
util::{all_cfgs, extract_cfg},
|
||||
};
|
||||
|
||||
/// The result of processing the `request` section of the macro.
|
||||
pub(crate) struct Request {
|
||||
@ -70,7 +73,7 @@ impl Request {
|
||||
error_ty: &TokenStream,
|
||||
ruma_api: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let ruma_api_macros = quote! { #ruma_api::exports::ruma_api_macros };
|
||||
let ruma_macros = quote! { #ruma_api::exports::ruma_macros };
|
||||
let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
|
||||
|
||||
let docs = format!(
|
||||
@ -96,12 +99,12 @@ impl Request {
|
||||
#[derive(
|
||||
Clone,
|
||||
Debug,
|
||||
#ruma_api_macros::Request,
|
||||
#ruma_macros::Request,
|
||||
#ruma_serde::Outgoing,
|
||||
#ruma_serde::_FakeDeriveSerde,
|
||||
)]
|
||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||
#[incoming_derive(!Deserialize, #ruma_api_macros::_FakeDeriveRumaApi)]
|
||||
#[incoming_derive(!Deserialize, #ruma_macros::_FakeDeriveRumaApi)]
|
||||
#[ruma_api(
|
||||
method = #method,
|
||||
authentication = #authentication,
|
@ -4,7 +4,7 @@ use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Field, Ident, Token};
|
||||
|
||||
use super::{kw, metadata::Metadata};
|
||||
use super::{api_metadata::Metadata, kw};
|
||||
|
||||
/// The result of processing the `response` section of the macro.
|
||||
pub(crate) struct Response {
|
||||
@ -25,7 +25,7 @@ impl Response {
|
||||
error_ty: &TokenStream,
|
||||
ruma_api: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let ruma_api_macros = quote! { #ruma_api::exports::ruma_api_macros };
|
||||
let ruma_macros = quote! { #ruma_api::exports::ruma_macros };
|
||||
let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
|
||||
|
||||
let docs =
|
||||
@ -39,12 +39,12 @@ impl Response {
|
||||
#[derive(
|
||||
Clone,
|
||||
Debug,
|
||||
#ruma_api_macros::Response,
|
||||
#ruma_macros::Response,
|
||||
#ruma_serde::Outgoing,
|
||||
#ruma_serde::_FakeDeriveSerde,
|
||||
)]
|
||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||
#[incoming_derive(!Deserialize, #ruma_api_macros::_FakeDeriveRumaApi)]
|
||||
#[incoming_derive(!Deserialize, #ruma_macros::_FakeDeriveRumaApi)]
|
||||
#[ruma_api(error_ty = #error_ty)]
|
||||
#( #struct_attributes )*
|
||||
pub struct #response_ident {
|
@ -13,7 +13,7 @@ use syn::{
|
||||
DeriveInput, Field, Generics, Ident, Lifetime, Lit, LitStr, Token, Type,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
use super::{
|
||||
attribute::{Meta, MetaNameValue, MetaValue},
|
||||
auth_scheme::AuthScheme,
|
||||
util::{collect_lifetime_idents, import_ruma_api},
|
||||
@ -197,7 +197,7 @@ impl Request {
|
||||
|
||||
fn expand_all(&self) -> TokenStream {
|
||||
let ruma_api = import_ruma_api();
|
||||
let ruma_api_macros = quote! { #ruma_api::exports::ruma_api_macros };
|
||||
let ruma_macros = quote! { #ruma_api::exports::ruma_macros };
|
||||
let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
|
||||
let serde = quote! { #ruma_api::exports::serde };
|
||||
|
||||
@ -216,7 +216,7 @@ impl Request {
|
||||
/// Data in the request body.
|
||||
#[derive(
|
||||
Debug,
|
||||
#ruma_api_macros::_FakeDeriveRumaApi,
|
||||
#ruma_macros::_FakeDeriveRumaApi,
|
||||
#ruma_serde::Outgoing,
|
||||
#serde::Serialize,
|
||||
#derive_deserialize
|
||||
@ -244,7 +244,7 @@ impl Request {
|
||||
/// Data in the request's query string.
|
||||
#[derive(
|
||||
Debug,
|
||||
#ruma_api_macros::_FakeDeriveRumaApi,
|
||||
#ruma_macros::_FakeDeriveRumaApi,
|
||||
#ruma_serde::Outgoing,
|
||||
#serde::Serialize,
|
||||
#derive_deserialize
|
@ -3,7 +3,7 @@ use quote::quote;
|
||||
use syn::Field;
|
||||
|
||||
use super::{Request, RequestField};
|
||||
use crate::auth_scheme::AuthScheme;
|
||||
use crate::api::auth_scheme::AuthScheme;
|
||||
|
||||
impl Request {
|
||||
pub fn expand_incoming(&self, ruma_api: &TokenStream) -> TokenStream {
|
@ -3,7 +3,7 @@ use quote::quote;
|
||||
use syn::{Field, LitStr};
|
||||
|
||||
use super::{Request, RequestField};
|
||||
use crate::{auth_scheme::AuthScheme, util};
|
||||
use crate::api::{auth_scheme::AuthScheme, util};
|
||||
|
||||
impl Request {
|
||||
pub fn expand_outgoing(&self, ruma_api: &TokenStream) -> TokenStream {
|
@ -13,7 +13,7 @@ use syn::{
|
||||
DeriveInput, Field, Generics, Ident, Lifetime, Token, Type,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
use super::{
|
||||
attribute::{Meta, MetaNameValue},
|
||||
util,
|
||||
};
|
||||
@ -96,7 +96,7 @@ impl Response {
|
||||
|
||||
fn expand_all(&self) -> TokenStream {
|
||||
let ruma_api = util::import_ruma_api();
|
||||
let ruma_api_macros = quote! { #ruma_api::exports::ruma_api_macros };
|
||||
let ruma_macros = quote! { #ruma_api::exports::ruma_macros };
|
||||
let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
|
||||
let serde = quote! { #ruma_api::exports::serde };
|
||||
|
||||
@ -112,7 +112,7 @@ impl Response {
|
||||
/// Data in the response body.
|
||||
#[derive(
|
||||
Debug,
|
||||
#ruma_api_macros::_FakeDeriveRumaApi,
|
||||
#ruma_macros::_FakeDeriveRumaApi,
|
||||
#ruma_serde::Outgoing,
|
||||
#serde_derives
|
||||
)]
|
@ -17,6 +17,7 @@ use ruma_identifiers_validation::{
|
||||
use syn::{parse_macro_input, DeriveInput, ItemEnum};
|
||||
|
||||
use self::{
|
||||
api::{request::expand_derive_request, response::expand_derive_response, Api},
|
||||
events::{
|
||||
event::expand_event,
|
||||
event_content::expand_event_content,
|
||||
@ -37,6 +38,7 @@ use self::{
|
||||
},
|
||||
};
|
||||
|
||||
mod api;
|
||||
mod events;
|
||||
mod identifiers;
|
||||
mod serde;
|
||||
@ -393,3 +395,37 @@ pub fn derive_string_enum(input: TokenStream) -> TokenStream {
|
||||
pub fn fake_derive_serde(_input: TokenStream) -> TokenStream {
|
||||
TokenStream::new()
|
||||
}
|
||||
|
||||
/// A procedural macro for easily generating [ruma-api]-compatible endpoints.
|
||||
///
|
||||
/// Note that for technical reasons, the `ruma_api!` macro is only documented in ruma-api, not here.
|
||||
///
|
||||
/// [ruma-api]: https://github.com/ruma/ruma/tree/main/ruma-api
|
||||
#[proc_macro]
|
||||
pub fn ruma_api(input: TokenStream) -> TokenStream {
|
||||
let api = parse_macro_input!(input as Api);
|
||||
api.expand_all().into()
|
||||
}
|
||||
|
||||
/// Internal helper taking care of the request-specific parts of `ruma_api!`.
|
||||
#[proc_macro_derive(Request, attributes(ruma_api))]
|
||||
pub fn derive_request(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
expand_derive_request(input).unwrap_or_else(syn::Error::into_compile_error).into()
|
||||
}
|
||||
|
||||
/// Internal helper taking care of the response-specific parts of `ruma_api!`.
|
||||
#[proc_macro_derive(Response, attributes(ruma_api))]
|
||||
pub fn derive_response(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
expand_derive_response(input).unwrap_or_else(syn::Error::into_compile_error).into()
|
||||
}
|
||||
|
||||
/// A derive macro that generates no code, but registers the ruma_api attribute so both
|
||||
/// `#[ruma_api(...)]` and `#[cfg_attr(..., ruma_api(...))]` are accepted on the type, its fields
|
||||
/// and (in case the input is an enum) variants fields.
|
||||
#[doc(hidden)]
|
||||
#[proc_macro_derive(_FakeDeriveRumaApi, attributes(ruma_api))]
|
||||
pub fn fake_derive_ruma_api(_input: TokenStream) -> TokenStream {
|
||||
TokenStream::new()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user