extract search module

This commit is contained in:
timeshifter
2023-01-12 12:19:34 +01:00
parent cacb240830
commit fc68560e8f
3 changed files with 86 additions and 79 deletions
+83
View File
@@ -0,0 +1,83 @@
use std::collections::HashSet;
use std::iter::Peekable;
use std::vec::IntoIter;
use anyhow::{Context, Result};
use clap::ArgMatches;
use regex::Regex;
use crate::grouping::{Group, Package, Section};
pub(crate) fn search_packages(args: &ArgMatches, groups: &HashSet<Group>) -> Result<()> {
let search_string = args
.get_one::<String>("string")
.context("getting search string from arg")?;
let re = Regex::new(search_string)?;
let mut vec = vec![];
for group in groups {
for section in &group.sections {
for package in &section.packages {
if re.is_match(&package.name) {
vec.push((group, section, package))
}
}
}
}
print_triples(vec);
Ok(())
}
fn print_triples(mut vec: Vec<(&Group, &Section, &Package)>) {
vec.sort_unstable();
let mut g0 = String::new();
let mut s0 = String::new();
let mut iter = vec.into_iter().peekable();
while let Some((g, s, p)) = iter.next() {
print_group_if_changed(g, &g0, &mut s0);
print_section_if_changed(s, &s0);
println!("{p}");
save_group_and_section_name(&mut g0, g, &mut s0, s);
print_separator_unless_exhausted(&mut iter, &g0);
}
}
fn save_group_and_section_name(g0: &mut String, g: &Group, s0: &mut String, s: &Section) {
*g0 = g.name.clone();
*s0 = s.name.clone();
}
fn print_separator_unless_exhausted(
iter: &mut Peekable<IntoIter<(&Group, &Section, &Package)>>,
g0: &String,
) {
if let Some((g, _, _)) = iter.peek() {
if g.name != *g0 {
println!();
}
}
}
fn print_section_if_changed(s: &Section, s0: &String) {
if s.name != *s0 {
println!("[{}]", s.name);
}
}
fn print_group_if_changed(g: &Group, g0: &String, s0: &mut String) {
if g.name != *g0 {
println!("{}", g.name);
for _ in 0..g.name.len() {
print!("-");
}
println!();
s0.clear();
}
}