api: Rename newtype raw body => raw body

This commit is contained in:
Jonas Platte 2021-08-16 21:11:46 +02:00
parent 7ac3c369cc
commit 42fda7c89f
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
8 changed files with 36 additions and 35 deletions

View File

@ -39,7 +39,7 @@ pub fn expand_derive_request(input: DeriveInput) -> syn::Result<TokenStream> {
RequestField::Header(..) => collect_lifetime_idents(&mut lifetimes.header, ty), RequestField::Header(..) => collect_lifetime_idents(&mut lifetimes.header, ty),
RequestField::Body(_) => collect_lifetime_idents(&mut lifetimes.body, ty), RequestField::Body(_) => collect_lifetime_idents(&mut lifetimes.body, ty),
RequestField::NewtypeBody(_) => collect_lifetime_idents(&mut lifetimes.body, ty), RequestField::NewtypeBody(_) => collect_lifetime_idents(&mut lifetimes.body, ty),
RequestField::NewtypeRawBody(_) => collect_lifetime_idents(&mut lifetimes.body, ty), RequestField::RawBody(_) => collect_lifetime_idents(&mut lifetimes.body, ty),
RequestField::Path(_) => collect_lifetime_idents(&mut lifetimes.path, ty), RequestField::Path(_) => collect_lifetime_idents(&mut lifetimes.path, ty),
RequestField::Query(_) => collect_lifetime_idents(&mut lifetimes.query, ty), RequestField::Query(_) => collect_lifetime_idents(&mut lifetimes.query, ty),
RequestField::QueryMap(_) => collect_lifetime_idents(&mut lifetimes.query, ty), RequestField::QueryMap(_) => collect_lifetime_idents(&mut lifetimes.query, ty),
@ -158,8 +158,8 @@ impl Request {
self.fields.iter().find_map(RequestField::as_newtype_body_field) self.fields.iter().find_map(RequestField::as_newtype_body_field)
} }
fn newtype_raw_body_field(&self) -> Option<&Field> { fn raw_body_field(&self) -> Option<&Field> {
self.fields.iter().find_map(RequestField::as_newtype_raw_body_field) self.fields.iter().find_map(RequestField::as_raw_body_field)
} }
fn query_map_field(&self) -> Option<&Field> { fn query_map_field(&self) -> Option<&Field> {
@ -246,7 +246,7 @@ impl Request {
// TODO: highlight problematic fields // TODO: highlight problematic fields
let newtype_body_fields = self.fields.iter().filter(|field| { let newtype_body_fields = self.fields.iter().filter(|field| {
matches!(field, RequestField::NewtypeBody(_) | RequestField::NewtypeRawBody(_)) matches!(field, RequestField::NewtypeBody(_) | RequestField::RawBody(_))
}); });
let has_newtype_body_field = match newtype_body_fields.count() { let has_newtype_body_field = match newtype_body_fields.count() {
@ -321,7 +321,7 @@ enum RequestField {
NewtypeBody(Field), NewtypeBody(Field),
/// Arbitrary bytes in the body of the request. /// Arbitrary bytes in the body of the request.
NewtypeRawBody(Field), RawBody(Field),
/// Data that appears in the URL path. /// Data that appears in the URL path.
Path(Field), Path(Field),
@ -342,7 +342,7 @@ impl RequestField {
RequestField::Header(field, header.expect("missing header name")) RequestField::Header(field, header.expect("missing header name"))
} }
RequestFieldKind::NewtypeBody => RequestField::NewtypeBody(field), RequestFieldKind::NewtypeBody => RequestField::NewtypeBody(field),
RequestFieldKind::NewtypeRawBody => RequestField::NewtypeRawBody(field), RequestFieldKind::RawBody => RequestField::RawBody(field),
RequestFieldKind::Path => RequestField::Path(field), RequestFieldKind::Path => RequestField::Path(field),
RequestFieldKind::Query => RequestField::Query(field), RequestFieldKind::Query => RequestField::Query(field),
RequestFieldKind::QueryMap => RequestField::QueryMap(field), RequestFieldKind::QueryMap => RequestField::QueryMap(field),
@ -360,8 +360,8 @@ impl RequestField {
} }
/// Return the contained field if this request field is a raw body kind. /// Return the contained field if this request field is a raw body kind.
pub fn as_newtype_raw_body_field(&self) -> Option<&Field> { pub fn as_raw_body_field(&self) -> Option<&Field> {
self.field_of_kind(RequestFieldKind::NewtypeRawBody) self.field_of_kind(RequestFieldKind::RawBody)
} }
/// Return the contained field if this request field is a query kind. /// Return the contained field if this request field is a query kind.
@ -380,7 +380,7 @@ impl RequestField {
RequestField::Body(field) RequestField::Body(field)
| RequestField::Header(field, _) | RequestField::Header(field, _)
| RequestField::NewtypeBody(field) | RequestField::NewtypeBody(field)
| RequestField::NewtypeRawBody(field) | RequestField::RawBody(field)
| RequestField::Path(field) | RequestField::Path(field)
| RequestField::Query(field) | RequestField::Query(field)
| RequestField::QueryMap(field) => field, | RequestField::QueryMap(field) => field,
@ -393,7 +393,7 @@ impl RequestField {
(RequestField::Body(field), RequestFieldKind::Body) (RequestField::Body(field), RequestFieldKind::Body)
| (RequestField::Header(field, _), RequestFieldKind::Header) | (RequestField::Header(field, _), RequestFieldKind::Header)
| (RequestField::NewtypeBody(field), RequestFieldKind::NewtypeBody) | (RequestField::NewtypeBody(field), RequestFieldKind::NewtypeBody)
| (RequestField::NewtypeRawBody(field), RequestFieldKind::NewtypeRawBody) | (RequestField::RawBody(field), RequestFieldKind::RawBody)
| (RequestField::Path(field), RequestFieldKind::Path) | (RequestField::Path(field), RequestFieldKind::Path)
| (RequestField::Query(field), RequestFieldKind::Query) | (RequestField::Query(field), RequestFieldKind::Query)
| (RequestField::QueryMap(field), RequestFieldKind::QueryMap) => Some(field), | (RequestField::QueryMap(field), RequestFieldKind::QueryMap) => Some(field),
@ -428,7 +428,7 @@ impl TryFrom<Field> for RequestField {
field_kind = Some(match meta { field_kind = Some(match meta {
Meta::Word(ident) => match &ident.to_string()[..] { Meta::Word(ident) => match &ident.to_string()[..] {
"body" => RequestFieldKind::NewtypeBody, "body" => RequestFieldKind::NewtypeBody,
"raw_body" => RequestFieldKind::NewtypeRawBody, "raw_body" => RequestFieldKind::RawBody,
"path" => RequestFieldKind::Path, "path" => RequestFieldKind::Path,
"query" => RequestFieldKind::Query, "query" => RequestFieldKind::Query,
"query_map" => RequestFieldKind::QueryMap, "query_map" => RequestFieldKind::QueryMap,
@ -476,7 +476,7 @@ enum RequestFieldKind {
Body, Body,
Header, Header,
NewtypeBody, NewtypeBody,
NewtypeRawBody, RawBody,
Path, Path,
Query, Query,
QueryMap, QueryMap,

View File

@ -202,7 +202,7 @@ impl Request {
}; };
(parse, quote! { #field_name, }) (parse, quote! { #field_name, })
} else if let Some(field) = self.newtype_raw_body_field() { } else if let Some(field) = self.raw_body_field() {
let field_name = field.ident.as_ref().expect("expected field to have an identifier"); let field_name = field.ident.as_ref().expect("expected field to have an identifier");
let parse = quote! { let parse = quote! {
let #field_name = let #field_name =

View File

@ -153,7 +153,7 @@ impl Request {
}; };
header_kvs.extend(hdr_kv); header_kvs.extend(hdr_kv);
let request_body = if let Some(field) = self.newtype_raw_body_field() { let request_body = if let Some(field) = self.raw_body_field() {
let field_name = field.ident.as_ref().expect("expected field to have an identifier"); let field_name = field.ident.as_ref().expect("expected field to have an identifier");
quote! { #ruma_serde::slice_to_buf(&self.#field_name) } quote! { #ruma_serde::slice_to_buf(&self.#field_name) }
} else if self.has_body_fields() || self.newtype_body_field().is_some() { } else if self.has_body_fields() || self.newtype_body_field().is_some() {

View File

@ -74,8 +74,8 @@ impl Response {
} }
/// Returns the body field. /// Returns the body field.
fn newtype_raw_body_field(&self) -> Option<&Field> { fn raw_body_field(&self) -> Option<&Field> {
self.fields.iter().find_map(ResponseField::as_newtype_raw_body_field) self.fields.iter().find_map(ResponseField::as_raw_body_field)
} }
/// Whether or not this request has any data in the URL path. /// Whether or not this request has any data in the URL path.
@ -90,7 +90,7 @@ impl Response {
let serde = quote! { #ruma_api::exports::serde }; let serde = quote! { #ruma_api::exports::serde };
let response_body_struct = let response_body_struct =
self.fields.iter().all(|f| !matches!(f, ResponseField::NewtypeRawBody(_))).then(|| { self.fields.iter().all(|f| !matches!(f, ResponseField::RawBody(_))).then(|| {
let newtype_body_field = let newtype_body_field =
self.fields.iter().find(|f| matches!(f, ResponseField::NewtypeBody(_))); self.fields.iter().find(|f| matches!(f, ResponseField::NewtypeBody(_)));
let def = if let Some(body_field) = newtype_body_field { let def = if let Some(body_field) = newtype_body_field {
@ -133,9 +133,10 @@ impl Response {
panic!("This macro doesn't support generic types"); panic!("This macro doesn't support generic types");
} }
let newtype_body_fields = self.fields.iter().filter(|f| { let newtype_body_fields = self
matches!(f, ResponseField::NewtypeBody(_) | ResponseField::NewtypeRawBody(_)) .fields
}); .iter()
.filter(|f| matches!(f, ResponseField::NewtypeBody(_) | ResponseField::RawBody(_)));
let has_newtype_body_field = match newtype_body_fields.count() { let has_newtype_body_field = match newtype_body_fields.count() {
0 => false, 0 => false,
@ -172,7 +173,7 @@ enum ResponseField {
NewtypeBody(Field), NewtypeBody(Field),
/// Arbitrary bytes in the body of the response. /// Arbitrary bytes in the body of the response.
NewtypeRawBody(Field), RawBody(Field),
} }
impl ResponseField { impl ResponseField {
@ -182,7 +183,7 @@ impl ResponseField {
ResponseField::Body(field) ResponseField::Body(field)
| ResponseField::Header(field, _) | ResponseField::Header(field, _)
| ResponseField::NewtypeBody(field) | ResponseField::NewtypeBody(field)
| ResponseField::NewtypeRawBody(field) => field, | ResponseField::RawBody(field) => field,
} }
} }
@ -210,10 +211,10 @@ impl ResponseField {
} }
} }
/// Return the contained field if this response field is a newtype raw body kind. /// Return the contained field if this response field is a raw body kind.
fn as_newtype_raw_body_field(&self) -> Option<&Field> { fn as_raw_body_field(&self) -> Option<&Field> {
match self { match self {
ResponseField::NewtypeRawBody(field) => Some(field), ResponseField::RawBody(field) => Some(field),
_ => None, _ => None,
} }
} }
@ -252,7 +253,7 @@ impl TryFrom<Field> for ResponseField {
field_kind = Some(match meta { field_kind = Some(match meta {
Meta::Word(ident) => match &ident.to_string()[..] { Meta::Word(ident) => match &ident.to_string()[..] {
"body" => ResponseFieldKind::NewtypeBody, "body" => ResponseFieldKind::NewtypeBody,
"raw_body" => ResponseFieldKind::NewtypeRawBody, "raw_body" => ResponseFieldKind::RawBody,
_ => { _ => {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
ident, ident,
@ -280,7 +281,7 @@ impl TryFrom<Field> for ResponseField {
ResponseField::Header(field, header.expect("missing header name")) ResponseField::Header(field, header.expect("missing header name"))
} }
ResponseFieldKind::NewtypeBody => ResponseField::NewtypeBody(field), ResponseFieldKind::NewtypeBody => ResponseField::NewtypeBody(field),
ResponseFieldKind::NewtypeRawBody => ResponseField::NewtypeRawBody(field), ResponseFieldKind::RawBody => ResponseField::RawBody(field),
}) })
} }
} }
@ -302,7 +303,7 @@ enum ResponseFieldKind {
Body, Body,
Header, Header,
NewtypeBody, NewtypeBody,
NewtypeRawBody, RawBody,
} }
fn has_lifetime(ty: &Type) -> bool { fn has_lifetime(ty: &Type) -> bool {

View File

@ -40,7 +40,7 @@ impl Response {
let response_init_fields = { let response_init_fields = {
let mut fields = vec![]; let mut fields = vec![];
let mut new_type_raw_body = None; let mut raw_body = None;
for response_field in &self.fields { for response_field in &self.fields {
let field = response_field.field(); let field = response_field.field();
@ -92,8 +92,8 @@ impl Response {
// This field must be instantiated last to avoid `use of move value` error. // This field must be instantiated last to avoid `use of move value` error.
// We are guaranteed only one new body field because of a check in // We are guaranteed only one new body field because of a check in
// `parse_response`. // `parse_response`.
ResponseField::NewtypeRawBody(_) => { ResponseField::RawBody(_) => {
new_type_raw_body = Some(quote! { raw_body = Some(quote! {
#( #cfg_attrs )* #( #cfg_attrs )*
#field_name: { #field_name: {
::std::convert::AsRef::<[::std::primitive::u8]>::as_ref( ::std::convert::AsRef::<[::std::primitive::u8]>::as_ref(
@ -108,7 +108,7 @@ impl Response {
}); });
} }
fields.extend(new_type_raw_body); fields.extend(raw_body);
quote! { quote! {
#(#fields,)* #(#fields,)*

View File

@ -37,7 +37,7 @@ impl Response {
}) })
}); });
let body = if let Some(field) = self.newtype_raw_body_field() { let body = if let Some(field) = self.raw_body_field() {
let field_name = field.ident.as_ref().expect("expected field to have an identifier"); let field_name = field.ident.as_ref().expect("expected field to have an identifier");
quote! { #ruma_serde::slice_to_buf(&self.#field_name) } quote! { #ruma_serde::slice_to_buf(&self.#field_name) }
} else if let Some(field) = self.newtype_body_field() { } else if let Some(field) = self.newtype_body_field() {

View File

@ -93,7 +93,7 @@ pub mod newtype_body_endpoint {
} }
} }
pub mod newtype_raw_body_endpoint { pub mod raw_body_endpoint {
use ruma_api::ruma_api; use ruma_api::ruma_api;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]

View File

@ -42,7 +42,7 @@ mod newtype_body {
} }
} }
mod newtype_raw_body { mod raw_body {
use ruma_api::ruma_api; use ruma_api::ruma_api;
use ruma_identifiers::UserId; use ruma_identifiers::UserId;