Clean up low-severity code quality issues

- Share IMAGE_EXTENSIONS between discovery.rs and watcher.rs (DRY)
- Extract compute_renamed_path() to deduplicate ~100 lines in executor
- Extract estimate_text_dimensions() to deduplicate watermark calc (3 copies)
- Fix encoder fallback defaults: WebP 85, AVIF 63 (match QualityPreset::High)
- Extract watch_config_dir() and load_watches() helpers in CLI (4 copies)
- Remove redundant else branches after unwrap_or_default()
- Rename misleading chrono_timestamp() to unix_timestamp()
This commit is contained in:
2026-03-08 00:22:24 +02:00
parent 7e5d19ab03
commit 8d754017fa
6 changed files with 97 additions and 133 deletions

View File

@@ -9,6 +9,20 @@ use pixstrip_core::types::*;
use std::io::Write;
use std::path::PathBuf;
fn watch_config_dir() -> PathBuf {
dirs::config_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".config")))
.unwrap_or_else(|| std::env::temp_dir())
.join("pixstrip")
}
fn load_watches(watches_path: &std::path::Path) -> Vec<pixstrip_core::watcher::WatchFolder> {
std::fs::read_to_string(watches_path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
}
#[derive(Parser)]
#[command(name = "pixstrip")]
#[command(about = "Batch image processor - resize, convert, compress, strip metadata, watermark, and rename")]
@@ -440,7 +454,7 @@ fn cmd_process(args: CmdProcessArgs) {
// Save to history - use actual output paths from the executor
let history = HistoryStore::new();
if let Err(e) = history.add(pixstrip_core::storage::HistoryEntry {
timestamp: chrono_timestamp(),
timestamp: unix_timestamp(),
input_dir: input_dir.canonicalize().unwrap_or_else(|_| input_dir.to_path_buf()).to_string_lossy().into(),
output_dir: output_dir.canonicalize().unwrap_or_else(|_| output_dir.to_path_buf()).to_string_lossy().into(),
preset_name: args.preset,
@@ -640,19 +654,9 @@ fn cmd_watch_add(path: &str, preset_name: &str, recursive: bool) {
};
// Store in config
let config_dir = dirs::config_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".config")))
.unwrap_or_else(|| std::env::temp_dir())
.join("pixstrip");
let config_dir = watch_config_dir();
let watches_path = config_dir.join("watches.json");
let mut watches: Vec<pixstrip_core::watcher::WatchFolder> = if watches_path.exists() {
std::fs::read_to_string(&watches_path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
} else {
Vec::new()
};
let mut watches = load_watches(&watches_path);
// Don't add duplicate paths
if watches.iter().any(|w| w.path == watch.path) {
@@ -682,20 +686,10 @@ fn cmd_watch_add(path: &str, preset_name: &str, recursive: bool) {
}
fn cmd_watch_list() {
let config_dir = dirs::config_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".config")))
.unwrap_or_else(|| std::env::temp_dir())
.join("pixstrip");
let config_dir = watch_config_dir();
let watches_path = config_dir.join("watches.json");
let watches: Vec<pixstrip_core::watcher::WatchFolder> = if watches_path.exists() {
std::fs::read_to_string(&watches_path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
} else {
Vec::new()
};
let watches = load_watches(&watches_path);
if watches.is_empty() {
println!("No watch folders configured.");
@@ -718,20 +712,10 @@ fn cmd_watch_list() {
}
fn cmd_watch_remove(path: &str) {
let config_dir = dirs::config_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".config")))
.unwrap_or_else(|| std::env::temp_dir())
.join("pixstrip");
let config_dir = watch_config_dir();
let watches_path = config_dir.join("watches.json");
let mut watches: Vec<pixstrip_core::watcher::WatchFolder> = if watches_path.exists() {
std::fs::read_to_string(&watches_path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
} else {
Vec::new()
};
let mut watches = load_watches(&watches_path);
let original_len = watches.len();
let target = PathBuf::from(path);
@@ -759,10 +743,7 @@ fn cmd_watch_remove(path: &str) {
}
fn cmd_watch_start() {
let config_dir = dirs::config_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".config")))
.unwrap_or_else(|| std::env::temp_dir())
.join("pixstrip");
let config_dir = watch_config_dir();
let watches_path = config_dir.join("watches.json");
let watches: Vec<pixstrip_core::watcher::WatchFolder> = if watches_path.exists() {
@@ -876,7 +857,7 @@ fn cmd_watch_start() {
println!(" Processed: {} -> {}", format_bytes(r.total_input_bytes), format_bytes(r.total_output_bytes));
let history = HistoryStore::new();
let _ = history.add(pixstrip_core::storage::HistoryEntry {
timestamp: chrono_timestamp(),
timestamp: unix_timestamp(),
input_dir: input_dir.to_string_lossy().into(),
output_dir: output_dir.to_string_lossy().into(),
preset_name: Some(preset_name.to_string()),
@@ -1065,7 +1046,7 @@ fn format_duration(ms: u64) -> String {
}
}
fn chrono_timestamp() -> String {
fn unix_timestamp() -> String {
// Store as Unix seconds string (must match GTK format for pruning compatibility)
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)