Improve test coverage, fix a misplaced comma bug, implement missing newtype body support for responses.

This commit is contained in:
Jimmy Cuadra 2017-07-01 16:24:44 -07:00
parent 84562c4260
commit b292a3e776
3 changed files with 54 additions and 14 deletions

View File

@ -164,7 +164,23 @@ impl ToTokens for Response {
tokens.append("}"); tokens.append("}");
} }
if self.has_body_fields() { if let Some(newtype_body_field) = self.newtype_body_field() {
let mut field = newtype_body_field.clone();
field.ident = None;
tokens.append(quote! {
/// Data in the response body.
#[derive(Debug, Deserialize)]
struct ResponseBody
});
tokens.append("(");
field.to_tokens(&mut tokens);
tokens.append(");");
} else if self.has_body_fields() {
tokens.append(quote! { tokens.append(quote! {
/// Data in the response body. /// Data in the response body.
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -175,11 +191,13 @@ impl ToTokens for Response {
for response_field in self.fields.iter() { for response_field in self.fields.iter() {
match *response_field { match *response_field {
ResponseField::Body(ref field) => field.to_tokens(&mut tokens), ResponseField::Body(ref field) => {
field.to_tokens(&mut tokens);
tokens.append(",");
}
_ => {} _ => {}
} }
tokens.append(",");
} }
tokens.append("}"); tokens.append("}");

View File

@ -1,4 +1,5 @@
//! Crate `ruma-api-macros` provides a procedural macro for easily generating `ruma-api` endpoints. //! Crate `ruma-api-macros` provides a procedural macro for easily generating
//! [ruma-api](https://github.com/ruma/ruma-api)-compatible endpoints.
#![deny(missing_debug_implementations)] #![deny(missing_debug_implementations)]
#![feature(proc_macro)] #![feature(proc_macro)]

View File

@ -10,24 +10,45 @@ extern crate serde_json;
extern crate serde_urlencoded; extern crate serde_urlencoded;
extern crate url; extern crate url;
pub mod get_supported_versions { pub mod some_endpoint {
use hyper::header::ContentType;
use ruma_api_macros::ruma_api; use ruma_api_macros::ruma_api;
ruma_api! { ruma_api! {
metadata { metadata {
description: "Get the versions of the client-server API supported by this homeserver.", description: "Does something.",
method: Method::Get, method: Method::Get, // A `hyper::Method` value. No need to import the name.
name: "api_versions", name: "some_endpoint",
path: "/_matrix/client/versions", path: "/_matrix/some/endpoint/:baz",
rate_limited: false, rate_limited: false,
requires_authentication: true, requires_authentication: false,
} }
request {} 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)]
pub content_type: ContentType,
// 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 { response {
/// A list of Matrix client API protocol versions supported by the homeserver. // This value will be extracted from the "Content-Type" HTTP header.
pub versions: Vec<String>, #[ruma_api(header)]
pub content_type: ContentType,
// With no attribute on the field, it will be extracted from the body of the response.
pub value: String,
} }
} }
} }