Hi there,
I have a rich enum that I use to parse command-line arguments:
pub enum CliCommand {
Build(BuildCommand),
Create(CreateCommand),
}
pub enum BuildCommand {
LocalProject(BuildInfo),
RemoteProject(BuildInfo),
}
pub struct BuildInfo {
pub path: String,
pub debug: bool,
...
}
The important thing for me here, is that there are arguments (the BuildInfo
struct) that are nested somewhat deeply into enums. I want to be able to pull those easily without having to clutter my code with a match
forest.
My goal here is to pull the BuildInfo arguments from the nested enum as cleanly as possible while minding boilerplate. The BuildCommand
enum only has two variants but it could grow to a lot more.
I found myself writing this code:
impl BuildCommand {
pub fn args(&self) -> &BuildInfo {
match self {
BuildCommand::LocalProject(info) => info,
BuildCommand::RemoteProject(info) => info,
}
}
I think it's an okay solution but my issue is that it feels very silly to do an exhaustive match with redundant pattern. I would like to do something like this:
impl BuildCommand {
pub fn args(&self) -> &BuildInfo {
match self {
BuildCommand::*(info: BuildInfo) => info,
}
}
Basically match on the types of the parameters on the variant. That way if I add more variants, I can just add the signatures in the match tree. This looks a lot cleaner.
I don't think the above syntax is possible (I'm new to Rust) but what's the closest I get to that and what's the "idiomatic" kosher way to do this.
Thanks