How to parse `LitStr` into `WherePredicate` in proc macro?

use syn::parse_quote;
use syn::WherePredicate;

// `bound_value` is a LitStr, "T::Value: Debug".
let parsed_wp:WherePredicate = parse_quote!(#bound_value);
// this raises error.
let parsed_wp:WherePredicate = parse_quote!(T::Value: Debug);
// But this works

The bound_value is from a attribute, #[debug(bound = "T::Value: Debug")].

for attr in &derive_input.attrs {
        if attr.path().is_ident("debug") {
            attr.parse_nested_meta(|meta| {
                if meta.path.is_ident("bound") {
                    let value = meta.value()?;
                    let bound_value = value.parse::<LitStr>()?;
//                    eprintln!("bound_value={:#?}", bound_value);

                    let parsed_wp:WherePredicate = parse_quote!(#bound_value);
                } else {
                    Err(meta.error("Invalid path"))

How to parse it properly?

Thanks in advance.

You can use parse from LitStr.

let parsed_wp: WherePredicate = bound_value.parse()?;
1 Like

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.