Skip to content

Commit 8397649

Browse files
committed
refactor: introduce vecs_eq helper; simplify experiment selection
1 parent 1002eef commit 8397649

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/main.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use experiments::{all_experiments, Experiment};
3838
use inquire::Confirm;
3939
use tracing::{info, warn};
4040
use tracing_subscriber::{fmt, prelude::*};
41-
use utils::{System, Worker};
41+
use utils::{vecs_eq, System, Worker};
4242

4343
/// A command-line utility to install modern Rust-based replacements of essential
4444
/// packages such as coreutils, findutils, diffutils and sudo and make them the
@@ -115,7 +115,7 @@ fn main() -> Result<()> {
115115
);
116116

117117
// Get selected experiments from the command line arguments
118-
let selected = get_selected_experiments(args.all, args.experiments.clone(), &system);
118+
let selected = selected_experiments(args.all, args.experiments.clone(), &system);
119119

120120
// Handle subcommands
121121
match args.cmd {
@@ -135,26 +135,23 @@ fn main() -> Result<()> {
135135
}
136136

137137
/// Get selected experiments from the command line arguments.
138-
fn get_selected_experiments<'a>(
138+
fn selected_experiments<'a>(
139139
all: bool,
140140
selected: Vec<String>,
141141
system: &'a impl Worker,
142142
) -> Vec<Experiment<'a>> {
143143
let all_experiments = all_experiments(system);
144+
let default_experiments = default_experiments();
144145

145146
match all {
146147
true => {
147-
let mut selected = selected.clone();
148-
selected.sort();
149-
if selected.len() > 0 && selected != default_experiments() {
148+
if selected.len() > 0 && !vecs_eq(selected, default_experiments) {
150149
warn!("Ignoring --experiments flag as --all is set");
151150
}
152151

153152
all_experiments
154153
}
155154
false => {
156-
let default_experiments = vec!["coreutils".to_string(), "sudo-rs".to_string()];
157-
158155
// If no experiments are selected, default to coreutils and sudo-rs
159156
let filter = match selected.len() {
160157
0 => default_experiments,

src/utils/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mod command;
22
mod worker;
33

4+
use std::collections::HashSet;
5+
use std::hash::Hash;
6+
47
pub use command::*;
58
pub use worker::*;
69

@@ -15,3 +18,15 @@ pub struct Distribution {
1518
pub id: String,
1619
pub release: String,
1720
}
21+
22+
/// Return true if the two (potentially unordered) vecs contain identical elements.
23+
pub fn vecs_eq<T>(v1: Vec<T>, v2: Vec<T>) -> bool
24+
where
25+
T: Hash + Eq,
26+
{
27+
if v1.len() != v2.len() {
28+
return false;
29+
}
30+
let hs: HashSet<_> = v1.iter().collect();
31+
v2.iter().all(|i| hs.contains(i))
32+
}

0 commit comments

Comments
 (0)