proc-macro2
proc-macro2 copied to clipboard
Return a result on `Ident::new` instead of panicking
I am working on generating code for the wayland protocol in waybackend. When creating identifiers, some of them are invalid identifiers in Rust (an enum variant that is just a number). For those, I have been using the following fallback:
// prepend an '_' if `name` is an invalid identifier
syn::parse_str(name).unwrap_or_else(|_| syn::parse_str(&format!("_{name}")).unwrap())
This is the only use of syn in waybackend-scanner. If proc_macro2 returned a Result instead of panicking outright, I could have one less dependency in my project.
I believe this could also be done for some other types as well.
Of course, this would be a breaking change, since old code would break, though you could restore the old behavior by just calling unwrap.