ferrite_image/
operations.rs

1use std::path::{Path, PathBuf};
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum FileOperationError {
6    #[error("Failed to move file to trash: {0}")]
7    TrashError(#[from] trash::Error),
8    #[error("File not found: {0}")]
9    FileNotFound(PathBuf),
10    #[error("IO error: {0}")]
11    IoError(#[from] std::io::Error),
12}
13
14pub type Result<T> = std::result::Result<T, FileOperationError>;
15
16/// File operations for managing image files
17pub struct FileOperations;
18
19impl FileOperations {
20    /// Delete a file by moving it to trash
21    ///
22    /// # Arguments
23    /// * `path` - Path to the file to delete
24    ///
25    /// # Returns
26    /// * `Ok(())` if the file was successfully moved to trash
27    /// * `Err(FileOperationError)` if the operation failed
28    pub fn delete_file<P: AsRef<Path>>(path: P) -> Result<()> {
29        let path = path.as_ref();
30
31        if !path.exists() {
32            return Err(FileOperationError::FileNotFound(path.to_path_buf()));
33        }
34
35        trash::delete(path)?;
36        tracing::info!("Successfully moved file to trash: {}", path.display());
37        Ok(())
38    }
39
40    /// Permanently delete a file (bypassing trash)
41    ///
42    /// # Arguments
43    /// * `path` - Path to the file to delete
44    ///
45    /// # Returns
46    /// * `Ok(())` if the file was successfully deleted
47    /// * `Err(FileOperationError)` if the operation failed
48    ///
49    /// # Warning
50    /// This permanently deletes the file and cannot be undone
51    pub fn delete_file_permanent<P: AsRef<Path>>(path: P) -> Result<()> {
52        let path = path.as_ref();
53
54        if !path.exists() {
55            return Err(FileOperationError::FileNotFound(path.to_path_buf()));
56        }
57
58        std::fs::remove_file(path)?;
59        tracing::warn!("Permanently deleted file: {}", path.display());
60        Ok(())
61    }
62}