Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename With Expressions #1964

Open
theduke opened this issue Jan 25, 2021 · 2 comments
Open

Rename With Expressions #1964

theduke opened this issue Jan 25, 2021 · 2 comments

Comments

@theduke
Copy link

theduke commented Jan 25, 2021

Currently rename = ? attributes are parsed as LitStr, which precludes using constant expressions instead of literals.

My concrete use case is a proc macro that needs to set renames based on an external constant.

For example:

trait Namespace {
  type NAME: &'static str;
}

#[derive(Entity, serde::Serialize)]

struct MyEntity {
  field: bool,
}

// Which would expand to:

#[derive(serde::Serialize)]
struct MyEntity {
  #[entity( namespace = SomeNamespace )]
  #[serde( rename = <SomeNamespace as Namespace>::NAME )]
  field: bool,
}

I would be happy to implement this, but I'd like to know beforehand if this would be accepted, which is probably not a given due to the added complexity.

( @dtolnay )

@Mephistophiles
Copy link

Maybe add a new attribute like rename_with? Otherwise, changing the semantics of the attribute will break backward compatibility.

What data types do you think should be allowed in this case? It seems to me better to restrict ourselves only to the associated constants.

My use case:
I wanted to write a library for working with redmine. When a group request for data, such as issues, redmine issues a request:

struct IssuesBatchResponse {
    issues: Vec<Issue>,
    total_count: u64,
    offset: u64,
    limit: u64,
}

As you can see, the 'issues' key is specified for a specific site endpoint. Other keys are given for the rest of the data types.

It would be nice to do something like the example below to reduce duplication of structures and code:

struct BatchResponse<S> {
    #[serde(rename_with = "S::NAME")]
    payload: Vec<S>,
    total_count: u64,
    offset: u64,
    limit: u64
}

@SOF3
Copy link

SOF3 commented Sep 16, 2022

a much use case case is when the struct is defined in a decl macro and the rename is the result of a concat!()/stringify!().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants