api: Slighty reduce the amount of macro-generated code
By moving format(_args)! usage into a regular fn and calling that from the proc-macro-generated code.
This commit is contained in:
parent
a4a14aa9dc
commit
a6e23d731e
@ -399,9 +399,23 @@ pub enum AuthScheme {
|
|||||||
QueryOnlyAccessToken,
|
QueryOnlyAccessToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function needs to be public, yet hidden, as it is used the code generated by `ruma_api!`.
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn make_endpoint_url(
|
||||||
|
base_url: &str,
|
||||||
|
path: fmt::Arguments<'_>,
|
||||||
|
query_string: Option<&str>,
|
||||||
|
) -> String {
|
||||||
|
let base = base_url.strip_suffix('/').unwrap_or(base_url);
|
||||||
|
match query_string {
|
||||||
|
Some(query) => format!("{base}{path}?{query}"),
|
||||||
|
None => format!("{base}{path}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This function helps picks the right path (or an error) from a set of matrix versions.
|
// This function helps picks the right path (or an error) from a set of matrix versions.
|
||||||
//
|
//
|
||||||
// This function needs to be public, yet hidden, as all `try_into_http_request`s would be using it.
|
// This function needs to be public, yet hidden, as it is used the code generated by `ruma_api!`.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn select_path<'a>(
|
pub fn select_path<'a>(
|
||||||
versions: &'_ [MatrixVersion],
|
versions: &'_ [MatrixVersion],
|
||||||
|
@ -60,9 +60,8 @@ impl Request {
|
|||||||
let request_query = RequestQuery(self.#field_name);
|
let request_query = RequestQuery(self.#field_name);
|
||||||
assert_trait_impl(&request_query.0);
|
assert_trait_impl(&request_query.0);
|
||||||
|
|
||||||
format_args!(
|
::std::option::Option::Some(
|
||||||
"?{}",
|
&#ruma_common::serde::urlencoded::to_string(request_query)?
|
||||||
#ruma_common::serde::urlencoded::to_string(request_query)?
|
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
} else if self.has_query_fields() {
|
} else if self.has_query_fields() {
|
||||||
@ -76,13 +75,12 @@ impl Request {
|
|||||||
#request_query_init_fields
|
#request_query_init_fields
|
||||||
};
|
};
|
||||||
|
|
||||||
format_args!(
|
::std::option::Option::Some(
|
||||||
"?{}",
|
&#ruma_common::serde::urlencoded::to_string(request_query)?
|
||||||
#ruma_common::serde::urlencoded::to_string(request_query)?
|
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
} else {
|
} else {
|
||||||
quote! { "" }
|
quote! { ::std::option::Option::None }
|
||||||
};
|
};
|
||||||
|
|
||||||
// If there are no body fields, the request body will be empty (not `{}`), so the
|
// If there are no body fields, the request body will be empty (not `{}`), so the
|
||||||
@ -195,10 +193,15 @@ impl Request {
|
|||||||
|
|
||||||
let mut req_builder = #http::Request::builder()
|
let mut req_builder = #http::Request::builder()
|
||||||
.method(#http::Method::#method)
|
.method(#http::Method::#method)
|
||||||
.uri(::std::format!(
|
.uri(#ruma_common::api::make_endpoint_url(
|
||||||
"{}{}{}",
|
base_url,
|
||||||
base_url.strip_suffix('/').unwrap_or(base_url),
|
#ruma_common::api::select_path(
|
||||||
#ruma_common::api::select_path(considering_versions, &metadata, #unstable_path, #r0_path, #stable_path)?,
|
considering_versions,
|
||||||
|
&metadata,
|
||||||
|
#unstable_path,
|
||||||
|
#r0_path,
|
||||||
|
#stable_path,
|
||||||
|
)?,
|
||||||
#request_query_string,
|
#request_query_string,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user