How to parse fn with attribute?

I want to create a new function with an attribute:

let getter_fn = quote!(
    #[getter]
    fn #get_fn(&self) -> #get_ret_value {
        self.0.#key
    }
)
.into();
ast.items
    .push(syn::parse::<syn::ImplItem>(getter_fn).expect("Failed to parse code"));

Without the attribute parse succeeds. But with the attribute I get the following error message:

message: Failed to parse code1: Error("expected one of: for, parentheses, fn, unsafe, extern, identifier, ::, <, dyn, square brackets, *, &, !, impl, _, lifetime")

How can I fix this?

I cannot immediately replicate the issue; admitted, I’ve only tried it with proc_macro2::TokenStream in a playground, though that shouldn’t make a difference, right?

use quote::quote;
fn main() {
    let get_fn = quote!(foo);
    let get_ret_value = quote!(());
    let key = quote!(x);
    let getter_fn = quote!(
        #[getter]
        fn #get_fn(&self) -> #get_ret_value {
            self.0.#key
        }
    );
    let i = syn::parse2::<syn::ImplItem>(getter_fn).expect("Failed to parse code");
    dbg!(i);
}

Rust Playground

Thank's for the quick answer. I've seen now that this is not the problem. The problem is that #get_ret_value is a TokenStream which contains a Literal::Str instead of an Ident.

As the return value I need something like:

String
&String
Vec
etc.

How can I parse a string which contains one of the above return types into a TokenStream?

Maybe just syn::parse_str?
Rust Playground