0

Okay here is an example of what I want to accomplish. I have a rigid, and complicated enum that must be parsed. The structure of the enum cant be changed.

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, Subcommand)]
#[serde(rename_all = "snake_case")]
pub enum Msg {
    SetConfig {
        set_config_msg: AnotherEnum,
    },
    AddAsset {
        asset_info_type: MyStruct,
    },
    AddNums {
        num_1: u8,
        num_2: u8
    }
}

pub struct MyStruct {
    string0: String,
    string1: String
} 

pub struct AnotherEnum {
    Variant0,
    Variant1(MyStruct),
} 

for now you can imagine AnotherEnum and SomeStruct are any arbitrary enum and struct. What I would like is to interactively go through every single field here and get user input. It would look like this:

>>> my-cli set_config
Enter set_config_msg (Variant0 or Variant1)
>>> Variant1
Enter string0:
>>> Hello
Enter string1:
>>> World
Done

Any info is super helpful. Looking for some guidance. For context I am building a very general CLI that other people will have to use with totally different structs and enums. I'm hoping the macros in Clap will be enough to get me what I need. I can't change the structure of the Msg enum, but I can add derive macros etc.

ewoolsey
  • 91
  • 1
  • 7
  • Clap probably won't be of any help. You might be able to hack something together with `serde` and `JsonSchema` though. – kmdreko Oct 16 '22 at 03:32
  • @kmdreko yeah I think I was really hoping there would be some crate that has this functionality, but I can't find anything. I think the enum_iterator trait might be helpful here for me. – ewoolsey Oct 16 '22 at 05:29
  • Strum Could also potentially be very helpful here. – ewoolsey Oct 16 '22 at 05:45

0 Answers0