Skip to content

Commit

Permalink
Merge pull request #71 from Anders429/master
Browse files Browse the repository at this point in the history
Allow raw identifiers.
  • Loading branch information
Hoverbear authored Mar 19, 2021
2 parents 3fb5351 + 2cb8b9d commit a4222c2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
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();
}

0 comments on commit a4222c2

Please sign in to comment.