I'm trying to create a command option repo
, and make it an Option
type:
#[derive(Clone, Debug)]
pub enum ImageRepository {
repoA,
repoB,
repoC,
}
fn format_repo(repo: &str) -> Result<Option<ImageRepository>> {
let repo = repo.to_lowercase().to_string();
match repo.as_str() {
"A" => Ok(Some(ImageRepository::repoA)),
"B" => Ok(Some(ImageRepository::repoB)),
"C" => Ok(Some(ImageRepository::repoC)),
_ => Err(anyhow!("the repo option has to be among (A, B, C)"))
}
}
/// Subcommand to list all images
#[derive(Clone, Debug, Parser)]
pub struct ImageArg {
#[clap(short, long, value_parser = format_repo, default_value = None)]
repo: Option<ImageRepository>,
}
and then i parse the option like this:
#[async_trait]
impl common::RunCommand for ImageArg {
async fn run_command(&self) -> Result<()> {
match self.repo {
Some(ImageRepository::repoA) => Ok(()),
Some(ImageRepository::repoB) => Ok(()),
Some(ImageRepository::repoB) => Ok(()),
None => Err(anyhow!("method to be implemented that go with all three repos")),
}
}
}
basically what I want to achieve is that when user specify this option, then go with specific enum value(repoA, repoB or repoC), but if user omit this option, I'll go with all enum values. when I try to run the command, I got the error like this:
thread 'main' panicked at 'Mismatch between definition and access of `repo`. Could not downcast to mushu::artifact::repo::ImageRepository, need to downcast to core::option::Option<mushu::artifact::repo::ImageRepository>
Not sure where the downcast happens here and what goes wrong, can anyone point out?
I tried to change it to:
#[async_trait]
impl common::RunCommand for ImageArg {
async fn run_command(&self) -> Result<()> {
let repo = &self.repo;
if let Some(repo) = repo {
match repo {
ImageRepository::repoA => Ok(()),
ImageRepository::repoB => Ok(()),
ImageRepository::repoC => Ok(()),
}
} else {
Err(anyhow!("method to be implemented that go with all three repos"))
}
}
}
but has no luck