Skip to content

Commit

Permalink
fmt: add rustfmt.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsonin committed May 9, 2024
1 parent ac8f107 commit ca6b00e
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 135 deletions.
6 changes: 6 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
edition = "2021"
merge_derives = false
imports_granularity = "Crate"
normalize_comments = true
reorder_impl_items = true
wrap_comments = true
170 changes: 82 additions & 88 deletions src/field_attribute_layout.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,16 +13,13 @@ pub struct AttributeLayout {
}

impl<T> From<T> for AttributeLayout
where
T: IntoIterator<Item=Meta>
where
T: IntoIterator<Item = Meta>,
{
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) = &current_layout.kind {
Expand All @@ -35,15 +30,14 @@ impl<T> From<T> 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) => {
Expand All @@ -53,7 +47,7 @@ impl<T> From<T> 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" => {
Expand All @@ -77,7 +71,7 @@ impl<T> From<T> for AttributeLayout
_ => abort!(
lit.span(),
"Unknown named attribute. Should be one of: `name`, `vis`, `type`"
)
),
}
}
}
Expand All @@ -86,19 +80,18 @@ impl<T> From<T> 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 });
Expand All @@ -111,131 +104,131 @@ 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 => {
let fn_name = mut_getter_fn_name();
(
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 => {
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::DerefGet => {
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 as ::std::ops::Deref>::Target })
get_ty_override()
.unwrap_or_else(|| quote! { &<#field_type as ::std::ops::Deref>::Target }),
)
}
AttributeKind::DerefGetMut => {
let fn_name = mut_getter_fn_name();
(
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 => {
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 as ::std::ops::Deref>::Target })
get_ty_override()
.unwrap_or_else(|| quote! { <#field_type as ::std::ops::Deref>::Target }),
)
}
AttributeKind::Set => {
let fn_name = setter_fn_name();
(
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,
)
}
};
Expand All @@ -251,19 +244,20 @@ fn generate_fn_name<F>(
fn_name_override: Option<&Ident>,
fn_name_fallback: impl FnOnce() -> F,
attr_span: impl FnOnce() -> Span,
attr_kind: AttributeKind) -> Ident
where
F: AsRef<str>
attr_kind: AttributeKind,
) -> Ident
where
F: AsRef<str>,
{
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
)
)
})
}
2 changes: 1 addition & 1 deletion src/field_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ define_field_attributes!(
AsDerefGet => "as_deref_get",
AsDerefGetMut => "as_deref_get_mut"
}
);
);
Loading

0 comments on commit ca6b00e

Please sign in to comment.