Skip to content

Commit

Permalink
Add option for skipping fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Jul 23, 2021
1 parent a4222c2 commit 1181b1a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {

let visibility = parse_visibility(attr.as_ref(), params.mode.name());
match attr {
// Generate nothing for skipped field.
Some(meta) if meta.path().is_ident("skip") => quote! {},
Some(_) => match params.mode {
GenMode::Get => {
quote! {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,13 @@ fn parse_attr(attr: &syn::Attribute, mode: GenMode) -> Option<Meta> {
if !(meta.path().is_ident("get")
|| meta.path().is_ident("get_copy")
|| meta.path().is_ident("get_mut")
|| meta.path().is_ident("set"))
|| meta.path().is_ident("set")
|| meta.path().is_ident("skip"))
{
abort!(meta.path().span(), "unknown setter or getter")
}
})
.filter(|meta| meta.path().is_ident(mode.name()))
.filter(|meta| meta.path().is_ident(mode.name()) || meta.path().is_ident("skip"))
.last()
} else {
attr.parse_meta()
Expand Down
34 changes: 34 additions & 0 deletions tests/skip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[macro_use]
extern crate getset;

#[derive(CopyGetters)]
#[get_copy]
pub struct Plain {
/// A doc comment.
#[getset(skip)]
non_copyable: String,

copyable: usize,
}

impl Plain {
fn custom_non_copyable(&self) -> &str {
&self.non_copyable
}
}

impl Default for Plain {
fn default() -> Self {
Plain {
non_copyable: "foo".to_string(),
copyable: 3,
}
}
}

#[test]
fn test_plain() {
let val = Plain::default();
val.copyable();
val.custom_non_copyable();
}

0 comments on commit 1181b1a

Please sign in to comment.