Comparing literal string with Ident in syn

I'm doing proc-macro-workshop, and I'm having trouble implementing this:

// If the new one-at-a-time builder method is given the same name as the field,
// avoid generating an all-at-once builder method for that field because the
// names would conflict.

In order to check it I parsed the attribute on field(#[builder(each = "method_name")]), returning ExprLit if valid attribute exists(builder/src/

pub fn get_each_from_builder_attribute(field: &Field) -> Result<Option<ExprLit>> { ... }

However, I'm not sure how I can compare this ExprLit with &field.ident.

This code below works, but I'm wondering if there is more better way. Does this involve unnecessary parsing and/or type conversion?

if let Ok(Some(ExprLit { attrs, lit })) = get_each_from_builder_attribute(field) {
    if let Lit::str(lit_str) = lit {
        eprintln("eq: {}", field.ident.as_ref().unwrap() == lit_str.value());
1 Like

It's not bad, if it compiles. Minimized as Rust Playground , where it should be &lit_str.value() instead.

lit_str.parse::<Ident>().ok() == field.ident is an option, both of which equal. Rust Playground

Edit: Well, proc-macro2::Ident PartialEq impl may also convert to Strings (without span compared), so I'd prefer yours.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.