From 08bad3380c528b55305629166540650ff4c9e74e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20P=C3=A9rex?= Date: Fri, 23 May 2025 20:03:30 +0100 Subject: [PATCH] chore: +nightly fmt --- src/directory_processor.rs | 98 ++++++++++++++++++++++++++------------ src/main.rs | 8 ++-- src/tools.rs | 35 ++++++++------ 3 files changed, 93 insertions(+), 48 deletions(-) diff --git a/src/directory_processor.rs b/src/directory_processor.rs index 2ccfb7d..d356d1f 100644 --- a/src/directory_processor.rs +++ b/src/directory_processor.rs @@ -1,9 +1,9 @@ +use crate::tools; +use crate::{FILES_TO_CONVERT, FILES_TO_COPY}; use std::collections::HashSet; use std::fs; use std::path::{Path, PathBuf}; use std::time::UNIX_EPOCH; -use crate::tools; -use crate::{FILES_TO_COPY, FILES_TO_CONVERT}; pub struct DirectoryProcessor { source_dir: PathBuf, @@ -45,7 +45,9 @@ impl DirectoryProcessor { if path.is_dir() { self.get_source_files(&path)?; } else if let Some(ext) = path.extension().and_then(|e| e.to_str()) { - if FILES_TO_CONVERT.contains(&ext.to_lowercase().as_str()) || FILES_TO_COPY.contains(&ext.to_lowercase().as_str()) { + if FILES_TO_CONVERT.contains(&ext.to_lowercase().as_str()) + || FILES_TO_COPY.contains(&ext.to_lowercase().as_str()) + { if let Ok(rel_path) = path.strip_prefix(&self.source_dir) { self.source_files.insert(rel_path.to_path_buf()); } @@ -66,10 +68,14 @@ impl DirectoryProcessor { if path.is_dir() { // Recursively check subdirectories let subdir_empty = self.clean_target_directory(&path)?; - + if subdir_empty { if let Err(e) = fs::remove_dir(&path) { - eprintln!("Warning: Could not remove empty directory {}: {}", path.display(), e); + eprintln!( + "Warning: Could not remove empty directory {}: {}", + path.display(), + e + ); } else { println!("Removed empty directory: {}", path.display()); } @@ -77,16 +83,18 @@ impl DirectoryProcessor { is_empty = false; } } else { - let rel_path = path.strip_prefix(&self.target_dir) - .map_err(|e| format!("Error getting relative path for {}: {}", path.display(), e))?; + let rel_path = path.strip_prefix(&self.target_dir).map_err(|e| { + format!("Error getting relative path for {}: {}", path.display(), e) + })?; // Check if this file should exist based on source files - let should_exist = if let Some(ext) = rel_path.extension().and_then(|e| e.to_str()) { + let should_exist = if let Some(ext) = rel_path.extension().and_then(|e| e.to_str()) + { if ext == "pdf" { // For PDF files, check if any corresponding source file exists - FILES_TO_CONVERT.iter().any(|&ext| { - self.source_files.contains(&rel_path.with_extension(ext)) - }) + FILES_TO_CONVERT + .iter() + .any(|&ext| self.source_files.contains(&rel_path.with_extension(ext))) } else { // For other files, check if they exist in source self.source_files.contains(rel_path) @@ -110,43 +118,63 @@ impl DirectoryProcessor { Ok(is_empty) } - fn process_directory(&self, current_source: &Path, current_target: &Path) -> Result<(), String> { + fn process_directory( + &self, + current_source: &Path, + current_target: &Path, + ) -> Result<(), String> { // Create destination directory if it doesn't exist if let Err(e) = fs::create_dir_all(current_target) { return Err(format!("Error creating destination directory: {}", e)); } - let entries = fs::read_dir(current_source) - .map_err(|e| format!("Error reading source directory {}: {}", current_source.display(), e))?; + let entries = fs::read_dir(current_source).map_err(|e| { + format!( + "Error reading source directory {}: {}", + current_source.display(), + e + ) + })?; for entry in entries.flatten() { let path = entry.path(); - + if path.is_dir() { // Get the relative path from source to the current subdirectory - let relative_path = path.strip_prefix(&self.source_dir) + let relative_path = path + .strip_prefix(&self.source_dir) .map_err(|e| format!("Error getting relative path: {}", e))?; - + // Create the corresponding destination subdirectory let dest_subdir = self.target_dir.join(relative_path); - + // Recursively process the subdirectory self.process_directory(&path, &dest_subdir)?; } else if let Some(ext) = path.extension().and_then(|e| e.to_str()) { // Get the relative path from source to the current file - let relative_path = path.strip_prefix(current_source) + let relative_path = path + .strip_prefix(current_source) .map_err(|e| format!("Error getting relative path: {}", e))?; if FILES_TO_CONVERT.contains(&ext.to_lowercase().as_str()) { // Construct the PDF path in the current target directory - let pdf_path = current_target.join( - relative_path.with_extension("pdf") - ); - + let pdf_path = current_target.join(relative_path.with_extension("pdf")); + if Self::needs_copy_or_conversion(&path, &pdf_path) { - match tools::convert_file(&path, &pdf_path.parent().unwrap_or(current_target)) { - Ok(_) => println!("Converted: {} -> {}", path.strip_prefix(&self.source_dir).unwrap().display(), pdf_path.strip_prefix(&self.target_dir).unwrap().display()), - Err(e) => eprintln!("Error: {} - {}", path.strip_prefix(&self.source_dir).unwrap().display(), e), + match tools::convert_file( + &path, + &pdf_path.parent().unwrap_or(current_target), + ) { + Ok(_) => println!( + "Converted: {} -> {}", + path.strip_prefix(&self.source_dir).unwrap().display(), + pdf_path.strip_prefix(&self.target_dir).unwrap().display() + ), + Err(e) => eprintln!( + "Error: {} - {}", + path.strip_prefix(&self.source_dir).unwrap().display(), + e + ), } } } else if FILES_TO_COPY.contains(&ext.to_lowercase().as_str()) { @@ -154,8 +182,16 @@ impl DirectoryProcessor { let dest_path = current_target.join(relative_path); if Self::needs_copy_or_conversion(&path, &dest_path) { match fs::copy(&path, &dest_path) { - Ok(_) => println!("Copied file: {} -> {}", path.strip_prefix(&self.source_dir).unwrap().display(), dest_path.strip_prefix(&self.target_dir).unwrap().display()), - Err(e) => eprintln!("Error copying file: {} - {}", path.strip_prefix(&self.source_dir).unwrap().display(), e), + Ok(_) => println!( + "Copied file: {} -> {}", + path.strip_prefix(&self.source_dir).unwrap().display(), + dest_path.strip_prefix(&self.target_dir).unwrap().display() + ), + Err(e) => eprintln!( + "Error copying file: {} - {}", + path.strip_prefix(&self.source_dir).unwrap().display(), + e + ), } } } @@ -167,13 +203,13 @@ impl DirectoryProcessor { pub fn process(&mut self) -> Result<(), String> { // Process all files self.process_directory(&self.source_dir.to_owned(), &self.target_dir.to_owned())?; - + // Get list of source files for cleaning self.get_source_files(&self.source_dir.to_owned())?; // Finally clean target directory self.clean_target_directory(&self.target_dir.to_owned())?; - + Ok(()) } -} +} diff --git a/src/main.rs b/src/main.rs index f31909a..c85d8b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ use clap::Parser; use std::path::PathBuf; -mod tools; mod directory_processor; +mod tools; use directory_processor::DirectoryProcessor; -pub const FILES_TO_COPY: [&str; 10] = ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp", "avif", "txt", "md"]; +pub const FILES_TO_COPY: [&str; 10] = [ + "jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp", "avif", "txt", "md", +]; pub const FILES_TO_CONVERT: [&str; 3] = ["odt", "doc", "docx"]; #[derive(Parser, Debug)] @@ -25,7 +27,7 @@ struct Args { fn main() { let args = Args::parse(); - + let mut processor = DirectoryProcessor::new(args.source, args.dest); if let Err(e) = processor.process() { eprintln!("Error processing directory: {}", e); diff --git a/src/tools.rs b/src/tools.rs index d96e675..e018348 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,7 +1,6 @@ +use std::env; use std::path::Path; use std::process::Command; -use std::env; - pub fn get_soffice_path() -> Result { // First try environment variable if set @@ -23,7 +22,7 @@ pub fn get_soffice_path() -> Result { "/usr/bin/soffice", "/usr/local/bin/soffice", "/usr/lib/libreoffice/program/soffice", - "soffice", // Try PATH + "soffice", // Try PATH ] }; @@ -39,26 +38,35 @@ pub fn get_soffice_path() -> Result { pub fn convert_file(odt_path: &Path, dest_dir: &Path) -> Result<(), String> { let soffice_path = get_soffice_path()?; - + // Verify input file exists and is readable if !odt_path.exists() { - return Err(format!("Source file does not exist: {}", odt_path.display())); + return Err(format!( + "Source file does not exist: {}", + odt_path.display() + )); } - + // Verify destination directory exists and is writable if !dest_dir.exists() { - return Err(format!("Destination directory does not exist: {}", dest_dir.display())); + return Err(format!( + "Destination directory does not exist: {}", + dest_dir.display() + )); } - + // Try to get write permissions on destination directory let metadata = std::fs::metadata(dest_dir) - .map_err(|e| format!("Failed to get destination directory metadata: {}", e))?; - + .map_err(|e| format!("Failed to get destination directory metadata: {}", e))?; + #[cfg(unix)] use std::os::unix::fs::PermissionsExt; #[cfg(unix)] if metadata.permissions().mode() & 0o200 == 0 { - return Err(format!("Destination directory is not writable: {}", dest_dir.display())); + return Err(format!( + "Destination directory is not writable: {}", + dest_dir.display() + )); } let output = Command::new(&soffice_path) @@ -78,10 +86,9 @@ pub fn convert_file(odt_path: &Path, dest_dir: &Path) -> Result<(), String> { let stdout_msg = String::from_utf8_lossy(&output.stdout); return Err(format!( "Conversion failed:\nError: {}\nOutput: {}", - error_msg, - stdout_msg + error_msg, stdout_msg )); } Ok(()) -} \ No newline at end of file +}