mirror of
https://github.com/RGBCube/dix
synced 2025-09-13 11:17:58 +00:00
print, main, error: cleanup, add docs
This commit is contained in:
parent
0d47c30b54
commit
d8f071c887
3 changed files with 23 additions and 36 deletions
11
src/error.rs
11
src/error.rs
|
@ -38,9 +38,6 @@ pub enum AppError {
|
||||||
|
|
||||||
#[error("Database error: {source}")]
|
#[error("Database error: {source}")]
|
||||||
DatabaseError { source: rusqlite::Error },
|
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
|
// Implement From traits to support the ? operator
|
||||||
|
@ -123,12 +120,4 @@ impl AppError {
|
||||||
context: context.into(),
|
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(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -1,11 +1,10 @@
|
||||||
mod print;
|
mod print;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use core::str;
|
use core::str;
|
||||||
use log::{debug, error, warn};
|
use log::{debug, error};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
process::Command,
|
|
||||||
string::ToString,
|
string::ToString,
|
||||||
sync::OnceLock,
|
sync::OnceLock,
|
||||||
thread,
|
thread,
|
||||||
|
@ -93,11 +92,9 @@ fn main() {
|
||||||
.format_timestamp(Some(env_logger::fmt::TimestampPrecision::Seconds))
|
.format_timestamp(Some(env_logger::fmt::TimestampPrecision::Seconds))
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
debug!("Nix available: {}", check_nix_available()); // XXX: is this supposed to be user-facing?
|
|
||||||
println!("<<< {}", args.path.to_string_lossy());
|
println!("<<< {}", args.path.to_string_lossy());
|
||||||
println!(">>> {}", args.path2.to_string_lossy());
|
println!(">>> {}", args.path2.to_string_lossy());
|
||||||
|
|
||||||
|
|
||||||
// handles to the threads collecting closure size information
|
// handles to the threads collecting closure size information
|
||||||
// We do this as early as possible because nix is slow.
|
// We do this as early as possible because nix is slow.
|
||||||
let closure_size_handles = if args.closure_size {
|
let closure_size_handles = if args.closure_size {
|
||||||
|
@ -222,9 +219,9 @@ fn main() {
|
||||||
.max()
|
.max()
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
print::print_added(added, &post, col_width);
|
print::print_added(&added, &post, col_width);
|
||||||
print::print_removed(removed, &pre, col_width);
|
print::print_removed(&removed, &pre, col_width);
|
||||||
print::print_changes(changed, &pre, &post, col_width);
|
print::print_changes(&changed, &pre, &post, col_width);
|
||||||
|
|
||||||
if let Some((pre_handle, post_handle)) = closure_size_handles {
|
if let Some((pre_handle, post_handle)) = closure_size_handles {
|
||||||
match (pre_handle.join(), post_handle.join()) {
|
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)> {
|
fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
|
||||||
let path = pack.into();
|
let path = pack.into();
|
||||||
|
|
||||||
|
@ -294,17 +298,3 @@ fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
|
||||||
source: None,
|
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
|
|
||||||
}
|
|
||||||
|
|
14
src/print.rs
14
src/print.rs
|
@ -2,6 +2,12 @@ use core::str;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use yansi::Paint;
|
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) {
|
fn diff_versions(left: &str, right: &str) -> (String, String) {
|
||||||
let mut prev = String::new();
|
let mut prev = String::new();
|
||||||
let mut post = String::new();
|
let mut post = String::new();
|
||||||
|
@ -31,7 +37,8 @@ fn diff_versions(left: &str, right: &str) -> (String, String) {
|
||||||
(prev, post)
|
(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());
|
println!("{}", "Packages added:".underline().bold());
|
||||||
|
|
||||||
// Use sorted outpu
|
// 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());
|
println!("{}", "Packages removed:".underline().bold());
|
||||||
|
|
||||||
// Use sorted output for more predictable and readable results
|
// 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(
|
pub fn print_changes(
|
||||||
set: HashSet<&str>,
|
set: &HashSet<&str>,
|
||||||
pre: &HashMap<&str, HashSet<&str>>,
|
pre: &HashMap<&str, HashSet<&str>>,
|
||||||
post: &HashMap<&str, HashSet<&str>>,
|
post: &HashMap<&str, HashSet<&str>>,
|
||||||
col_width: usize,
|
col_width: usize,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue