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