Fix a few bugs in ruma_api_macros

This commit is contained in:
Jonas Platte 2019-11-16 15:21:18 +01:00
parent 2b9742b99e
commit 18ed83ef76
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 27 additions and 15 deletions

View File

@ -173,11 +173,17 @@ impl From<Vec<Field>> for Request {
RequestField::new(field_kind.unwrap_or(RequestFieldKind::Body), field, header) RequestField::new(field_kind.unwrap_or(RequestFieldKind::Body), field, header)
}).collect(); }).collect();
if fields.len() > 1 { let num_body_fields = fields.iter().filter(|f| f.is_body()).count();
let num_newtype_body_fields = fields.iter().filter(|f| f.is_newtype_body()).count();
assert!(
num_newtype_body_fields <= 1,
"ruma_api! request can only have one newtype body field"
);
if num_newtype_body_fields == 1 {
assert!( assert!(
!fields.iter().any(|field| field.is_newtype_body()), num_body_fields == 0,
"ruma_api! newtype body has to be the only response field" "ruma_api! request can't have both regular body fields and a newtype body field"
) );
} }
Self { fields } Self { fields }
@ -342,7 +348,7 @@ impl RequestField {
self.kind() == RequestFieldKind::Query self.kind() == RequestFieldKind::Query
} }
/// Return the contained field if this response field is a body kind. /// Return the contained field if this request field is a body kind.
fn as_body_field(&self) -> Option<&Field> { fn as_body_field(&self) -> Option<&Field> {
if let RequestField::Body(field) = self { if let RequestField::Body(field) = self {
Some(field) Some(field)
@ -351,7 +357,7 @@ impl RequestField {
} }
} }
/// Return the contained field if this response field is a path kind. /// Return the contained field if this request field is a path kind.
fn as_path_field(&self) -> Option<&Field> { fn as_path_field(&self) -> Option<&Field> {
if let RequestField::Path(field) = self { if let RequestField::Path(field) = self {
Some(field) Some(field)
@ -360,7 +366,7 @@ impl RequestField {
} }
} }
/// Return the contained field if this response field is a query kind. /// Return the contained field if this request field is a query kind.
fn as_query_field(&self) -> Option<&Field> { fn as_query_field(&self) -> Option<&Field> {
if let RequestField::Query(field) = self { if let RequestField::Query(field) = self {
Some(field) Some(field)

View File

@ -9,7 +9,7 @@ use crate::api::{
strip_serde_attrs, strip_serde_attrs,
}; };
/// The result of processing the `request` section of the macro. /// The result of processing the `response` section of the macro.
pub struct Response { pub struct Response {
/// The fields of the response. /// The fields of the response.
fields: Vec<ResponseField>, fields: Vec<ResponseField>,
@ -31,7 +31,7 @@ impl Response {
self.fields.iter().any(|field| field.is_header()) self.fields.iter().any(|field| field.is_header())
} }
/// Produces code for a request struct initializer. /// Produces code for a response struct initializer.
pub fn init_fields(&self) -> TokenStream { pub fn init_fields(&self) -> TokenStream {
let fields = self let fields = self
.fields .fields
@ -81,7 +81,7 @@ impl Response {
} }
} }
/// Gets the newtype body field, if this request has one. /// Gets the newtype body field, if this response has one.
pub fn newtype_body_field(&self) -> Option<&Field> { pub fn newtype_body_field(&self) -> Option<&Field> {
for response_field in self.fields.iter() { for response_field in self.fields.iter() {
match *response_field { match *response_field {
@ -126,7 +126,7 @@ impl From<Vec<Field>> for Response {
Meta::NameValue(MetaNameValue { name, value }) => { Meta::NameValue(MetaNameValue { name, value }) => {
assert!( assert!(
name == "header", name == "header",
"ruma_api! name/value pair attribute on requests must be: header" "ruma_api! name/value pair attribute on responses must be: header"
); );
assert!( assert!(
field_kind.is_none(), field_kind.is_none(),
@ -151,11 +151,17 @@ impl From<Vec<Field>> for Response {
}) })
.collect(); .collect();
if fields.len() > 1 { let num_body_fields = fields.iter().filter(|f| f.is_body()).count();
let num_newtype_body_fields = fields.iter().filter(|f| f.is_newtype_body()).count();
assert!(
num_newtype_body_fields <= 1,
"ruma_api! response can only have one newtype body field"
);
if num_newtype_body_fields == 1 {
assert!( assert!(
!fields.iter().any(|field| field.is_newtype_body()), num_body_fields == 0,
"ruma_api! newtype body has to be the only response field" "ruma_api! response can't have both regular body fields and a newtype body field"
) );
} }
Self { fields } Self { fields }