Skip to content
Merged
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
52 changes: 37 additions & 15 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::{
self, ext::IdentExt, spanned::Spanned, 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 {
Expand All @@ -15,6 +15,7 @@ pub struct GenParams {
#[derive(PartialEq, Eq, Copy, Clone)]
pub enum GenMode {
Get,
GetClone,
GetCopy,
GetMut,
Set,
Expand All @@ -25,6 +26,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,23 +36,23 @@ 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,
}
}
}
Expand Down Expand Up @@ -112,6 +114,7 @@ fn has_prefix_attr(f: &Field, params: &GenParams) -> bool {
.filter_map(|attr| parse_attr(attr, params.mode))
.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 @@ -167,7 +170,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 +179,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 +197,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
}
}
}
GenMode::Set => {
Set => {
quote! {
#(#doc)*
#[inline(always)]
Expand All @@ -195,7 +207,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
}
}
}
GenMode::GetMut => {
GetMut => {
quote! {
#(#doc)*
#[inline(always)]
Expand All @@ -204,7 +216,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
}
}
}
GenMode::SetWith => {
SetWith => {
quote! {
#(#doc)*
#[inline(always)]
Expand Down Expand Up @@ -234,7 +246,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 +256,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 +276,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 +287,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 +297,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
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ pub fn getters(input: TokenStream) -> TokenStream {
produce(&ast, &params).into()
}

#[proc_macro_derive(CloneGetters, attributes(get_clone, with_prefix, getset))]
#[proc_macro_error]
pub fn clone_getters(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let params = GenParams {
mode: GenMode::GetClone,
global_attr: parse_global_attr(&ast.attrs, GenMode::GetClone),
};

produce(&ast, &params).into()
}

#[proc_macro_derive(CopyGetters, attributes(get_copy, with_prefix, getset))]
#[proc_macro_error]
pub fn copy_getters(input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -292,6 +304,7 @@ fn parse_attr(attr: &syn::Attribute, mode: GenMode) -> Option<syn::Meta> {
.into_iter()
.inspect(|meta| {
if !(meta.path().is_ident("get")
|| meta.path().is_ident("get_clone")
|| meta.path().is_ident("get_copy")
|| meta.path().is_ident("get_mut")
|| meta.path().is_ident("set")
Expand Down
Loading
Loading