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

Skip Rust's escaping in the enum_def macro (#793) #794

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 12 additions & 4 deletions sea-query-attr/src/lib.rs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could reduce the number of allocations, but that's not the point here.

Original file line number Diff line number Diff line change
Expand Up @@ -58,10 58,18 @@ pub fn enum_def(args: TokenStream, input: TokenStream) -> TokenStream {
.iter()
.map(|field| {
let ident = &field.ident;
let string = ident
.as_ref()
.expect("#[enum_def] can only be used on structs with named fields")
.to_string();
let string = {
let mut inner = ident
.as_ref()
.expect("#[enum_def] can only be used on structs with named fields")
.to_string();

if inner.starts_with("r#") {
inner = (&inner[2..]).to_string();
}
Comment on lines 67 to 69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a IdentExt::unraw helper which does exactly this

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, how to use IdentExt::unraw ?


inner
};
let as_pascal = string.to_pascal_case();
NamingHolder {
default: ident.as_ref().unwrap().clone(),
Expand Down
10 changes: 10 additions & 0 deletions sea-query-attr/tests/pass/escaping.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@
use sea_query_attr::enum_def;

#[enum_def]
pub struct Hello {
pub r#type: String
}

fn main() {
println!("{:?}", HelloIden::Type);
}