Use enum in parent's module enum variant

Hello, everyone!
I'm trying to imitate a CLI by implementing some of my own commands in Rust. I have a submodule called note which has this enum:

pub enum Command {

and I want to include these variants into another enum in my main module, something that would look like this:

use crate::to_do::note;

enum Functions {
    NotesCmd(note::Command),  //note::Command being the first enum written above.

and later on, use it like this:

let command = match args[1].as_str() {
    "get" => Functions::NotesCmd(note::Command::Get),

How can I get this to work? Thanks.

If I understand your question, this is what you are looking for. It's case sensitive.

"get" => Functions::NotesCmd( note::Command::Get )

Thank you, that was totally foolish from my part, didn't even notice.
Is this approach good enough? because I feel like it's not too readable.

You might want to look into a library like clap for making a good CLI. I have found it to be very convenient. It will get you decent validations out of the box rather than having to parse the command line manually.

In terms of readability, I'm not sure why you use a nested enum, but it might be the right choice. It just doesn't seem to be represented in your command line if you just take "get". I would have expected to see maybe get_note or cmd note get if there are other contexts than note.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.