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

Disable code generation for a specific strum derive macro #413

Open
Archisman-Mridha opened this issue Feb 10, 2025 · 2 comments
Open

Disable code generation for a specific strum derive macro #413

Archisman-Mridha opened this issue Feb 10, 2025 · 2 comments

Comments

@Archisman-Mridha
Copy link

I have a scenario like this :

use strum_macros::{Display, EnumString};

#[derive(Debug, PartialEq, Display, EnumString)]
pub enum Token {
  /// A numeric string, with digits, decimal points, and/or exponents.
  /// Leading signs (e.g. -) are separate tokens.
  #[strum(serialize = "{0}")]
  Number(String),

  /// A Unicode string, with quotes stripped and escape sequences resolved.
  #[strum(serialize = "{0}")]
  String(String),

  /// An identifier, with any quotes stripped.
  #[strum(serialize = "{0}")]
  Identifier(String),

  #[strum(serialize = "?")]
  Question,

  #[strum(serialize = "=")]
  Equal,

  ....

I want Display to be applied for all the enum variants, but EnumString to be applied for all except Token::Number, Token::String and Token::Identifier.

Currently we have the disabled strum attribute, using which we can disable code generation for an enum variant, for all strum derive macros.

Can we have an attribute which disables code generation specific to a strum derive macro please (in this case : EnumString) ? So the resulting code will look like :

use strum_macros::{Display, EnumString};

#[derive(Debug, PartialEq, Display, EnumString)]
pub enum Token {
  /// A numeric string, with digits, decimal points, and/or exponents.
  /// Leading signs (e.g. -) are separate tokens.
  #[strum(serialize = "{0}", disable_enum_string)]
  Number(String),

  /// A Unicode string, with quotes stripped and escape sequences resolved.
  #[strum(serialize = "{0}", disable_enum_string)]
  String(String),

  /// An identifier, with any quotes stripped.
  #[strum(serialize = "{0}", disable_enum_string)]
  Identifier(String),

  #[strum(serialize = "?")]
  Question,

  #[strum(serialize = "=")]
  Equal,

  ....

Also, looking forward to #258 getting merged, so I can do strum(transparent) instead of strum(serialize = "{0}") :).

@Peternator7
Copy link
Owner

Hi @Archisman-Mridha, I have a rough idea in my head to make attributes scope-able to specific macros which would solve your problem, but not a lot of time to develop on strum at the moment unfortunately. I think it's a good idea, but I can't say it'll get implemented any time soon. Sorry about that :/

#258 is merged though :)

@Archisman-Mridha
Copy link
Author

Hi @Archisman-Mridha, I have a rough idea in my head to make attributes scope-able to specific macros which would solve your problem, but not a lot of time to develop on strum at the moment unfortunately. I think it's a good idea, but I can't say it'll get implemented any time soon. Sorry about that :/

#258 is merged though :)

You can assign this to me .... I'll give it a shot if I get time....

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

No branches or pull requests

2 participants