diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..6b10b88 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,6 @@ +edition = "2021" +merge_derives = false +imports_granularity = "Crate" +normalize_comments = true +reorder_impl_items = true +wrap_comments = true \ No newline at end of file diff --git a/src/field_attribute_layout.rs b/src/field_attribute_layout.rs index da3de24..8c6c420 100644 --- a/src/field_attribute_layout.rs +++ b/src/field_attribute_layout.rs @@ -1,10 +1,8 @@ -use { - crate::field_attributes::AttributeKind, - proc_macro2::{Ident, Span, TokenStream as TokenStream2}, - proc_macro_error::{abort, ResultExt}, - quote::quote, - syn::{Lit, Meta, MetaNameValue, spanned::Spanned, Type, Visibility}, -}; +use crate::field_attributes::AttributeKind; +use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; +use proc_macro_error::{abort, ResultExt}; +use quote::quote; +use syn::{spanned::Spanned, Lit, Meta, MetaNameValue, Type, Visibility}; #[derive(Default)] pub struct AttributeLayout { @@ -15,16 +13,13 @@ pub struct AttributeLayout { } impl From for AttributeLayout - where - T: IntoIterator +where + T: IntoIterator, { - fn from(metas: T) -> Self - { + fn from(metas: T) -> Self { let mut current_layout = Self::default(); - for meta in metas - { - match &meta - { + for meta in metas { + match &meta { Meta::Path(path) => { let path = quote! { #path }.to_string(); if let Some(kind) = ¤t_layout.kind { @@ -35,15 +30,14 @@ impl From for AttributeLayout path ) } - let kind: AttributeKind = path.parse() - .unwrap_or_else( - |_| abort!( - meta.span(), - "Unknown getset kind attribute: `{}`. Should be one of: {}", - path, - AttributeKind::all_kinds() - ) - ); + let kind: AttributeKind = path.parse().unwrap_or_else(|_| { + abort!( + meta.span(), + "Unknown getset kind attribute: `{}`. Should be one of: {}", + path, + AttributeKind::all_kinds() + ) + }); current_layout.kind = kind.into() } Meta::List(list) => { @@ -53,7 +47,7 @@ impl From for AttributeLayout let path = quote! { #path }.to_string(); let lit_str = match &lit { Lit::Str(str) => str.value(), - _ => abort!(lit.span(), "Value type not supported. Should be string") + _ => abort!(lit.span(), "Value type not supported. Should be string"), }; match path.as_str() { "name" => { @@ -77,7 +71,7 @@ impl From for AttributeLayout _ => abort!( lit.span(), "Unknown named attribute. Should be one of: `name`, `vis`, `type`" - ) + ), } } } @@ -86,19 +80,18 @@ impl From for AttributeLayout } } -impl AttributeLayout -{ +impl AttributeLayout { pub fn generate_fn_def( self, field_ident_or_idx: &str, field_type: &Type, - attr_span: impl Fn() -> Span) -> TokenStream2 - { + attr_span: impl Fn() -> Span, + ) -> TokenStream2 { let Self { fn_name_override, visibility, kind, - type_override + type_override, } = self; let fn_name_override = fn_name_override.as_ref(); let get_ty_override = || type_override.map(|ty| quote! { #ty }); @@ -111,35 +104,34 @@ impl AttributeLayout ) }; - let getter_fn_name = || generate_fn_name( - fn_name_override, - || field_ident_or_idx, - &attr_span, - kind, - ); - let mut_getter_fn_name = || generate_fn_name( - fn_name_override, - || format!("{field_ident_or_idx}_mut"), - &attr_span, - kind, - ); - let setter_fn_name = || generate_fn_name( - fn_name_override, - || format!("set_{field_ident_or_idx}"), - &attr_span, - kind, - ); + let getter_fn_name = + || generate_fn_name(fn_name_override, || field_ident_or_idx, &attr_span, kind); + let mut_getter_fn_name = || { + generate_fn_name( + fn_name_override, + || format!("{field_ident_or_idx}_mut"), + &attr_span, + kind, + ) + }; + let setter_fn_name = || { + generate_fn_name( + fn_name_override, + || format!("set_{field_ident_or_idx}"), + &attr_span, + kind, + ) + }; let field_ident_or_idx: TokenStream2 = field_ident_or_idx.parse().unwrap(); - let (signature, body, ty) = match kind - { + let (signature, body, ty) = match kind { AttributeKind::Get => { let fn_name = getter_fn_name(); ( quote! { #fn_name(&self) }, quote! { &self.#field_ident_or_idx }, - get_ty_override().unwrap_or_else(|| quote! { &#field_type }) + get_ty_override().unwrap_or_else(|| quote! { &#field_type }), ) } AttributeKind::GetMut => { @@ -147,7 +139,7 @@ impl AttributeLayout ( quote! { #fn_name(&mut self) }, quote! { &mut self.#field_ident_or_idx }, - get_ty_override().unwrap_or_else(|| quote! { &mut #field_type }) + get_ty_override().unwrap_or_else(|| quote! { &mut #field_type }), ) } AttributeKind::GetCopy => { @@ -155,7 +147,7 @@ impl AttributeLayout ( quote! { #fn_name(&self) }, quote! { self.#field_ident_or_idx }, - get_ty_override().unwrap_or_else(|| quote! { #field_type }) + get_ty_override().unwrap_or_else(|| quote! { #field_type }), ) } AttributeKind::DerefGet => { @@ -163,7 +155,8 @@ impl AttributeLayout ( quote! { #fn_name(&self) }, quote! { &self.#field_ident_or_idx }, - get_ty_override().unwrap_or_else(|| quote! { &<#field_type as ::std::ops::Deref>::Target }) + get_ty_override() + .unwrap_or_else(|| quote! { &<#field_type as ::std::ops::Deref>::Target }), ) } AttributeKind::DerefGetMut => { @@ -171,7 +164,9 @@ impl AttributeLayout ( quote! { #fn_name(&mut self) }, quote! { &mut self.#field_ident_or_idx }, - get_ty_override().unwrap_or_else(|| quote! { &mut <#field_type as ::std::ops::Deref>::Target }) + get_ty_override().unwrap_or_else( + || quote! { &mut <#field_type as ::std::ops::Deref>::Target }, + ), ) } AttributeKind::DerefGetCopy => { @@ -179,7 +174,8 @@ impl AttributeLayout ( quote! { #fn_name(&self) }, quote! { *self.#field_ident_or_idx }, - get_ty_override().unwrap_or_else(|| quote! { <#field_type as ::std::ops::Deref>::Target }) + get_ty_override() + .unwrap_or_else(|| quote! { <#field_type as ::std::ops::Deref>::Target }), ) } AttributeKind::Set => { @@ -187,55 +183,52 @@ impl AttributeLayout ( quote! { #fn_name(&mut self, value: #field_type) }, quote! { self.#field_ident_or_idx = value }, - get_ty_override().unwrap_or_else(|| quote! { () }) + get_ty_override().unwrap_or_else(|| quote! { () }), ) } AttributeKind::AsRefGet => { let fn_name = getter_fn_name(); - let ty = get_ty_override() - .unwrap_or_else( - || abort!( - attr_span(), - "Missed `type` attribute. \ + let ty = get_ty_override().unwrap_or_else(|| { + abort!( + attr_span(), + "Missed `type` attribute. \ Should be set for `as_ref_get` getset kind", - ) - ); + ) + }); ( quote! { #fn_name(&self) }, quote! { self.#field_ident_or_idx.as_ref() }, - ty + ty, ) } AttributeKind::AsDerefGet => { let fn_name = getter_fn_name(); - let ty = get_ty_override() - .unwrap_or_else( - || abort!( - attr_span(), - "Missed `type` attribute. \ + let ty = get_ty_override().unwrap_or_else(|| { + abort!( + attr_span(), + "Missed `type` attribute. \ Should be set for `as_deref_get` getset kind", - ) - ); + ) + }); ( quote! { #fn_name(&self) }, quote! { self.#field_ident_or_idx.as_deref() }, - ty + ty, ) } AttributeKind::AsDerefGetMut => { let fn_name = mut_getter_fn_name(); - let ty = get_ty_override() - .unwrap_or_else( - || abort!( - attr_span(), - "Missed `type` attribute. \ + let ty = get_ty_override().unwrap_or_else(|| { + abort!( + attr_span(), + "Missed `type` attribute. \ Should be set for `as_deref_get_mut` getset kind", - ) - ); + ) + }); ( quote! { #fn_name(&mut self) }, quote! { self.#field_ident_or_idx.as_deref_mut() }, - ty + ty, ) } }; @@ -251,19 +244,20 @@ fn generate_fn_name( fn_name_override: Option<&Ident>, fn_name_fallback: impl FnOnce() -> F, attr_span: impl FnOnce() -> Span, - attr_kind: AttributeKind) -> Ident - where - F: AsRef + attr_kind: AttributeKind, +) -> Ident +where + F: AsRef, { fn_name_override .cloned() .or_else(|| syn::parse_str(fn_name_fallback().as_ref()).ok()) - .unwrap_or_else( - || abort!( + .unwrap_or_else(|| { + abort!( attr_span(), "Missed `name` attribute. \ Should be set for `{}` getset kind when struct ", attr_kind ) - ) + }) } diff --git a/src/field_attributes.rs b/src/field_attributes.rs index 8d0a7cc..d8ed1f0 100644 --- a/src/field_attributes.rs +++ b/src/field_attributes.rs @@ -70,4 +70,4 @@ define_field_attributes!( AsDerefGet => "as_deref_get", AsDerefGetMut => "as_deref_get_mut" } -); \ No newline at end of file +); diff --git a/src/lib.rs b/src/lib.rs index cc3dce3..de1a3e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,11 @@ -use { - field_attribute_layout::AttributeLayout, - proc_macro2::TokenStream as TokenStream2, - proc_macro::TokenStream, - proc_macro_error::{abort_call_site, proc_macro_error, ResultExt}, - quote::quote, - syn::{ - Data, - DataStruct, - DeriveInput, - Field, - Meta, - parse_macro_input, - punctuated::Punctuated, - spanned::Spanned, - Token, - }, +use field_attribute_layout::AttributeLayout; +use proc_macro::TokenStream; +use proc_macro2::TokenStream as TokenStream2; +use proc_macro_error::{abort_call_site, proc_macro_error, ResultExt}; +use quote::quote; +use syn::{ + parse_macro_input, punctuated::Punctuated, spanned::Spanned, Data, DataStruct, DeriveInput, + Field, Meta, Token, }; mod field_attribute_layout; @@ -22,8 +13,7 @@ mod field_attributes; #[proc_macro_derive(Getset, attributes(getset))] #[proc_macro_error] -pub fn derive_getset(input: TokenStream) -> TokenStream -{ +pub fn derive_getset(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); let DeriveInput { ident, @@ -35,48 +25,48 @@ pub fn derive_getset(input: TokenStream) -> TokenStream let fields = match data { Data::Struct(DataStruct { fields, .. }) => fields, - _ => abort_call_site!("#[derive(Getset)] is only supported for structs") + _ => abort_call_site!("#[derive(Getset)] is only supported for structs"), }; let mut impls = TokenStream2::new(); - for (idx, field) in fields.iter().enumerate() - { + for (idx, field) in fields.iter().enumerate() { let Field { attrs, ident: field_ident, ty: field_type, .. } = field; - let getset_attrs = attrs.iter() + let getset_attrs = attrs + .iter() .filter(|attr| attr.path.is_ident("getset")) .collect::>(); if getset_attrs.is_empty() { continue; } - let ident_or_index = field_ident.as_ref() + let ident_or_index = field_ident + .as_ref() .map(ToString::to_string) .unwrap_or_else(|| idx.to_string()); let ident_or_index = ident_or_index.as_str(); - let doc = attrs.iter() - .filter( - |v| { - v.parse_meta() - .map(|meta| meta.path().is_ident("doc")) - .unwrap_or(false) - } - ) + let doc = attrs + .iter() + .filter(|v| { + v.parse_meta() + .map(|meta| meta.path().is_ident("doc")) + .unwrap_or(false) + }) .collect::>(); - for attr in getset_attrs - { + for attr in getset_attrs { let metas = attr .parse_args_with(Punctuated::::parse_terminated) .unwrap_or_abort(); - let fn_def = AttributeLayout::from(metas) - .generate_fn_def(ident_or_index, field_type, || attr.span()); + let fn_def = + AttributeLayout::from(metas) + .generate_fn_def(ident_or_index, field_type, || attr.span()); let imp = quote! { #(#doc)* @@ -95,4 +85,4 @@ pub fn derive_getset(input: TokenStream) -> TokenStream } }; tokens.into() -} \ No newline at end of file +} diff --git a/tests/getset.rs b/tests/getset.rs index 7dbb6bd..99ff910 100644 --- a/tests/getset.rs +++ b/tests/getset.rs @@ -1,9 +1,15 @@ -use {gset::Getset, std::{fmt::Debug, ops::{Deref, DerefMut}}}; +#![allow(dead_code)] + +use gset::Getset; +use std::{ + fmt::Debug, + ops::{Deref, DerefMut}, +}; #[derive(Getset)] struct Struct - where - T: Debug +where + T: Debug, { /// Field 1. #[getset(get_copy, name = "get_field_1", vis = "pub")] @@ -30,14 +36,17 @@ struct Struct /// Field 5. #[getset(as_ref_get, name = "get_field_5", type = "Option<&F64>")] #[getset(as_deref_get, name = "get_field_5_deref", type = "Option<&f64>")] - #[getset(as_deref_get_mut, name = "get_field_5_deref_mut", type = "Option<&mut f64>")] + #[getset( + as_deref_get_mut, + name = "get_field_5_deref_mut", + type = "Option<&mut f64>" + )] field_5: Option, } struct F64(f64); -impl Deref for F64 -{ +impl Deref for F64 { type Target = f64; fn deref(&self) -> &Self::Target { @@ -45,9 +54,8 @@ impl Deref for F64 } } -impl DerefMut for F64 -{ +impl DerefMut for F64 { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } -} \ No newline at end of file +}