Skip to content

Return a result on Ident::new instead of panicking #521

@LGFae

Description

@LGFae

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions