Skip to content

Add PreservedStrExpr utility wrapper - #346

Merged
TedDriggs merged 2 commits into
TedDriggs:masterfrom
m4tx:preserved-str-expr
Jun 6, 2025
Merged

TedDriggs merged 2 commits into
TedDriggs:masterfrom
m4tx:preserved-str-expr

Conversation

@m4tx

@m4tx m4tx commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

By default, darling evaluates the expression inside string literals. This can be worked around with preserve_str_literal, but sometimes it's difficult—for instance, when a struct contains HashMap<syn::Ident, syn::Expr>.

This commit introduces a utility wrapper over syn::Expr that has a simple FromMeta impl that just copies the inner Expr instance. It can be then used like so:

#[derive(FromMeta)]
#[darling(attributes(demo))]
struct Demo {
    option: Option<HashMap<syn::Ident, PreservedStrExpr>>,
}

@m4tx
m4tx force-pushed the preserved-str-expr branch from 42b8cde to 90d4105 Compare June 4, 2025 18:31
@m4tx
m4tx force-pushed the preserved-str-expr branch from 90d4105 to c01e6f6 Compare June 4, 2025 18:32
@TedDriggs

Copy link
Copy Markdown
Owner

This is very elegant.

@TedDriggs
TedDriggs merged commit e4ae877 into TedDriggs:master Jun 6, 2025
m4tx added a commit to cot-rs/cot that referenced this pull request Jul 10, 2025
Our contribution has just been released, so keeping `PreservedStrExpr`
is no longer needed (TedDriggs/darling#346).
seqre pushed a commit to cot-rs/cot that referenced this pull request Jul 10, 2025
Our contribution has just been released, so keeping `PreservedStrExpr`
is no longer needed (TedDriggs/darling#346).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants