Search code examples
rustserializationdeserialization

Is it possible to find unused enum variants?


I am currently working on a project where I am serializing certain variants of an enum to prepare for an HTTP request. I would like to know if there is a way to identify which variants of the enum are not being used in this process, as I would like to potentially remove them in order to keep my code clean and organized. The enum is not matched anywhere in the code. Is there a specific method or tool that can be used to achieve this? Thank you for your time and help.

For example all but "UseCase4" are in use:

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum UseCases {
    UseCase1,
    UseCase2,
    UseCase3,
    UseCase4,
}

One potential solution would be to find an IDE extension that can assist with this task. I am currently using VS Code and IntelliJ IDEA, so a solution that works with either of these IDEs would be ideal.

I could not find any information.


Solution

  • There is no way to do this automatically when using dynamic data. The best you can do is look at your input data and determine what possibilities exist in it and check manually. This is a limitation of any deserialization library in any language.

    If it is highly structured data and has a specification, you may be able to write a small tool that scans the specification and pulls out the possible values yourself.

    Good luck.