cargo-msrv icon indicating copy to clipboard operation
cargo-msrv copied to clipboard

Localize config instances for subcommands

Open foresterre opened this issue 2 years ago • 0 comments

Instead of running a subcommand with:

fn run<R: Output>(&self, config: &Config, reporter: &R) -> TResult<()>;

we can do:

fn run<R: Output>(&self, reporter: &R) -> TResult<()>;

And let each subcommand take a struct with a Config for just that subcommand. By doing this, a subcommand will only have access to relevant config fields and values, and in turn, it's also easier to test, as the set of possible states becomes a lot smaller.

pub struct DoctorConfig { // possibly construct with builder
  crate_path: PathBuf,
  interactive: bool,
}

pub struct Doctor {
  config: DoctorConfig,
}

impl Doctor {
  fn from_config(config: &Config) -> Self {
    let doctor_config = DoctorConfig::from(config);

    Self {
      doctor_config,
    }
  }
}

impl SubCommand for Doctor {
    fn run<R: Output>(&self, reporter: &R) -> TResult<()> {
        show_msrv(self.config, reporter)
        
       ...
    }
}

foresterre avatar Apr 08 '22 23:04 foresterre