macros: Slightly simplify generated API trait impls
This commit is contained in:
parent
e3a8b6b81f
commit
4e7eeb8aa4
@ -70,7 +70,7 @@ pub mod v3 {
|
|||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
impl<'a> ruma_common::api::OutgoingRequest for Request<'a> {
|
impl<'a> ruma_common::api::OutgoingRequest for Request<'a> {
|
||||||
type EndpointError = crate::Error;
|
type EndpointError = crate::Error;
|
||||||
type IncomingResponse = <Response as ruma_common::serde::Outgoing>::Incoming;
|
type IncomingResponse = Response;
|
||||||
|
|
||||||
const METADATA: ruma_common::api::Metadata = METADATA;
|
const METADATA: ruma_common::api::Metadata = METADATA;
|
||||||
|
|
||||||
|
@ -64,9 +64,14 @@ impl Request {
|
|||||||
quote! { request_query },
|
quote! { request_query },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let request_query_ty = if self.lifetimes.query.is_empty() {
|
||||||
|
quote! { RequestQuery }
|
||||||
|
} else {
|
||||||
|
quote! { IncomingRequestQuery }
|
||||||
|
};
|
||||||
|
|
||||||
let parse = quote! {
|
let parse = quote! {
|
||||||
let request_query: <RequestQuery as #ruma_common::serde::Outgoing>::Incoming =
|
let request_query: #request_query_ty = #ruma_common::serde::urlencoded::from_str(
|
||||||
#ruma_common::serde::urlencoded::from_str(
|
|
||||||
&request.uri().query().unwrap_or("")
|
&request.uri().query().unwrap_or("")
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
@ -144,17 +149,14 @@ impl Request {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let extract_body = self.has_body_fields().then(|| {
|
let extract_body = self.has_body_fields().then(|| {
|
||||||
let body_lifetimes = (!self.lifetimes.body.is_empty()).then(|| {
|
let request_body_ty = if self.lifetimes.body.is_empty() {
|
||||||
// duplicate the anonymous lifetime as many times as needed
|
quote! { RequestBody }
|
||||||
let lifetimes = std::iter::repeat(quote! { '_ }).take(self.lifetimes.body.len());
|
} else {
|
||||||
quote! { < #( #lifetimes, )* > }
|
quote! { IncomingRequestBody }
|
||||||
});
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
let request_body: <
|
let request_body: #request_body_ty = {
|
||||||
RequestBody #body_lifetimes
|
|
||||||
as #ruma_common::serde::Outgoing
|
|
||||||
>::Incoming = {
|
|
||||||
let body = ::std::convert::AsRef::<[::std::primitive::u8]>::as_ref(
|
let body = ::std::convert::AsRef::<[::std::primitive::u8]>::as_ref(
|
||||||
request.body(),
|
request.body(),
|
||||||
);
|
);
|
||||||
|
@ -186,7 +186,7 @@ impl Request {
|
|||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
impl #impl_generics #ruma_common::api::OutgoingRequest for Request #ty_generics #where_clause {
|
impl #impl_generics #ruma_common::api::OutgoingRequest for Request #ty_generics #where_clause {
|
||||||
type EndpointError = #error_ty;
|
type EndpointError = #error_ty;
|
||||||
type IncomingResponse = <Response as #ruma_common::serde::Outgoing>::Incoming;
|
type IncomingResponse = Response;
|
||||||
|
|
||||||
const METADATA: #ruma_common::api::Metadata = self::METADATA;
|
const METADATA: #ruma_common::api::Metadata = self::METADATA;
|
||||||
|
|
||||||
|
@ -17,10 +17,7 @@ impl Response {
|
|||||||
|
|
||||||
let typed_response_body_decl = self.has_body_fields().then(|| {
|
let typed_response_body_decl = self.has_body_fields().then(|| {
|
||||||
quote! {
|
quote! {
|
||||||
let response_body: <
|
let response_body: ResponseBody = {
|
||||||
ResponseBody
|
|
||||||
as #ruma_common::serde::Outgoing
|
|
||||||
>::Incoming = {
|
|
||||||
let body = ::std::convert::AsRef::<[::std::primitive::u8]>::as_ref(
|
let body = ::std::convert::AsRef::<[::std::primitive::u8]>::as_ref(
|
||||||
response.body(),
|
response.body(),
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user