Skip to content
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

Allow raw identifiers. #71

Merged
merged 2 commits into from
Mar 19, 2021
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
38 changes: 23 additions & 15 deletions src/generate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span};
use proc_macro_error::{abort, ResultExt};
use syn::{self, spanned::Spanned, Field, Lit, Meta, MetaNameValue, Visibility};
use syn::{self, ext::IdentExt, spanned::Spanned, Field, Lit, Meta, MetaNameValue, Visibility};

use self::GenMode::*;
use super::parse_attr;
Expand Down Expand Up @@ -112,20 +112,28 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
.ident
.unwrap_or_else(|| abort!(field.span(), "Expected the field to have a name"));

let fn_name = Ident::new(
&format!(
"{}{}{}{}",
if has_prefix_attr(field, params) && (params.mode.is_get()) {
"get_"
} else {
""
},
params.mode.prefix(),
field_name,
params.mode.suffix()
),
Span::call_site(),
);
let fn_name = if !has_prefix_attr(field, params)
&& (params.mode.is_get())
&& params.mode.suffix().is_empty()
&& field_name.to_string().starts_with("r#")
{
field_name.clone()
} else {
Ident::new(
&format!(
"{}{}{}{}",
if has_prefix_attr(field, params) && (params.mode.is_get()) {
"get_"
} else {
""
},
params.mode.prefix(),
field_name.unraw(),
params.mode.suffix()
),
Span::call_site(),
)
};
let ty = field.ty.clone();

let doc = field.attrs.iter().filter(|v| {
Expand Down
57 changes: 57 additions & 0 deletions tests/raw_identifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[macro_use]
extern crate getset;

#[derive(CopyGetters, Default, Getters, MutGetters, Setters)]
struct RawIdentifiers {
#[get]
r#type: usize,
#[get_copy]
r#move: usize,
#[get_mut]
r#union: usize,
#[set]
r#enum: usize,
#[get = "with_prefix"]
r#const: usize,
#[get_copy = "with_prefix"]
r#if: usize,
// Ensure having no gen mode doesn't break things.
#[allow(dead_code)]
r#loop: usize,
}

#[test]
fn test_get() {
let val = RawIdentifiers::default();
let _ = val.r#type();
}

#[test]
fn test_get_copy() {
let val = RawIdentifiers::default();
let _ = val.r#move();
}

#[test]
fn test_get_mut() {
let mut val = RawIdentifiers::default();
let _ = val.union_mut();
}

#[test]
fn test_set() {
let mut val = RawIdentifiers::default();
val.set_enum(42);
}

#[test]
fn test_get_with_prefix() {
let val = RawIdentifiers::default();
let _ = val.get_const();
}

#[test]
fn test_get_copy_with_prefix() {
let val = RawIdentifiers::default();
let _ = val.get_if();
}