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

src/main.rs: cleanup

This commit is contained in:
Bloxx12 2025-05-04 16:09:30 +02:00
parent 53179520b8
commit ed528573ee
No known key found for this signature in database
2 changed files with 59 additions and 63 deletions

View file

@ -7,3 +7,5 @@ edition = "2024"
clap = { version = "4.5.37", features = ["derive"] }
regex = "1.11.1"
[workspace.lints.clippy]
pedantic = { level = "warn", priority = -1 }

View file

@ -3,9 +3,8 @@ use core::str;
use regex::Regex;
use std::{
collections::{HashMap, HashSet},
hash::Hash,
process::Command,
string::String,
string::{String, ToString},
};
#[derive(Parser, Debug)]
@ -19,12 +18,6 @@ struct Args {
paths: bool,
}
#[derive(Debug, PartialEq, PartialOrd, Eq, Clone, Hash)]
struct Package<'a> {
name: &'a str,
version: &'a str,
}
// Only there to make the compiler shut up for now.
#[derive(Debug)]
enum BlaErr {
@ -39,37 +32,42 @@ fn main() {
println!("<<< {}", args.path.to_string_lossy());
println!(">>> {}", args.path2.to_string_lossy());
if check_if_system(&args.path) && check_if_system(&args.path2) {
let packages = get_packages(&args.path);
let packages2 = get_packages(&args.path2);
if let (Ok(packages), Ok(packages2)) = (packages, packages2) {
// Map from packages of the first closure to their version
let mut pre: HashMap<String, String> = HashMap::new();
// Map from packages of the second closure to their version
let mut post: HashMap<String, String> = HashMap::new();
for p in packages.iter() {
for p in &packages {
let version = get_version(p);
pre.insert(version.0.to_string(), version.1.to_string());
}
for p in packages2.iter() {
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
let pre_keys: HashSet<String> = pre.clone().into_keys().collect();
let post_keys: HashSet<String> = post.clone().into_keys().collect();
// get the intersection of the package names for version changes
let maybe_changed: HashSet<_> = pre_keys.intersection(&post_keys).collect();
// difference gives us added and removed packages
let added: HashSet<String> = &post_keys - &pre_keys;
let removed: HashSet<String> = &pre_keys - &post_keys;
let maybe_changed: HashSet<_> = pre_keys.intersection(&post_keys).collect();
println!("Difference between the two generations:");
println!("Packages added: ");
for p in added {
let version = post.get(&p);
if let Some(ver) = version {
println!("A: {} @ {}", p, ver);
println!("A: {p} @ {ver}");
}
}
println!();
@ -77,7 +75,7 @@ fn main() {
for p in removed {
let version = pre.get(&p);
if let Some(ver) = version {
println!("R: {} @ {}", p, ver);
println!("R: {p} @ {ver}");
}
}
println!();
@ -91,21 +89,15 @@ fn main() {
if let (Some(ver_pre), Some(ver_post)) = (version_pre, version_post) {
if ver_pre != ver_post {
println!("C: {} @ {} -> {}", p, ver_pre, ver_post);
println!("C: {p} @ {ver_pre} -> {ver_post}");
}
}
}
}
} else {
println!("One of them is not a system!")
}
}
fn check_if_system(path: &std::path::Path) -> bool {
path.join("activate").exists()
}
fn get_packages(path: &std::path::Path) -> Result<Vec<String>, BlaErr> {
// get the nix store paths using nix-store --query --references <path>
let references = Command::new("nix-store")
.arg("--query")
.arg("--references")
@ -116,7 +108,7 @@ fn get_packages(path: &std::path::Path) -> Result<Vec<String>, BlaErr> {
let list = str::from_utf8(&query.stdout);
if let Ok(list) = list {
let res: Vec<String> = list.lines().map(|s| s.to_string()).collect();
let res: Vec<String> = list.lines().map(ToString::to_string).collect();
return Ok(res);
}
}
@ -124,6 +116,7 @@ fn get_packages(path: &std::path::Path) -> Result<Vec<String>, BlaErr> {
}
fn get_version(pack: &str) -> (&str, &str) {
// 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();
// No cap frfr
@ -137,6 +130,7 @@ fn get_version(pack: &str) -> (&str, &str) {
}
fn check_nix_available() -> bool {
// Check if nix is available on the host system.
let nix_available = Command::new("nix").arg("--version").output().ok();
let nix_query_available = Command::new("nix-store").arg("--version").output().ok();