udoprog/genco

Generating Rust attribute, string with import

Opened this issue · 1 comments

I'm trying to generate the following attribute on a struct field, which contains an import-able symbol DurationSeconds:

#[serde_with]
pub struct MyStruct {
    #[serde_as(as = "Option<DurationSeconds>")]
    duration: Option<Duration>,
}

Here's what I've tried:

let duration_seconds = rust::import("serde_with", "DurationSeconds");
quote_in! { tokens =>
    #[serde_as(as = $[str](Option<$duration_seconds>))]
}

The error I get is cryptic to me:

error[E0599]: the method `lang_supports_eval` exists for mutable reference `&mut Tokens<Rust>`, but its trait bounds were not satisfied
   --> packages/codegen/src/generator.rs:38:13
    |
38  |               quote_in! { tokens =>
    |  _____________^
39  | |                 #[serde_as(as = $[str](Option<$duration_seconds>))]
40  | |             }
    | |_____________^ method cannot be called on `&mut Tokens<Rust>` due to unsatisfied trait bounds
    |
   ::: /Users/sirius/.cargo/registry/src/index.crates.io-6f17d22bba15001f/genco-0.17.8/src/lang/rust.rs:51:1
    |
51  | / impl_lang! {
52  | |     /// Language specialization for Rust.
53  | |     pub Rust {
54  | |         type Config = Config;
...   |
137 | |     }
138 | | }
    | |_- doesn't satisfy `Rust: LangSupportsEval`
    |
    = note: the following trait bounds were not satisfied:
            `Rust: LangSupportsEval`
    = note: this error originates in the macro `quote_in` (in Nightly builds, run with -Z macro-backtrace for more info)

I take it this has to do with not being able to nest the string literal? I've also tried wrapping it with parens, and a few other things, nothings seems to work.

The error means that Rust doesn't support dynamic string interpolation. As per the documentation, try doing this instead: $[str](Option<$duration_seconds>))].