gravityfile_ops/
operation.rs

1//! File operation types.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7/// A file operation to be executed.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum FileOperation {
10    /// Copy files/directories to a destination.
11    Copy {
12        sources: Vec<PathBuf>,
13        destination: PathBuf,
14    },
15    /// Move files/directories to a destination.
16    Move {
17        sources: Vec<PathBuf>,
18        destination: PathBuf,
19    },
20    /// Rename a single file or directory.
21    Rename { source: PathBuf, new_name: String },
22    /// Delete files/directories.
23    Delete {
24        targets: Vec<PathBuf>,
25        use_trash: bool,
26    },
27    /// Create a new empty file.
28    CreateFile { path: PathBuf },
29    /// Create a new directory.
30    CreateDirectory { path: PathBuf },
31}
32
33impl FileOperation {
34    /// Create a copy operation.
35    pub fn copy(sources: Vec<PathBuf>, destination: PathBuf) -> Self {
36        Self::Copy {
37            sources,
38            destination,
39        }
40    }
41
42    /// Create a move operation.
43    pub fn move_to(sources: Vec<PathBuf>, destination: PathBuf) -> Self {
44        Self::Move {
45            sources,
46            destination,
47        }
48    }
49
50    /// Create a rename operation.
51    pub fn rename(source: PathBuf, new_name: impl Into<String>) -> Self {
52        Self::Rename {
53            source,
54            new_name: new_name.into(),
55        }
56    }
57
58    /// Create a delete operation.
59    pub fn delete(targets: Vec<PathBuf>, use_trash: bool) -> Self {
60        Self::Delete { targets, use_trash }
61    }
62
63    /// Create a file creation operation.
64    pub fn create_file(path: PathBuf) -> Self {
65        Self::CreateFile { path }
66    }
67
68    /// Create a directory creation operation.
69    pub fn create_directory(path: PathBuf) -> Self {
70        Self::CreateDirectory { path }
71    }
72}
73
74/// An error that occurred during a file operation.
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct OperationError {
77    /// The path that caused the error.
78    pub path: PathBuf,
79    /// A human-readable error message.
80    pub message: String,
81}
82
83impl OperationError {
84    /// Create a new operation error.
85    pub fn new(path: PathBuf, message: impl Into<String>) -> Self {
86        Self {
87            path,
88            message: message.into(),
89        }
90    }
91}
92
93impl std::fmt::Display for OperationError {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        write!(f, "{}: {}", self.path.display(), self.message)
96    }
97}