gravityfile_ops/
operation.rs1use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum FileOperation {
10 Copy {
12 sources: Vec<PathBuf>,
13 destination: PathBuf,
14 },
15 Move {
17 sources: Vec<PathBuf>,
18 destination: PathBuf,
19 },
20 Rename { source: PathBuf, new_name: String },
22 Delete {
24 targets: Vec<PathBuf>,
25 use_trash: bool,
26 },
27 CreateFile { path: PathBuf },
29 CreateDirectory { path: PathBuf },
31}
32
33impl FileOperation {
34 pub fn copy(sources: Vec<PathBuf>, destination: PathBuf) -> Self {
36 Self::Copy {
37 sources,
38 destination,
39 }
40 }
41
42 pub fn move_to(sources: Vec<PathBuf>, destination: PathBuf) -> Self {
44 Self::Move {
45 sources,
46 destination,
47 }
48 }
49
50 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 pub fn delete(targets: Vec<PathBuf>, use_trash: bool) -> Self {
60 Self::Delete { targets, use_trash }
61 }
62
63 pub fn create_file(path: PathBuf) -> Self {
65 Self::CreateFile { path }
66 }
67
68 pub fn create_directory(path: PathBuf) -> Self {
70 Self::CreateDirectory { path }
71 }
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct OperationError {
77 pub path: PathBuf,
79 pub message: String,
81}
82
83impl OperationError {
84 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}