1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-27 11:47:46 +00:00

main.rs: overabstract package HashMaps

This commit is contained in:
Bloxx12 2025-05-04 16:34:12 +02:00
parent ed528573ee
commit 0c226426de
No known key found for this signature in database

View file

@ -37,20 +37,21 @@ fn main() {
if let (Ok(packages), Ok(packages2)) = (packages, packages2) { if let (Ok(packages), Ok(packages2)) = (packages, packages2) {
// Map from packages of the first closure to their version // Map from packages of the first closure to their version
let mut pre: HashMap<String, String> = HashMap::new(); let pre: HashMap<String, String> = packages
.into_iter()
.map(|p| {
let (name, version) = get_version(&*p);
(name.to_string(), version.to_string())
})
.collect();
// Map from packages of the second closure to their version let post: HashMap<String, String> = packages2
let mut post: HashMap<String, String> = HashMap::new(); .into_iter()
.map(|p| {
for p in &packages { let (name, version) = get_version(&*p);
let version = get_version(p); (name.to_string(), version.to_string())
pre.insert(version.0.to_string(), version.1.to_string()); })
} .collect();
for p in &packages2 {
let version = get_version(p);
post.insert(version.0.to_string(), version.1.to_string());
}
// Compare the package names of both versions // Compare the package names of both versions
let pre_keys: HashSet<String> = pre.clone().into_keys().collect(); let pre_keys: HashSet<String> = pre.clone().into_keys().collect();
@ -115,12 +116,12 @@ fn get_packages(path: &std::path::Path) -> Result<Vec<String>, BlaErr> {
Err(BlaErr::LolErr) Err(BlaErr::LolErr)
} }
fn get_version(pack: &str) -> (&str, &str) { fn get_version<'a>(pack: impl Into<&'a str>) -> (&'a str, &'a str) {
// funny regex to split a nix store path into its name and its version. // funny regex to split a nix store path into its name and its version.
let re = Regex::new(r"^/nix/store/[a-z0-9]+-(.+?)-([0-9].*?)$").unwrap(); let re = Regex::new(r"^/nix/store/[a-z0-9]+-(.+?)-([0-9].*?)$").unwrap();
// No cap frfr // No cap frfr
if let Some(cap) = re.captures(pack) { if let Some(cap) = re.captures(pack.into()) {
let name = cap.get(1).unwrap().as_str(); let name = cap.get(1).unwrap().as_str();
let version = cap.get(2).unwrap().as_str(); let version = cap.get(2).unwrap().as_str();
return (name, version); return (name, version);