diff --git a/src/main.rs b/src/main.rs index 6d1afec..8cff829 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,4 @@ -use nix::fcntl::FdFlag; -use nix::unistd::{close, pipe}; -use nix::Error as NixError; -use std::io::Read; -use std::os::fd::FromRawFd; use std::path::PathBuf; -use std::process::Stdio; // For Stdio::null, Stdio::piped use anyhow::{anyhow, Context, Error, Result}; use clap::error::ErrorKind; diff --git a/src/modes/diff.rs b/src/modes/diff.rs index 67f3fe3..670d3ae 100644 --- a/src/modes/diff.rs +++ b/src/modes/diff.rs @@ -1,22 +1,13 @@ use crate::compression::CompressionType; -use crate::modes::common::format_size; -use crate::modes::common::get_format_box_chars_no_border_line_separator; -use crate::modes::common::ColumnType; -use crate::db::{get_item, get_item_last, get_item_matching}; -use crate::modes::common::format_size; +use crate::modes::common::{format_size, ColumnType}; use std::path::PathBuf; use std::str::FromStr; -use anyhow::{anyhow, Context, Error, Result}; +use anyhow::{anyhow, Error, Result}; use clap::Command; -use is_terminal::IsTerminal; use nix::fcntl::FdFlag; use nix::unistd::{close, pipe}; use nix::Error as NixError; -use prettytable::format; -use prettytable::{Attr, Cell, Row, Table}; -use std::collections::HashMap; -use std::fs; use std::io::Read; use std::os::fd::FromRawFd; use std::process::Stdio; @@ -31,7 +22,7 @@ pub fn mode_diff( ) -> Result<()> { if !tags.is_empty() { cmd.error( - clap::error::ErrorKind::InvalidValue, + ErrorKind::InvalidValue, "Tags are not supported with --diff. Please provide exactly two IDs.", ) .exit(); @@ -45,20 +36,20 @@ pub fn mode_diff( } // Fetch items, ensuring they exist. - let item_a = db::get_item(conn, ids[0])? + let item_a = crate::db::get_item(conn, ids[0])? .ok_or_else(|| anyhow!("Unable to find first item (ID: {}) in database", ids[0]))?; - let item_b = db::get_item(conn, ids[1])? + let item_b = crate::db::get_item(conn, ids[1])? .ok_or_else(|| anyhow!("Unable to find second item (ID: {}) in database", ids[1]))?; - debug!("MAIN: Found item A {:?}", item_a); - debug!("MAIN: Found item B {:?}", item_b); + log::debug!("MAIN: Found item A {:?}", item_a); + log::debug!("MAIN: Found item B {:?}", item_b); - let item_a_tags: Vec = db::get_item_tags(conn, &item_a)? + let item_a_tags: Vec = crate::db::get_item_tags(conn, &item_a)? .into_iter() .map(|x| x.name) .collect(); - let item_b_tags: Vec = db::get_item_tags(conn, &item_b)? + let item_b_tags: Vec = crate::db::get_item_tags(conn, &item_b)? .into_iter() .map(|x| x.name) .collect(); @@ -94,7 +85,7 @@ pub fn mode_diff( ) .map_err(|e| anyhow!("Failed to set FD_CLOEXEC on fd_b_write: {}", e))?; - debug!("MAIN: Creating child process for diff"); + log::debug!("MAIN: Creating child process for diff"); let mut diff_command = std::process::Command::new("diff"); diff_command .arg("-u") @@ -132,7 +123,7 @@ pub fn mode_diff( .take() .expect("BUG: Failed to capture diff stderr pipe"); - debug!("MAIN: Creating threads for diff I/O"); + log::debug!("MAIN: Creating threads for diff I/O"); // Thread to write Item A data to diff let writer_thread_a = { @@ -145,7 +136,7 @@ pub fn mode_diff( // This matches that style. For more robust error handling, return Result from thread. use std::io::BufWriter; let mut buffered_pipe_writer_a = BufWriter::new(pipe_writer_a_raw); - let engine_a = compression::get_engine(compression_type_a_clone) + let engine_a = crate::compression::get_engine(compression_type_a_clone) .expect("Unable to get compression engine for Item A"); debug!("THREAD_A: Sending item A to diff"); engine_a @@ -163,8 +154,8 @@ pub fn mode_diff( let item_path_b_clone = item_path_b.clone(); let compression_type_b_clone = compression_type_b.clone(); std::thread::spawn(move || { - let mut buffered_pipe_writer_b = BufWriter::new(pipe_writer_b_raw); - let engine_b = compression::get_engine(compression_type_b_clone) + let mut buffered_pipe_writer_b = std::io::BufWriter::new(pipe_writer_b_raw); + let engine_b = crate::compression::get_engine(compression_type_b_clone) .expect("Unable to get compression engine for Item B"); debug!("THREAD_B: Sending item B to diff"); engine_b @@ -177,7 +168,7 @@ pub fn mode_diff( // Thread to read diff's standard output let stdout_reader_thread = std::thread::spawn(move || { let mut output_buffer = Vec::new(); - debug!("STDOUT_READER: Reading diff stdout"); + log::debug!("STDOUT_READER: Reading diff stdout"); // child_stdout_pipe is a ChildStdout, which implements std::io::Read child_stdout_pipe .read_to_end(&mut output_buffer) @@ -188,7 +179,7 @@ pub fn mode_diff( // Thread to read diff's standard error let stderr_reader_thread = std::thread::spawn(move || { let mut error_buffer = Vec::new(); - debug!("STDERR_READER: Reading diff stderr"); + log::debug!("STDERR_READER: Reading diff stderr"); child_stderr_pipe .read_to_end(&mut error_buffer) .map_err(|e| anyhow!("Failed to read diff stderr: {}", e)) @@ -196,7 +187,7 @@ pub fn mode_diff( }); // Wait for writer threads to complete (meaning all input has been sent to diff) - debug!("MAIN: Waiting on writer thread for item A"); + log::debug!("MAIN: Waiting on writer thread for item A"); if let Err(panic_payload) = writer_thread_a.join() { // Propagate panic from writer thread return Err(anyhow!( @@ -205,9 +196,9 @@ pub fn mode_diff( panic_payload )); } - debug!("MAIN: Writer thread for item A completed."); + log::debug!("MAIN: Writer thread for item A completed."); - debug!("MAIN: Waiting on writer thread for item B"); + log::debug!("MAIN: Waiting on writer thread for item B"); if let Err(panic_payload) = writer_thread_b.join() { return Err(anyhow!( "Writer thread for item B (ID: {}) panicked: {:?}", @@ -215,16 +206,16 @@ pub fn mode_diff( panic_payload )); } - debug!("MAIN: Writer thread for item B completed."); - debug!("MAIN: Done waiting on input-writer threads."); + log::debug!("MAIN: Writer thread for item B completed."); + log::debug!("MAIN: Done waiting on input-writer threads."); // Now that all input has been sent and input pipes will be closed by threads exiting, // wait for the diff child process to terminate. - debug!("MAIN: Waiting for diff child process to finish..."); + log::debug!("MAIN: Waiting for diff child process to finish..."); let diff_status = child_process .wait() .map_err(|e| anyhow!("Failed to wait on diff command: {}", e))?; - debug!( + log::debug!( "MAIN: Diff child process finished with status: {}", diff_status ); @@ -253,7 +244,7 @@ pub fn mode_diff( match diff_status.code() { Some(0) => { // Exit code 0: No differences - debug!("MAIN: Diff successful, no differences found."); + log::debug!("MAIN: Diff successful, no differences found."); // Typically, diff -u doesn't print to stdout if no differences. // But if it did, it would be shown here. if !stdout_capture_result.is_empty() { @@ -262,7 +253,7 @@ pub fn mode_diff( } Some(1) => { // Exit code 1: Differences found - debug!("MAIN: Diff successful, differences found."); + log::debug!("MAIN: Diff successful, differences found."); println!("{}", String::from_utf8_lossy(&stdout_capture_result)); } Some(error_code) => {