api-macros: Replace ToTokens implementations with inherent methods
This commit is contained in:
parent
2e0f787ccd
commit
536a8aea1b
@ -69,8 +69,8 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
||||
})
|
||||
.collect();
|
||||
|
||||
let request_type = &api.request;
|
||||
let response_type = &api.response;
|
||||
let request_type = api.request.expand_type_def(&ruma_api);
|
||||
let response_type = api.response.expand_type_def(&ruma_api);
|
||||
|
||||
let incoming_request_type =
|
||||
if api.request.contains_lifetimes() { quote!(IncomingRequest) } else { quote!(Request) };
|
||||
@ -100,7 +100,7 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
||||
api.request.request_init_query_fields()
|
||||
};
|
||||
|
||||
let mut header_kvs = api.request.append_header_kvs();
|
||||
let mut header_kvs = api.request.append_header_kvs(&ruma_api);
|
||||
for auth in &api.metadata.authentication {
|
||||
if auth.value == "AccessToken" {
|
||||
let attrs = &auth.attrs;
|
||||
@ -160,7 +160,7 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
||||
};
|
||||
|
||||
let parse_request_headers = if api.request.has_header_fields() {
|
||||
api.request.parse_headers_from_request()
|
||||
api.request.parse_headers_from_request(&ruma_api)
|
||||
} else {
|
||||
TokenStream::new()
|
||||
};
|
||||
@ -201,10 +201,10 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
||||
TokenStream::new()
|
||||
};
|
||||
|
||||
let response_init_fields = api.response.init_fields();
|
||||
let serialize_response_headers = api.response.apply_header_fields();
|
||||
let response_init_fields = api.response.init_fields(&ruma_api);
|
||||
let serialize_response_headers = api.response.apply_header_fields(&ruma_api);
|
||||
|
||||
let body = api.response.to_body();
|
||||
let body = api.response.to_body(&ruma_api);
|
||||
|
||||
let metadata_doc = format!("Metadata for the `{}` API endpoint.", name);
|
||||
let request_doc =
|
||||
|
@ -14,7 +14,6 @@ use super::{
|
||||
response::{ResponseField, ResponseFieldKind},
|
||||
Api, Metadata, Request, Response,
|
||||
};
|
||||
use crate::util;
|
||||
|
||||
mod kw {
|
||||
use syn::custom_keyword;
|
||||
@ -195,7 +194,7 @@ impl Parse for Request {
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Self { attributes, fields, lifetimes, ruma_api_import: util::import_ruma_api() })
|
||||
Ok(Self { attributes, fields, lifetimes })
|
||||
}
|
||||
}
|
||||
|
||||
@ -280,7 +279,7 @@ impl Parse for Response {
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Self { attributes, fields, ruma_api_import: util::import_ruma_api() })
|
||||
Ok(Self { attributes, fields })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{quote, quote_spanned, ToTokens};
|
||||
use quote::{quote, quote_spanned};
|
||||
use syn::{spanned::Spanned, Attribute, Field, Ident, Lifetime};
|
||||
|
||||
use crate::util;
|
||||
@ -26,15 +26,11 @@ pub(crate) struct Request {
|
||||
|
||||
/// The collected lifetime identifiers from the declared fields.
|
||||
pub(super) lifetimes: RequestLifetimes,
|
||||
|
||||
// Guarantee `ruma_api` is available and named something we can refer to.
|
||||
pub(super) ruma_api_import: TokenStream,
|
||||
}
|
||||
|
||||
impl Request {
|
||||
/// Produces code to add necessary HTTP headers to an `http::Request`.
|
||||
pub fn append_header_kvs(&self) -> TokenStream {
|
||||
let ruma_api = &self.ruma_api_import;
|
||||
pub fn append_header_kvs(&self, ruma_api: &TokenStream) -> TokenStream {
|
||||
let http = quote! { #ruma_api::exports::http };
|
||||
|
||||
self.header_fields()
|
||||
@ -71,8 +67,7 @@ impl Request {
|
||||
}
|
||||
|
||||
/// Produces code to extract fields from the HTTP headers in an `http::Request`.
|
||||
pub fn parse_headers_from_request(&self) -> TokenStream {
|
||||
let ruma_api = &self.ruma_api_import;
|
||||
pub fn parse_headers_from_request(&self, ruma_api: &TokenStream) -> TokenStream {
|
||||
let http = quote! { #ruma_api::exports::http };
|
||||
let serde = quote! { #ruma_api::exports::serde };
|
||||
let serde_json = quote! { #ruma_api::exports::serde_json };
|
||||
@ -286,11 +281,8 @@ impl Request {
|
||||
|
||||
quote! { #(#fields,)* }
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Request {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let ruma_api = &self.ruma_api_import;
|
||||
pub(super) fn expand_type_def(&self, ruma_api: &TokenStream) -> TokenStream {
|
||||
let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
|
||||
let serde = quote! { #ruma_api::exports::serde };
|
||||
|
||||
@ -387,7 +379,7 @@ impl ToTokens for Request {
|
||||
TokenStream::new()
|
||||
};
|
||||
|
||||
let request = quote! {
|
||||
quote! {
|
||||
#[derive(Debug, Clone, #ruma_serde::Outgoing, #ruma_serde::_FakeDeriveSerde)]
|
||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||
#[incoming_derive(!Deserialize)]
|
||||
@ -397,9 +389,7 @@ impl ToTokens for Request {
|
||||
#request_body_struct
|
||||
|
||||
#request_query_struct
|
||||
};
|
||||
|
||||
request.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Details of the `response` section of the procedural macro.
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{quote, quote_spanned, ToTokens};
|
||||
use quote::{quote, quote_spanned};
|
||||
use syn::{spanned::Spanned, Attribute, Field, Ident};
|
||||
|
||||
/// The result of processing the `response` section of the macro.
|
||||
@ -11,9 +11,6 @@ pub(crate) struct Response {
|
||||
|
||||
/// The fields of the response.
|
||||
pub fields: Vec<ResponseField>,
|
||||
|
||||
// Guarantee `ruma_api` is available and named something we can refer to.
|
||||
pub ruma_api_import: TokenStream,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
@ -28,8 +25,7 @@ impl Response {
|
||||
}
|
||||
|
||||
/// Produces code for a response struct initializer.
|
||||
pub fn init_fields(&self) -> TokenStream {
|
||||
let ruma_api = &self.ruma_api_import;
|
||||
pub fn init_fields(&self, ruma_api: &TokenStream) -> TokenStream {
|
||||
let http = quote! { #ruma_api::exports::http };
|
||||
|
||||
let mut fields = vec![];
|
||||
@ -99,8 +95,7 @@ impl Response {
|
||||
}
|
||||
|
||||
/// Produces code to add necessary HTTP headers to an `http::Response`.
|
||||
pub fn apply_header_fields(&self) -> TokenStream {
|
||||
let ruma_api = &self.ruma_api_import;
|
||||
pub fn apply_header_fields(&self, ruma_api: &TokenStream) -> TokenStream {
|
||||
let http = quote! { #ruma_api::exports::http };
|
||||
|
||||
let header_calls = self.fields.iter().filter_map(|response_field| {
|
||||
@ -144,8 +139,7 @@ impl Response {
|
||||
}
|
||||
|
||||
/// Produces code to initialize the struct that will be used to create the response body.
|
||||
pub fn to_body(&self) -> TokenStream {
|
||||
let ruma_api = &self.ruma_api_import;
|
||||
pub fn to_body(&self, ruma_api: &TokenStream) -> TokenStream {
|
||||
let serde_json = quote! { #ruma_api::exports::serde_json };
|
||||
|
||||
if let Some(field) = self.newtype_raw_body_field() {
|
||||
@ -193,11 +187,8 @@ impl Response {
|
||||
pub fn newtype_raw_body_field(&self) -> Option<&Field> {
|
||||
self.fields.iter().find_map(ResponseField::as_newtype_raw_body_field)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Response {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let ruma_api = &self.ruma_api_import;
|
||||
pub(super) fn expand_type_def(&self, ruma_api: &TokenStream) -> TokenStream {
|
||||
let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
|
||||
let serde = quote! { #ruma_api::exports::serde };
|
||||
|
||||
@ -230,7 +221,7 @@ impl ToTokens for Response {
|
||||
struct ResponseBody #def
|
||||
};
|
||||
|
||||
let response = quote! {
|
||||
quote! {
|
||||
#[derive(Debug, Clone, #ruma_serde::Outgoing, #ruma_serde::_FakeDeriveSerde)]
|
||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||
#[incoming_derive(!Deserialize)]
|
||||
@ -238,9 +229,7 @@ impl ToTokens for Response {
|
||||
pub struct Response #response_def
|
||||
|
||||
#response_body_struct
|
||||
};
|
||||
|
||||
response.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user