Configure rustfmt to produce denser code
This commit is contained in:
parent
a969fcf625
commit
5a6557e7d7
3
.rustfmt.toml
Normal file
3
.rustfmt.toml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
edition = "2018"
|
||||||
|
merge_imports = true
|
||||||
|
use_small_heuristics = "Max"
|
@ -30,10 +30,9 @@ impl Meta {
|
|||||||
/// Panics if the given attribute is a ruma_api attribute, but fails to parse.
|
/// Panics if the given attribute is a ruma_api attribute, but fails to parse.
|
||||||
pub fn from_attribute(attr: &syn::Attribute) -> syn::Result<Option<Self>> {
|
pub fn from_attribute(attr: &syn::Attribute) -> syn::Result<Option<Self>> {
|
||||||
match &attr.path {
|
match &attr.path {
|
||||||
syn::Path {
|
syn::Path { leading_colon: None, segments }
|
||||||
leading_colon: None,
|
if segments.len() == 1 && segments[0].ident == "ruma_api" =>
|
||||||
segments,
|
{
|
||||||
} if segments.len() == 1 && segments[0].ident == "ruma_api" => {
|
|
||||||
attr.parse_args().map(Some)
|
attr.parse_args().map(Some)
|
||||||
}
|
}
|
||||||
_ => Ok(None),
|
_ => Ok(None),
|
||||||
@ -47,10 +46,7 @@ impl Parse for Meta {
|
|||||||
|
|
||||||
if input.peek(Token![=]) {
|
if input.peek(Token![=]) {
|
||||||
let _ = input.parse::<Token![=]>();
|
let _ = input.parse::<Token![=]>();
|
||||||
Ok(Meta::NameValue(MetaNameValue {
|
Ok(Meta::NameValue(MetaNameValue { name: ident, value: input.parse()? }))
|
||||||
name: ident,
|
|
||||||
value: input.parse()?,
|
|
||||||
}))
|
|
||||||
} else {
|
} else {
|
||||||
Ok(Meta::Word(ident))
|
Ok(Meta::Word(ident))
|
||||||
}
|
}
|
||||||
|
@ -42,10 +42,7 @@ impl TryFrom<RawMetadata> for Metadata {
|
|||||||
|
|
||||||
match &identifier.to_string()[..] {
|
match &identifier.to_string()[..] {
|
||||||
"description" => match expr {
|
"description" => match expr {
|
||||||
Expr::Lit(ExprLit {
|
Expr::Lit(ExprLit { lit: Lit::Str(literal), .. }) => {
|
||||||
lit: Lit::Str(literal),
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
description = Some(literal);
|
description = Some(literal);
|
||||||
}
|
}
|
||||||
_ => return Err(syn::Error::new_spanned(expr, "expected a string literal")),
|
_ => return Err(syn::Error::new_spanned(expr, "expected a string literal")),
|
||||||
@ -57,37 +54,25 @@ impl TryFrom<RawMetadata> for Metadata {
|
|||||||
_ => return Err(syn::Error::new_spanned(expr, "expected an identifier")),
|
_ => return Err(syn::Error::new_spanned(expr, "expected an identifier")),
|
||||||
},
|
},
|
||||||
"name" => match expr {
|
"name" => match expr {
|
||||||
Expr::Lit(ExprLit {
|
Expr::Lit(ExprLit { lit: Lit::Str(literal), .. }) => {
|
||||||
lit: Lit::Str(literal),
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
name = Some(literal);
|
name = Some(literal);
|
||||||
}
|
}
|
||||||
_ => return Err(syn::Error::new_spanned(expr, "expected a string literal")),
|
_ => return Err(syn::Error::new_spanned(expr, "expected a string literal")),
|
||||||
},
|
},
|
||||||
"path" => match expr {
|
"path" => match expr {
|
||||||
Expr::Lit(ExprLit {
|
Expr::Lit(ExprLit { lit: Lit::Str(literal), .. }) => {
|
||||||
lit: Lit::Str(literal),
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
path = Some(literal);
|
path = Some(literal);
|
||||||
}
|
}
|
||||||
_ => return Err(syn::Error::new_spanned(expr, "expected a string literal")),
|
_ => return Err(syn::Error::new_spanned(expr, "expected a string literal")),
|
||||||
},
|
},
|
||||||
"rate_limited" => match expr {
|
"rate_limited" => match expr {
|
||||||
Expr::Lit(ExprLit {
|
Expr::Lit(ExprLit { lit: Lit::Bool(literal), .. }) => {
|
||||||
lit: Lit::Bool(literal),
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
rate_limited = Some(literal);
|
rate_limited = Some(literal);
|
||||||
}
|
}
|
||||||
_ => return Err(syn::Error::new_spanned(expr, "expected a bool literal")),
|
_ => return Err(syn::Error::new_spanned(expr, "expected a bool literal")),
|
||||||
},
|
},
|
||||||
"requires_authentication" => match expr {
|
"requires_authentication" => match expr {
|
||||||
Expr::Lit(ExprLit {
|
Expr::Lit(ExprLit { lit: Lit::Bool(literal), .. }) => {
|
||||||
lit: Lit::Bool(literal),
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
requires_authentication = Some(literal);
|
requires_authentication = Some(literal);
|
||||||
}
|
}
|
||||||
_ => return Err(syn::Error::new_spanned(expr, "expected a bool literal")),
|
_ => return Err(syn::Error::new_spanned(expr, "expected a bool literal")),
|
||||||
|
@ -162,10 +162,7 @@ impl ToTokens for Api {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let create_http_request = if let Some(field) = self.request.newtype_body_field() {
|
let create_http_request = if let Some(field) = self.request.newtype_body_field() {
|
||||||
let field_name = field
|
let field_name = field.ident.as_ref().expect("expected field to have an identifier");
|
||||||
.ident
|
|
||||||
.as_ref()
|
|
||||||
.expect("expected field to have an identifier");
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
let request_body = RequestBody(request.#field_name);
|
let request_body = RequestBody(request.#field_name);
|
||||||
@ -335,11 +332,7 @@ pub struct RawApi {
|
|||||||
|
|
||||||
impl Parse for RawApi {
|
impl Parse for RawApi {
|
||||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self { metadata: input.parse()?, request: input.parse()?, response: input.parse()? })
|
||||||
metadata: input.parse()?,
|
|
||||||
request: input.parse()?,
|
|
||||||
response: input.parse()?,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,9 +79,7 @@ impl Request {
|
|||||||
|
|
||||||
/// Returns the body field.
|
/// Returns the body field.
|
||||||
pub fn newtype_body_field(&self) -> Option<&Field> {
|
pub fn newtype_body_field(&self) -> Option<&Field> {
|
||||||
self.fields
|
self.fields.iter().find_map(RequestField::as_newtype_body_field)
|
||||||
.iter()
|
|
||||||
.find_map(RequestField::as_newtype_body_field)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Produces code for a struct initializer for body fields on a variable named `request`.
|
/// Produces code for a struct initializer for body fields on a variable named `request`.
|
||||||
@ -108,10 +106,8 @@ impl Request {
|
|||||||
) -> TokenStream {
|
) -> TokenStream {
|
||||||
let fields = self.fields.iter().filter_map(|f| {
|
let fields = self.fields.iter().filter_map(|f| {
|
||||||
f.field_of_kind(request_field_kind).map(|field| {
|
f.field_of_kind(request_field_kind).map(|field| {
|
||||||
let field_name = field
|
let field_name =
|
||||||
.ident
|
field.ident.as_ref().expect("expected field to have an identifier");
|
||||||
.as_ref()
|
|
||||||
.expect("expected field to have an identifier");
|
|
||||||
let span = field.span();
|
let span = field.span();
|
||||||
|
|
||||||
quote_spanned! {span=>
|
quote_spanned! {span=>
|
||||||
@ -228,10 +224,8 @@ impl ToTokens for Request {
|
|||||||
let request_struct_body = if self.fields.is_empty() {
|
let request_struct_body = if self.fields.is_empty() {
|
||||||
quote!(;)
|
quote!(;)
|
||||||
} else {
|
} else {
|
||||||
let fields = self
|
let fields =
|
||||||
.fields
|
self.fields.iter().map(|request_field| strip_serde_attrs(request_field.field()));
|
||||||
.iter()
|
|
||||||
.map(|request_field| strip_serde_attrs(request_field.field()));
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
{
|
{
|
||||||
|
@ -35,48 +35,39 @@ impl Response {
|
|||||||
|
|
||||||
/// Produces code for a response 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.iter().map(|response_field| match response_field {
|
||||||
.fields
|
ResponseField::Body(field) => {
|
||||||
.iter()
|
let field_name =
|
||||||
.map(|response_field| match response_field {
|
field.ident.as_ref().expect("expected field to have an identifier");
|
||||||
ResponseField::Body(field) => {
|
let span = field.span();
|
||||||
let field_name = field
|
|
||||||
.ident
|
|
||||||
.as_ref()
|
|
||||||
.expect("expected field to have an identifier");
|
|
||||||
let span = field.span();
|
|
||||||
|
|
||||||
quote_spanned! {span=>
|
quote_spanned! {span=>
|
||||||
#field_name: response_body.#field_name
|
#field_name: response_body.#field_name
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ResponseField::Header(field, header_name) => {
|
}
|
||||||
let field_name = field
|
ResponseField::Header(field, header_name) => {
|
||||||
.ident
|
let field_name =
|
||||||
.as_ref()
|
field.ident.as_ref().expect("expected field to have an identifier");
|
||||||
.expect("expected field to have an identifier");
|
let span = field.span();
|
||||||
let span = field.span();
|
|
||||||
|
|
||||||
quote_spanned! {span=>
|
quote_spanned! {span=>
|
||||||
#field_name: headers.remove(ruma_api::exports::http::header::#header_name)
|
#field_name: headers.remove(ruma_api::exports::http::header::#header_name)
|
||||||
.expect("response missing expected header")
|
.expect("response missing expected header")
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("failed to convert HeaderValue to str")
|
.expect("failed to convert HeaderValue to str")
|
||||||
.to_owned()
|
.to_owned()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ResponseField::NewtypeBody(field) => {
|
}
|
||||||
let field_name = field
|
ResponseField::NewtypeBody(field) => {
|
||||||
.ident
|
let field_name =
|
||||||
.as_ref()
|
field.ident.as_ref().expect("expected field to have an identifier");
|
||||||
.expect("expected field to have an identifier");
|
let span = field.span();
|
||||||
let span = field.span();
|
|
||||||
|
|
||||||
quote_spanned! {span=>
|
quote_spanned! {span=>
|
||||||
#field_name: response_body
|
#field_name: response_body
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#(#fields,)*
|
#(#fields,)*
|
||||||
@ -85,9 +76,7 @@ impl Response {
|
|||||||
|
|
||||||
/// Gets the newtype body field, if this response 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> {
|
||||||
self.fields
|
self.fields.iter().find_map(ResponseField::as_newtype_body_field)
|
||||||
.iter()
|
|
||||||
.find_map(ResponseField::as_newtype_body_field)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,10 +179,8 @@ impl ToTokens for Response {
|
|||||||
let response_struct_body = if self.fields.is_empty() {
|
let response_struct_body = if self.fields.is_empty() {
|
||||||
quote!(;)
|
quote!(;)
|
||||||
} else {
|
} else {
|
||||||
let fields = self
|
let fields =
|
||||||
.fields
|
self.fields.iter().map(|response_field| strip_serde_attrs(response_field.field()));
|
||||||
.iter()
|
|
||||||
.map(|response_field| strip_serde_attrs(response_field.field()));
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
{
|
{
|
||||||
|
10
src/lib.rs
10
src/lib.rs
@ -11,11 +11,7 @@
|
|||||||
//! those requests.
|
//! those requests.
|
||||||
|
|
||||||
#![warn(rust_2018_idioms)]
|
#![warn(rust_2018_idioms)]
|
||||||
#![deny(
|
#![deny(missing_copy_implementations, missing_debug_implementations, missing_docs)]
|
||||||
missing_copy_implementations,
|
|
||||||
missing_debug_implementations,
|
|
||||||
missing_docs
|
|
||||||
)]
|
|
||||||
// Since we support Rust 1.34.2, we can't apply this suggestion yet
|
// Since we support Rust 1.34.2, we can't apply this suggestion yet
|
||||||
#![allow(clippy::use_self)]
|
#![allow(clippy::use_self)]
|
||||||
|
|
||||||
@ -218,9 +214,7 @@ mod tests {
|
|||||||
.to_string()
|
.to_string()
|
||||||
.replace(":room_alias", &request.room_alias.to_string());
|
.replace(":room_alias", &request.room_alias.to_string());
|
||||||
|
|
||||||
let request_body = RequestBody {
|
let request_body = RequestBody { room_id: request.room_id };
|
||||||
room_id: request.room_id,
|
|
||||||
};
|
|
||||||
|
|
||||||
let http_request = http::Request::builder()
|
let http_request = http::Request::builder()
|
||||||
.method(metadata.method)
|
.method(metadata.method)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user