refactor with iter and filter

This commit is contained in:
tahahawa 2025-04-11 16:13:21 -04:00 committed by taha
parent fbea2a40c3
commit 8cc5880135

View File

@ -55,32 +55,24 @@ pub async fn init<T: Topology + std::fmt::Debug + Send + Sync + 'static>(
let scores_read = scores.read().expect("Should be able to read scores");
let scores_read_vec: Vec<Box<dyn Score<T>>> =
scores_read.iter().map(|s| s.clone_box()).collect();
let filtered: Vec<&Box<dyn Score<T>>> = scores_read_vec
.iter()
.filter(|s| s.name().contains(&args.run))
.collect();
if filtered.len() == 0 {
return Err("No score containing query found".into());
}
if args.all {
for s in scores_read_vec {
if s.name().contains(&args.run) {
println!("Running: {}", s.clone_box().name());
}
for s in filtered {
println!("Running: {}", s.clone_box().name());
}
} else {
let mut count = 0;
let mut found = false;
for s in scores_read_vec {
if s.name().contains(&args.run) {
found = true;
if count < args.number {
// Skip scores that aren't the nth specified
count = count + 1;
continue;
} else {
// Run nth score found
println!("Running: {}", s.clone_box().name());
break;
}
}
}
if !found {
println!("Couldn't find score by that name")
let score = filtered.get(args.number as usize);
match score {
Some(s) => println!("Running: {}", s.clone_box().name()),
None => return Err("Invalid index given".into()),
}
}