Skip to content

Commit

Permalink
take Criterion<'a> as &'a self
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed Jan 30, 2023
1 parent 386ee88 commit 1ef3c44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,30 @@ pub struct Args {
}

impl Args {
pub fn criterion(self) -> Criterion {
pub fn criterion(&self) -> Criterion {
match self {
_ if self.stamp => Criterion::Stamp,
_ if self.file => Criterion::File,
_ if self.installed => Criterion::Installed,
_ if !self.toolchains.is_empty() => Criterion::Toolchains(self.toolchains),
_ if !self.toolchains.is_empty() => Criterion::Toolchains(&self.toolchains),
Self {
time: Some(time), ..
} => Criterion::Time(time),
} => Criterion::Time(*time),
Self {
maxsize: Some(size),
..
} => Criterion::MaxSize(size),
} => Criterion::MaxSize(*size),
_ => unreachable!("guaranteed by clap ArgGroup"),
}
}
}

pub enum Criterion {
pub enum Criterion<'a> {
Stamp,
File,
Time(u64),
Installed,
Toolchains(Vec<String>),
Toolchains(&'a [String]),
MaxSize(u64),
}

Expand Down
28 changes: 11 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod fingerprint;
mod stamp;
mod util;

use self::cli::{Args, Criterion};
use self::cli::Criterion;
use self::fingerprint::{remove_not_built_with, remove_older_than, remove_older_until_fits};
use self::stamp::Timestamp;
use self::util::format_bytes;
Expand Down Expand Up @@ -132,32 +132,26 @@ fn metadata(path: &Path) -> Result<Metadata, Error> {
fn main() -> anyhow::Result<()> {
let args = cli::parse();

let Args {
ref path,
dry_run,
hidden,
recursive,
verbose,
..
} = args;
let dry_run = args.dry_run;
let criterion = args.criterion();
setup_logging(args.verbose);

// Default to current invocation path.
let path = path
.clone()
let path = args
.path
.as_ref()
.cloned()
.unwrap_or_else(|| env::current_dir().expect("Failed to get current directory"));

let criterion = args.criterion();
setup_logging(verbose);

if let Criterion::Stamp = criterion {
debug!("Writing timestamp file in: {:?}", path);
return Timestamp::new()
.store(path.as_path())
.context("Failed to write timestamp file");
}

let paths = if recursive {
find_cargo_projects(&path, hidden)
let paths = if args.recursive {
find_cargo_projects(&path, args.hidden)
} else {
let metadata = metadata(&path).context(format!(
"Failed to gather metadata for {:?}",
Expand All @@ -173,7 +167,7 @@ fn main() -> anyhow::Result<()> {

let toolchains = match &criterion {
Criterion::Installed => Some(vec![]),
Criterion::Toolchains(vec) => Some(vec).cloned(),
Criterion::Toolchains(vec) => Some(vec.to_vec()),
_ => None,
};
if let Some(toolchains) = toolchains {
Expand Down

0 comments on commit 1ef3c44

Please sign in to comment.