1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-09-13 03:07:59 +00:00

print, main, error: cleanup, add docs

This commit is contained in:
Bloxx12 2025-05-06 22:17:38 +02:00
parent 0d47c30b54
commit d8f071c887
No known key found for this signature in database
3 changed files with 23 additions and 36 deletions

View file

@ -38,9 +38,6 @@ pub enum AppError {
#[error("Database error: {source}")]
DatabaseError { source: rusqlite::Error },
#[error("Nix store error: {message} for path: {store_path}")]
NixStoreError { message: String, store_path: String },
}
// Implement From traits to support the ? operator
@ -123,12 +120,4 @@ impl AppError {
context: context.into(),
}
}
/// Create a Nix store error
pub fn nix_store_error<M: Into<String>, P: Into<String>>(message: M, store_path: P) -> Self {
Self::NixStoreError {
message: message.into(),
store_path: store_path.into(),
}
}
}

View file

@ -1,11 +1,10 @@
mod print;
use clap::Parser;
use core::str;
use log::{debug, error, warn};
use log::{debug, error};
use regex::Regex;
use std::{
collections::{HashMap, HashSet},
process::Command,
string::ToString,
sync::OnceLock,
thread,
@ -93,11 +92,9 @@ fn main() {
.format_timestamp(Some(env_logger::fmt::TimestampPrecision::Seconds))
.init();
debug!("Nix available: {}", check_nix_available()); // XXX: is this supposed to be user-facing?
println!("<<< {}", args.path.to_string_lossy());
println!(">>> {}", args.path2.to_string_lossy());
// handles to the threads collecting closure size information
// We do this as early as possible because nix is slow.
let closure_size_handles = if args.closure_size {
@ -222,9 +219,9 @@ fn main() {
.max()
.unwrap_or_default();
print::print_added(added, &post, col_width);
print::print_removed(removed, &pre, col_width);
print::print_changes(changed, &pre, &post, col_width);
print::print_added(&added, &post, col_width);
print::print_removed(&removed, &pre, col_width);
print::print_changes(&changed, &pre, &post, col_width);
if let Some((pre_handle, post_handle)) = closure_size_handles {
match (pre_handle.join(), post_handle.join()) {
@ -261,7 +258,14 @@ fn store_path_regex() -> &'static Regex {
})
}
// Parse the nix store path to extract package name and version
/// Parses a nix store path to extract the packages name and version
///
/// This function first drops the inputs first 44 chars, since that is exactly the length of the /nix/store/... prefix. Then it matches that against our store path regex.
///
/// # Returns
///
/// * Result<(&'a str, &'a str)> - The Package's name and version, or an error if
/// one or both cannot be retrieved.
fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
let path = pack.into();
@ -294,17 +298,3 @@ fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
source: None,
})
}
fn check_nix_available() -> bool {
// Check if nix is available on the host system.
debug!("Checking nix command availability");
let nix_available = Command::new("nix").arg("--version").output().ok();
let nix_query_available = Command::new("nix-store").arg("--version").output().ok();
let result = nix_available.is_some() && nix_query_available.is_some();
if !result {
warn!("Nix commands not available, functionality may be limited");
}
result
}

View file

@ -2,6 +2,12 @@ use core::str;
use std::collections::{HashMap, HashSet};
use yansi::Paint;
/// diffs two strings character by character, and returns a tuple of strings
/// colored in a way to represent the differences between the two input strings.
///
/// # Returns:
///
/// * (String, String) - The differing chars being red in the left, and green in the right one.
fn diff_versions(left: &str, right: &str) -> (String, String) {
let mut prev = String::new();
let mut post = String::new();
@ -31,7 +37,8 @@ fn diff_versions(left: &str, right: &str) -> (String, String) {
(prev, post)
}
pub fn print_added(set: HashSet<&str>, post: &HashMap<&str, HashSet<&str>>, col_width: usize) {
/// print the packages added between two closures.
pub fn print_added(set: &HashSet<&str>, post: &HashMap<&str, HashSet<&str>>, col_width: usize) {
println!("{}", "Packages added:".underline().bold());
// Use sorted outpu
@ -57,7 +64,8 @@ pub fn print_added(set: HashSet<&str>, post: &HashMap<&str, HashSet<&str>>, col_
}
}
pub fn print_removed(set: HashSet<&str>, pre: &HashMap<&str, HashSet<&str>>, col_width: usize) {
/// print the packages removed between two closures.
pub fn print_removed(set: &HashSet<&str>, pre: &HashMap<&str, HashSet<&str>>, col_width: usize) {
println!("{}", "Packages removed:".underline().bold());
// Use sorted output for more predictable and readable results
@ -84,7 +92,7 @@ pub fn print_removed(set: HashSet<&str>, pre: &HashMap<&str, HashSet<&str>>, col
}
pub fn print_changes(
set: HashSet<&str>,
set: &HashSet<&str>,
pre: &HashMap<&str, HashSet<&str>>,
post: &HashMap<&str, HashSet<&str>>,
col_width: usize,