diff --git a/harmony_cli/src/lib.rs b/harmony_cli/src/lib.rs index a470822..dc125fe 100644 --- a/harmony_cli/src/lib.rs +++ b/harmony_cli/src/lib.rs @@ -55,32 +55,24 @@ pub async fn init( let scores_read = scores.read().expect("Should be able to read scores"); let scores_read_vec: Vec>> = scores_read.iter().map(|s| s.clone_box()).collect(); + let filtered: Vec<&Box>> = 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()), } }