diff --git a/src/api/response.rs b/src/api/response.rs index 5a203e13..abe0905e 100644 --- a/src/api/response.rs +++ b/src/api/response.rs @@ -164,7 +164,23 @@ impl ToTokens for Response { 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! { /// Data in the response body. #[derive(Debug, Deserialize)] @@ -175,11 +191,13 @@ impl ToTokens for Response { for response_field in self.fields.iter() { 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("}"); diff --git a/src/lib.rs b/src/lib.rs index 74738261..9ce90231 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] #![feature(proc_macro)] diff --git a/tests/ruma_api_macros.rs b/tests/ruma_api_macros.rs index fc7c4913..cc93423e 100644 --- a/tests/ruma_api_macros.rs +++ b/tests/ruma_api_macros.rs @@ -10,24 +10,45 @@ extern crate serde_json; extern crate serde_urlencoded; extern crate url; -pub mod get_supported_versions { +pub mod some_endpoint { + use hyper::header::ContentType; use ruma_api_macros::ruma_api; ruma_api! { metadata { - description: "Get the versions of the client-server API supported by this homeserver.", - method: Method::Get, - name: "api_versions", - path: "/_matrix/client/versions", + description: "Does something.", + method: Method::Get, // A `hyper::Method` value. No need to import the name. + name: "some_endpoint", + path: "/_matrix/some/endpoint/:baz", 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 { - /// A list of Matrix client API protocol versions supported by the homeserver. - pub versions: Vec, + // This value will be extracted from the "Content-Type" HTTP header. + #[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, } } }