Stop special-casing zero fields in macro code

This commit is contained in:
Jonas Platte 2021-07-09 15:10:22 +02:00
parent a59c43321f
commit 9b2602649f
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
71 changed files with 94 additions and 110 deletions

View File

@ -142,13 +142,6 @@ impl Request {
);
let struct_attributes = &self.attributes;
let request_def = if self.fields.is_empty() {
quote!(;)
} else {
let fields = self.fields.iter().map(|request_field| request_field.field());
quote! { { #(#fields),* } }
};
let request_body_struct =
if let Some(body_field) = self.fields.iter().find(|f| f.is_newtype_body()) {
let field = Field { ident: None, colon_token: None, ..body_field.field().clone() };
@ -232,6 +225,8 @@ impl Request {
};
let lifetimes = self.combine_lifetimes();
let fields = self.fields.iter().map(|request_field| request_field.field());
let outgoing_request_impl = self.expand_outgoing(metadata, error_ty, &lifetimes, ruma_api);
let incoming_request_impl = self.expand_incoming(metadata, error_ty, ruma_api);
@ -241,7 +236,9 @@ impl Request {
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(!Deserialize)]
#( #struct_attributes )*
pub struct Request #lifetimes #request_def
pub struct Request #lifetimes {
#(#fields),*
}
#request_body_struct
#request_query_struct

View File

@ -52,22 +52,11 @@ impl Response {
format!("Data in the response from the `{}` API endpoint.", metadata.name.value());
let struct_attributes = &self.attributes;
let response_def = if self.fields.is_empty() {
quote!(;)
} else {
let fields = self.fields.iter().map(|response_field| response_field.field());
quote! { { #(#fields),* } }
};
let def = if let Some(body_field) = self.fields.iter().find(|f| f.is_newtype_body()) {
let field = Field { ident: None, colon_token: None, ..body_field.field().clone() };
quote! { (#field); }
} else if self.has_body_fields() {
let fields = self.fields.iter().filter(|f| f.is_body());
let fields = fields.map(ResponseField::field);
let fields = self.fields.iter().filter(|f| f.is_body()).map(ResponseField::field);
quote! { { #(#fields),* } }
} else {
quote! { {} }
@ -91,6 +80,8 @@ impl Response {
quote! { #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] }
};
let fields = self.fields.iter().map(|response_field| response_field.field());
let outgoing_response_impl = self.expand_outgoing(ruma_api);
let incoming_response_impl = self.expand_incoming(error_ty, ruma_api);
@ -100,7 +91,9 @@ impl Response {
#non_exhaustive_attr
#[incoming_derive(!Deserialize)]
#( #struct_attributes )*
pub struct Response #response_def
pub struct Response {
#(#fields),*
}
#response_body_struct

View File

@ -92,7 +92,7 @@ impl IncomingRequest {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -33,6 +33,6 @@ impl<'a> Request<'a> {
impl Response {
/// Create an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -33,6 +33,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -42,6 +42,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -48,6 +48,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -49,6 +49,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -27,7 +27,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -38,6 +38,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -35,6 +35,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -44,6 +44,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -34,6 +34,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -40,7 +40,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -40,6 +40,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -28,7 +28,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -50,6 +50,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -59,6 +59,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -29,7 +29,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -64,6 +64,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -41,6 +41,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -40,6 +40,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -28,7 +28,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -40,6 +40,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -40,6 +40,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -40,6 +40,6 @@ impl Request {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -54,6 +54,6 @@ impl Request<'_> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -28,7 +28,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -42,6 +42,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -35,6 +35,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -50,7 +50,7 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -28,7 +28,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -42,6 +42,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -35,6 +35,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -38,6 +38,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -43,6 +43,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -70,7 +70,7 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -39,6 +39,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -44,6 +44,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -29,7 +29,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -27,7 +27,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -28,7 +28,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -43,7 +43,7 @@ impl Request {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -77,6 +77,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -48,6 +48,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -57,6 +57,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -47,6 +47,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -44,6 +44,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -46,6 +46,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -36,7 +36,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -24,13 +24,13 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -24,13 +24,13 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -48,6 +48,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -43,6 +43,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -30,7 +30,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -50,7 +50,7 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -46,7 +46,7 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -38,7 +38,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -32,7 +32,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -32,7 +32,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -173,23 +173,15 @@ pub fn expand_event_content(
};
let redaction_struct_fields = kept_redacted_fields.iter().flat_map(|f| &f.ident);
// redacted_fields allows one to declare an empty redacted event without braces,
// otherwise `RedactedWhateverEventContent {}` is needed.
// The redacted_return is used in `EventContent::redacted` which only returns
// zero sized types (unit structs).
let (redacted_fields, redacted_return) = if kept_redacted_fields.is_empty() {
(quote! { ; }, quote! { Ok(#redacted_ident {}) })
// Used in `EventContent::redacted` which only returns zero sized types (unit structs).
let redacted_return = if kept_redacted_fields.is_empty() {
quote! { Ok(#redacted_ident {}) }
} else {
(
quote! {
{ #( #kept_redacted_fields, )* }
},
quote! {
Err(#serde::de::Error::custom(
format!("this redacted event has fields that cannot be constructed")
))
},
)
}
};
let has_deserialize_fields = if kept_redacted_fields.is_empty() {
@ -210,7 +202,7 @@ pub fn expand_event_content(
impl #redacted_ident {
#[doc = #doc]
pub fn new() -> Self {
Self
Self {}
}
}
}
@ -252,7 +244,9 @@ pub fn expand_event_content(
#[doc = #doc]
#[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct #redacted_ident #redacted_fields
pub struct #redacted_ident {
#( #kept_redacted_fields, )*
}
#initializer

View File

@ -27,7 +27,7 @@ pub struct DummyToDeviceEventContent;
impl DummyToDeviceEventContent {
/// Create a new `DummyToDeviceEventContent`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -26,7 +26,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -27,7 +27,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -27,7 +27,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -45,7 +45,7 @@ impl<'a> Request<'a> {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -52,6 +52,6 @@ impl<'a> Request<'a> {
impl Response {
/// Creates a new `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -25,7 +25,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -22,13 +22,13 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -33,6 +33,6 @@ impl Request {
impl Response {
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
Self {}
}
}

View File

@ -29,7 +29,7 @@ ruma_api! {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
Self {}
}
}