chore: +nightly fmt

This commit is contained in:
Jesús Pérex 2025-05-23 20:03:30 +01:00
parent 1b871c3513
commit 08bad3380c
3 changed files with 93 additions and 48 deletions

View File

@ -1,9 +1,9 @@
use crate::tools;
use crate::{FILES_TO_CONVERT, FILES_TO_COPY};
use std::collections::HashSet; use std::collections::HashSet;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH; use std::time::UNIX_EPOCH;
use crate::tools;
use crate::{FILES_TO_COPY, FILES_TO_CONVERT};
pub struct DirectoryProcessor { pub struct DirectoryProcessor {
source_dir: PathBuf, source_dir: PathBuf,
@ -45,7 +45,9 @@ impl DirectoryProcessor {
if path.is_dir() { if path.is_dir() {
self.get_source_files(&path)?; self.get_source_files(&path)?;
} else if let Some(ext) = path.extension().and_then(|e| e.to_str()) { } 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) { if let Ok(rel_path) = path.strip_prefix(&self.source_dir) {
self.source_files.insert(rel_path.to_path_buf()); self.source_files.insert(rel_path.to_path_buf());
} }
@ -69,7 +71,11 @@ impl DirectoryProcessor {
if subdir_empty { if subdir_empty {
if let Err(e) = fs::remove_dir(&path) { 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 { } else {
println!("Removed empty directory: {}", path.display()); println!("Removed empty directory: {}", path.display());
} }
@ -77,16 +83,18 @@ impl DirectoryProcessor {
is_empty = false; is_empty = false;
} }
} else { } else {
let rel_path = path.strip_prefix(&self.target_dir) let rel_path = path.strip_prefix(&self.target_dir).map_err(|e| {
.map_err(|e| format!("Error getting relative path for {}: {}", path.display(), e))?; format!("Error getting relative path for {}: {}", path.display(), e)
})?;
// Check if this file should exist based on source files // 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" { if ext == "pdf" {
// For PDF files, check if any corresponding source file exists // For PDF files, check if any corresponding source file exists
FILES_TO_CONVERT.iter().any(|&ext| { FILES_TO_CONVERT
self.source_files.contains(&rel_path.with_extension(ext)) .iter()
}) .any(|&ext| self.source_files.contains(&rel_path.with_extension(ext)))
} else { } else {
// For other files, check if they exist in source // For other files, check if they exist in source
self.source_files.contains(rel_path) self.source_files.contains(rel_path)
@ -110,21 +118,31 @@ impl DirectoryProcessor {
Ok(is_empty) 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 // Create destination directory if it doesn't exist
if let Err(e) = fs::create_dir_all(current_target) { if let Err(e) = fs::create_dir_all(current_target) {
return Err(format!("Error creating destination directory: {}", e)); return Err(format!("Error creating destination directory: {}", e));
} }
let entries = fs::read_dir(current_source) let entries = fs::read_dir(current_source).map_err(|e| {
.map_err(|e| format!("Error reading source directory {}: {}", current_source.display(), e))?; format!(
"Error reading source directory {}: {}",
current_source.display(),
e
)
})?;
for entry in entries.flatten() { for entry in entries.flatten() {
let path = entry.path(); let path = entry.path();
if path.is_dir() { if path.is_dir() {
// Get the relative path from source to the current subdirectory // 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))?; .map_err(|e| format!("Error getting relative path: {}", e))?;
// Create the corresponding destination subdirectory // Create the corresponding destination subdirectory
@ -134,19 +152,29 @@ impl DirectoryProcessor {
self.process_directory(&path, &dest_subdir)?; self.process_directory(&path, &dest_subdir)?;
} else if let Some(ext) = path.extension().and_then(|e| e.to_str()) { } else if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
// Get the relative path from source to the current file // 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))?; .map_err(|e| format!("Error getting relative path: {}", e))?;
if FILES_TO_CONVERT.contains(&ext.to_lowercase().as_str()) { if FILES_TO_CONVERT.contains(&ext.to_lowercase().as_str()) {
// Construct the PDF path in the current target directory // Construct the PDF path in the current target directory
let pdf_path = current_target.join( let pdf_path = current_target.join(relative_path.with_extension("pdf"));
relative_path.with_extension("pdf")
);
if Self::needs_copy_or_conversion(&path, &pdf_path) { if Self::needs_copy_or_conversion(&path, &pdf_path) {
match tools::convert_file(&path, &pdf_path.parent().unwrap_or(current_target)) { match tools::convert_file(
Ok(_) => println!("Converted: {} -> {}", path.strip_prefix(&self.source_dir).unwrap().display(), pdf_path.strip_prefix(&self.target_dir).unwrap().display()), &path,
Err(e) => eprintln!("Error: {} - {}", path.strip_prefix(&self.source_dir).unwrap().display(), e), &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()) { } 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); let dest_path = current_target.join(relative_path);
if Self::needs_copy_or_conversion(&path, &dest_path) { if Self::needs_copy_or_conversion(&path, &dest_path) {
match fs::copy(&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()), Ok(_) => println!(
Err(e) => eprintln!("Error copying file: {} - {}", path.strip_prefix(&self.source_dir).unwrap().display(), e), "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
),
} }
} }
} }

View File

@ -1,12 +1,14 @@
use clap::Parser; use clap::Parser;
use std::path::PathBuf; use std::path::PathBuf;
mod tools;
mod directory_processor; mod directory_processor;
mod tools;
use directory_processor::DirectoryProcessor; 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"]; pub const FILES_TO_CONVERT: [&str; 3] = ["odt", "doc", "docx"];
#[derive(Parser, Debug)] #[derive(Parser, Debug)]

View File

@ -1,7 +1,6 @@
use std::env;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::env;
pub fn get_soffice_path() -> Result<String, String> { pub fn get_soffice_path() -> Result<String, String> {
// First try environment variable if set // First try environment variable if set
@ -23,7 +22,7 @@ pub fn get_soffice_path() -> Result<String, String> {
"/usr/bin/soffice", "/usr/bin/soffice",
"/usr/local/bin/soffice", "/usr/local/bin/soffice",
"/usr/lib/libreoffice/program/soffice", "/usr/lib/libreoffice/program/soffice",
"soffice", // Try PATH "soffice", // Try PATH
] ]
}; };
@ -42,12 +41,18 @@ pub fn convert_file(odt_path: &Path, dest_dir: &Path) -> Result<(), String> {
// Verify input file exists and is readable // Verify input file exists and is readable
if !odt_path.exists() { 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 // Verify destination directory exists and is writable
if !dest_dir.exists() { 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 // Try to get write permissions on destination directory
@ -58,7 +63,10 @@ pub fn convert_file(odt_path: &Path, dest_dir: &Path) -> Result<(), String> {
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
#[cfg(unix)] #[cfg(unix)]
if metadata.permissions().mode() & 0o200 == 0 { 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) let output = Command::new(&soffice_path)
@ -78,8 +86,7 @@ pub fn convert_file(odt_path: &Path, dest_dir: &Path) -> Result<(), String> {
let stdout_msg = String::from_utf8_lossy(&output.stdout); let stdout_msg = String::from_utf8_lossy(&output.stdout);
return Err(format!( return Err(format!(
"Conversion failed:\nError: {}\nOutput: {}", "Conversion failed:\nError: {}\nOutput: {}",
error_msg, error_msg, stdout_msg
stdout_msg
)); ));
} }