Skip to content

Add impl_attrs feature #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ quote = "1"
syn = "2"
proc-macro2 = { version = "1", default-features = false }
proc-macro-error2 = "2"

[dev-dependencies]
fixture = { path = "tests/fixture" }
66 changes: 45 additions & 21 deletions src/generate.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use proc_macro_error2::abort;
use syn::{
self, ext::IdentExt, spanned::Spanned, Expr, Field, Lit, Meta, MetaNameValue, Visibility,
self, ext::IdentExt, spanned::Spanned, Attribute, Expr, Field, Lit, Meta, MetaNameValue,
Visibility,
};

use self::GenMode::{Get, GetCopy, GetMut, Set, SetWith};
use self::GenMode::{Get, GetClone, GetCopy, GetMut, Set, SetWith};
use super::parse_attr;

pub struct GenParams {
pub mode: GenMode,
pub global_attr: Option<Meta>,
pub impl_attrs: Vec<Attribute>,
}

#[derive(PartialEq, Eq, Copy, Clone)]
pub enum GenMode {
Get,
GetClone,
GetCopy,
GetMut,
Set,
Expand All @@ -25,6 +28,7 @@ impl GenMode {
pub fn name(self) -> &'static str {
match self {
Get => "get",
GetClone => "get_clone",
GetCopy => "get_copy",
GetMut => "get_mut",
Set => "set",
Expand All @@ -34,29 +38,29 @@ impl GenMode {

pub fn prefix(self) -> &'static str {
match self {
Get | GetCopy | GetMut => "",
Get | GetClone | GetCopy | GetMut => "",
Set => "set_",
SetWith => "with_",
}
}

pub fn suffix(self) -> &'static str {
match self {
Get | GetCopy | Set | SetWith => "",
Get | GetClone | GetCopy | Set | SetWith => "",
GetMut => "_mut",
}
}

fn is_get(self) -> bool {
match self {
GenMode::Get | GenMode::GetCopy | GenMode::GetMut => true,
GenMode::Set | GenMode::SetWith => false,
Get | GetClone | GetCopy | GetMut => true,
Set | SetWith => false,
}
}
}

// Helper function to extract string from Expr
fn expr_to_string(expr: &Expr) -> Option<String> {
pub(crate) fn expr_to_string(expr: &Expr) -> Option<String> {
if let Expr::Lit(expr_lit) = expr {
if let Lit::Str(s) = &expr_lit.lit {
Some(s.value())
Expand All @@ -69,7 +73,7 @@ fn expr_to_string(expr: &Expr) -> Option<String> {
}

// Helper function to parse visibility
fn parse_vis_str(s: &str, span: proc_macro2::Span) -> Visibility {
fn parse_vis_str(s: &str, span: Span) -> Visibility {
match syn::parse_str(s) {
Ok(vis) => vis,
Err(e) => abort!(span, "Invalid visibility found: {}", e),
Expand Down Expand Up @@ -109,9 +113,10 @@ fn has_prefix_attr(f: &Field, params: &GenParams) -> bool {
let field_attr_has_prefix = f
.attrs
.iter()
.filter_map(|attr| parse_attr(attr, params.mode))
.filter_map(|attr| parse_attr(attr, params.mode, false).0)
.find(|meta| {
meta.path().is_ident("get")
|| meta.path().is_ident("get_clone")
|| meta.path().is_ident("get_copy")
|| meta.path().is_ident("get_mut")
})
Expand Down Expand Up @@ -158,7 +163,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
let attr = field
.attrs
.iter()
.filter_map(|v| parse_attr(v, params.mode))
.filter_map(|v| parse_attr(v, params.mode, false).0)
.last()
.or_else(|| params.global_attr.clone());

Expand All @@ -167,7 +172,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
// Generate nothing for skipped field
Some(meta) if meta.path().is_ident("skip") => quote! {},
Some(_) => match params.mode {
GenMode::Get => {
Get => {
quote! {
#(#doc)*
#[inline(always)]
Expand All @@ -176,7 +181,16 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
}
}
}
GenMode::GetCopy => {
GetClone => {
quote! {
#(#doc)*
#[inline(always)]
#visibility fn #fn_name(&self) -> #ty {
self.#field_name.clone()
}
}
}
GetCopy => {
quote! {
#(#doc)*
#[inline(always)]
Expand All @@ -185,7 +199,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
}
}
}
GenMode::Set => {
Set => {
quote! {
#(#doc)*
#[inline(always)]
Expand All @@ -195,7 +209,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
}
}
}
GenMode::GetMut => {
GetMut => {
quote! {
#(#doc)*
#[inline(always)]
Expand All @@ -204,7 +218,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
}
}
}
GenMode::SetWith => {
SetWith => {
quote! {
#(#doc)*
#[inline(always)]
Expand All @@ -224,7 +238,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
let attr = field
.attrs
.iter()
.filter_map(|v| parse_attr(v, params.mode))
.filter_map(|v| parse_attr(v, params.mode, false).0)
.last()
.or_else(|| params.global_attr.clone());
let ty = field.ty.clone();
Expand All @@ -234,7 +248,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
// Generate nothing for skipped field
Some(meta) if meta.path().is_ident("skip") => quote! {},
Some(_) => match params.mode {
GenMode::Get => {
Get => {
let fn_name = Ident::new("get", Span::call_site());
quote! {
#(#doc)*
Expand All @@ -244,7 +258,17 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
}
}
}
GenMode::GetCopy => {
GetClone => {
let fn_name = Ident::new("get", Span::call_site());
quote! {
#(#doc)*
#[inline(always)]
#visibility fn #fn_name(&self) -> #ty {
self.0.clone()
}
}
}
GetCopy => {
let fn_name = Ident::new("get", Span::call_site());
quote! {
#(#doc)*
Expand All @@ -254,7 +278,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
}
}
}
GenMode::Set => {
Set => {
let fn_name = Ident::new("set", Span::call_site());
quote! {
#(#doc)*
Expand All @@ -265,7 +289,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
}
}
}
GenMode::GetMut => {
GetMut => {
let fn_name = Ident::new("get_mut", Span::call_site());
quote! {
#(#doc)*
Expand All @@ -275,7 +299,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
}
}
}
GenMode::SetWith => {
SetWith => {
let fn_name = Ident::new("set_with", Span::call_site());
quote! {
#(#doc)*
Expand Down
Loading
Loading