formatting: Using canonical formatting on whole project
This commit is contained in:
@@ -38,17 +38,16 @@
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use syn::parse_macro_input;
|
||||
use syn::Data;
|
||||
use syn::DeriveInput;
|
||||
use syn::Error;
|
||||
use syn::Fields;
|
||||
use syn::LitStr;
|
||||
use syn::Result;
|
||||
use syn::parse_macro_input;
|
||||
|
||||
#[proc_macro_derive(Attributes, attributes(attribute))]
|
||||
pub fn derive_attributes(input: TokenStream) -> TokenStream
|
||||
{
|
||||
pub fn derive_attributes(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
expand(input)
|
||||
@@ -56,12 +55,10 @@ pub fn derive_attributes(input: TokenStream) -> TokenStream
|
||||
.into()
|
||||
}
|
||||
|
||||
fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
{
|
||||
fn expand(input: DeriveInput) -> Result<TokenStream2> {
|
||||
let ident = &input.ident;
|
||||
|
||||
if !input.generics.params.is_empty()
|
||||
{
|
||||
if !input.generics.params.is_empty() {
|
||||
return Err(Error::new_spanned(
|
||||
&input.generics,
|
||||
"`Attributes` cannot be derived for generic structs",
|
||||
@@ -70,8 +67,7 @@ fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
|
||||
check_repr(&input)?;
|
||||
|
||||
let data = match &input.data
|
||||
{
|
||||
let data = match &input.data {
|
||||
Data::Struct(data) => data,
|
||||
Data::Enum(_) => {
|
||||
return Err(Error::new_spanned(
|
||||
@@ -87,8 +83,7 @@ fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
}
|
||||
};
|
||||
|
||||
let fields = match &data.fields
|
||||
{
|
||||
let fields = match &data.fields {
|
||||
Fields::Named(fields) => &fields.named,
|
||||
_ => {
|
||||
return Err(Error::new_spanned(
|
||||
@@ -106,8 +101,7 @@ fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
let mut field_types = Vec::new();
|
||||
let mut index = 0usize;
|
||||
|
||||
for field in fields
|
||||
{
|
||||
for field in fields {
|
||||
let name = field
|
||||
.ident
|
||||
.as_ref()
|
||||
@@ -117,10 +111,8 @@ fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
field_types.push(quote! { #ty });
|
||||
|
||||
let mut options = FieldOptions::default();
|
||||
for attribute in &field.attrs
|
||||
{
|
||||
if !attribute.path().is_ident("attribute")
|
||||
{
|
||||
for attribute in &field.attrs {
|
||||
if !attribute.path().is_ident("attribute") {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -149,8 +141,7 @@ fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
})?;
|
||||
}
|
||||
|
||||
if options.skip
|
||||
{
|
||||
if options.skip {
|
||||
skipped.push(quote! {
|
||||
#name: ::core::default::Default::default()
|
||||
});
|
||||
@@ -161,8 +152,7 @@ fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
.label
|
||||
.unwrap_or_else(|| LitStr::new(&name.to_string(), name.span()));
|
||||
|
||||
let semantic = match options.semantic
|
||||
{
|
||||
let semantic = match options.semantic {
|
||||
Some(semantic) => quote! { ::core::option::Option::Some(#semantic) },
|
||||
None => quote! { ::core::option::Option::None },
|
||||
};
|
||||
@@ -272,39 +262,31 @@ fn expand(input: DeriveInput) -> Result<TokenStream2>
|
||||
|
||||
/// Collects the `#[attribute(...)]` options of a single field.
|
||||
#[derive(Default)]
|
||||
struct FieldOptions
|
||||
{
|
||||
struct FieldOptions {
|
||||
skip: bool,
|
||||
label: Option<LitStr>,
|
||||
semantic: Option<LitStr>,
|
||||
}
|
||||
|
||||
fn check_repr(input: &DeriveInput) -> Result<()>
|
||||
{
|
||||
fn check_repr(input: &DeriveInput) -> Result<()> {
|
||||
let mut found = false;
|
||||
|
||||
for attribute in &input.attrs
|
||||
{
|
||||
if !attribute.path().is_ident("repr")
|
||||
{
|
||||
for attribute in &input.attrs {
|
||||
if !attribute.path().is_ident("repr") {
|
||||
continue;
|
||||
}
|
||||
|
||||
attribute.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("C") || meta.path.is_ident("transparent")
|
||||
{
|
||||
if meta.path.is_ident("C") || meta.path.is_ident("transparent") {
|
||||
found = true;
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
|
||||
if found
|
||||
{
|
||||
if found {
|
||||
Ok(())
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Err(Error::new_spanned(
|
||||
&input.ident,
|
||||
"`Attributes` requires the struct to be `#[repr(C)]` or `#[repr(transparent)]`",
|
||||
|
||||
Reference in New Issue
Block a user