api-macros: Replace ToTokens implementations with inherent methods

This commit is contained in:
Jonas Platte 2021-04-05 12:57:50 +02:00
parent 2e0f787ccd
commit 536a8aea1b
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
4 changed files with 22 additions and 44 deletions

View File

@ -69,8 +69,8 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
}) })
.collect(); .collect();
let request_type = &api.request; let request_type = api.request.expand_type_def(&ruma_api);
let response_type = &api.response; let response_type = api.response.expand_type_def(&ruma_api);
let incoming_request_type = let incoming_request_type =
if api.request.contains_lifetimes() { quote!(IncomingRequest) } else { quote!(Request) }; 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() 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 { for auth in &api.metadata.authentication {
if auth.value == "AccessToken" { if auth.value == "AccessToken" {
let attrs = &auth.attrs; 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() { 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 { } else {
TokenStream::new() TokenStream::new()
}; };
@ -201,10 +201,10 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
TokenStream::new() TokenStream::new()
}; };
let response_init_fields = api.response.init_fields(); let response_init_fields = api.response.init_fields(&ruma_api);
let serialize_response_headers = api.response.apply_header_fields(); 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 metadata_doc = format!("Metadata for the `{}` API endpoint.", name);
let request_doc = let request_doc =

View File

@ -14,7 +14,6 @@ use super::{
response::{ResponseField, ResponseFieldKind}, response::{ResponseField, ResponseFieldKind},
Api, Metadata, Request, Response, Api, Metadata, Request, Response,
}; };
use crate::util;
mod kw { mod kw {
use syn::custom_keyword; 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 })
} }
} }

View File

@ -3,7 +3,7 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens}; use quote::{quote, quote_spanned};
use syn::{spanned::Spanned, Attribute, Field, Ident, Lifetime}; use syn::{spanned::Spanned, Attribute, Field, Ident, Lifetime};
use crate::util; use crate::util;
@ -26,15 +26,11 @@ pub(crate) struct Request {
/// The collected lifetime identifiers from the declared fields. /// The collected lifetime identifiers from the declared fields.
pub(super) lifetimes: RequestLifetimes, pub(super) lifetimes: RequestLifetimes,
// Guarantee `ruma_api` is available and named something we can refer to.
pub(super) ruma_api_import: TokenStream,
} }
impl Request { impl Request {
/// Produces code to add necessary HTTP headers to an `http::Request`. /// Produces code to add necessary HTTP headers to an `http::Request`.
pub fn append_header_kvs(&self) -> TokenStream { pub fn append_header_kvs(&self, ruma_api: &TokenStream) -> TokenStream {
let ruma_api = &self.ruma_api_import;
let http = quote! { #ruma_api::exports::http }; let http = quote! { #ruma_api::exports::http };
self.header_fields() self.header_fields()
@ -71,8 +67,7 @@ impl Request {
} }
/// Produces code to extract fields from the HTTP headers in an `http::Request`. /// Produces code to extract fields from the HTTP headers in an `http::Request`.
pub fn parse_headers_from_request(&self) -> TokenStream { pub fn parse_headers_from_request(&self, ruma_api: &TokenStream) -> TokenStream {
let ruma_api = &self.ruma_api_import;
let http = quote! { #ruma_api::exports::http }; let http = quote! { #ruma_api::exports::http };
let serde = quote! { #ruma_api::exports::serde }; let serde = quote! { #ruma_api::exports::serde };
let serde_json = quote! { #ruma_api::exports::serde_json }; let serde_json = quote! { #ruma_api::exports::serde_json };
@ -286,11 +281,8 @@ impl Request {
quote! { #(#fields,)* } quote! { #(#fields,)* }
} }
}
impl ToTokens for Request { pub(super) fn expand_type_def(&self, ruma_api: &TokenStream) -> TokenStream {
fn to_tokens(&self, tokens: &mut TokenStream) {
let ruma_api = &self.ruma_api_import;
let ruma_serde = quote! { #ruma_api::exports::ruma_serde }; let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
let serde = quote! { #ruma_api::exports::serde }; let serde = quote! { #ruma_api::exports::serde };
@ -387,7 +379,7 @@ impl ToTokens for Request {
TokenStream::new() TokenStream::new()
}; };
let request = quote! { quote! {
#[derive(Debug, Clone, #ruma_serde::Outgoing, #ruma_serde::_FakeDeriveSerde)] #[derive(Debug, Clone, #ruma_serde::Outgoing, #ruma_serde::_FakeDeriveSerde)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(!Deserialize)] #[incoming_derive(!Deserialize)]
@ -397,9 +389,7 @@ impl ToTokens for Request {
#request_body_struct #request_body_struct
#request_query_struct #request_query_struct
}; }
request.to_tokens(tokens);
} }
} }

View File

@ -1,7 +1,7 @@
//! Details of the `response` section of the procedural macro. //! Details of the `response` section of the procedural macro.
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens}; use quote::{quote, quote_spanned};
use syn::{spanned::Spanned, Attribute, Field, Ident}; use syn::{spanned::Spanned, Attribute, Field, Ident};
/// The result of processing the `response` section of the macro. /// The result of processing the `response` section of the macro.
@ -11,9 +11,6 @@ pub(crate) struct Response {
/// The fields of the response. /// The fields of the response.
pub fields: Vec<ResponseField>, pub fields: Vec<ResponseField>,
// Guarantee `ruma_api` is available and named something we can refer to.
pub ruma_api_import: TokenStream,
} }
impl Response { impl Response {
@ -28,8 +25,7 @@ impl Response {
} }
/// Produces code for a response struct initializer. /// Produces code for a response struct initializer.
pub fn init_fields(&self) -> TokenStream { pub fn init_fields(&self, ruma_api: &TokenStream) -> TokenStream {
let ruma_api = &self.ruma_api_import;
let http = quote! { #ruma_api::exports::http }; let http = quote! { #ruma_api::exports::http };
let mut fields = vec![]; let mut fields = vec![];
@ -99,8 +95,7 @@ impl Response {
} }
/// Produces code to add necessary HTTP headers to an `http::Response`. /// Produces code to add necessary HTTP headers to an `http::Response`.
pub fn apply_header_fields(&self) -> TokenStream { pub fn apply_header_fields(&self, ruma_api: &TokenStream) -> TokenStream {
let ruma_api = &self.ruma_api_import;
let http = quote! { #ruma_api::exports::http }; let http = quote! { #ruma_api::exports::http };
let header_calls = self.fields.iter().filter_map(|response_field| { 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. /// Produces code to initialize the struct that will be used to create the response body.
pub fn to_body(&self) -> TokenStream { pub fn to_body(&self, ruma_api: &TokenStream) -> TokenStream {
let ruma_api = &self.ruma_api_import;
let serde_json = quote! { #ruma_api::exports::serde_json }; let serde_json = quote! { #ruma_api::exports::serde_json };
if let Some(field) = self.newtype_raw_body_field() { if let Some(field) = self.newtype_raw_body_field() {
@ -193,11 +187,8 @@ impl Response {
pub fn newtype_raw_body_field(&self) -> Option<&Field> { pub fn newtype_raw_body_field(&self) -> Option<&Field> {
self.fields.iter().find_map(ResponseField::as_newtype_raw_body_field) self.fields.iter().find_map(ResponseField::as_newtype_raw_body_field)
} }
}
impl ToTokens for Response { pub(super) fn expand_type_def(&self, ruma_api: &TokenStream) -> TokenStream {
fn to_tokens(&self, tokens: &mut TokenStream) {
let ruma_api = &self.ruma_api_import;
let ruma_serde = quote! { #ruma_api::exports::ruma_serde }; let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
let serde = quote! { #ruma_api::exports::serde }; let serde = quote! { #ruma_api::exports::serde };
@ -230,7 +221,7 @@ impl ToTokens for Response {
struct ResponseBody #def struct ResponseBody #def
}; };
let response = quote! { quote! {
#[derive(Debug, Clone, #ruma_serde::Outgoing, #ruma_serde::_FakeDeriveSerde)] #[derive(Debug, Clone, #ruma_serde::Outgoing, #ruma_serde::_FakeDeriveSerde)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(!Deserialize)] #[incoming_derive(!Deserialize)]
@ -238,9 +229,7 @@ impl ToTokens for Response {
pub struct Response #response_def pub struct Response #response_def
#response_body_struct #response_body_struct
}; }
response.to_tokens(tokens);
} }
} }