0

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

xx11
  • 1
  • 1
  • How is `format_repo` defined? – cafce25 Mar 14 '23 at 01:43
  • 1
    Please provide a [mre] (emphasis on _reproducible_: this should be a full program that exhibits the issue, but doesn't contain any code that is not needed to reproduce it). As it is, I suspect that you're using `self.repo` directly at a place that expects an `ImageRepository`, so you would need to `unwrap` it. But I can't tell you more without a [mre]. – Jmb Mar 14 '23 at 08:02
  • i added `format_repo` – xx11 Mar 15 '23 at 19:36
  • i checked throughly and I didn't see anywhere use this `self.repo` outside this file – xx11 Mar 15 '23 at 19:38
  • `format_repo` should return a `Result`, not `Result – Jmb Mar 16 '23 at 07:22

0 Answers0