Skip to main content

oxinav_core/
operation.rs

1use crate::error::OxiNavError;
2use std::path::PathBuf;
3
4#[derive(Debug, Clone)]
5pub enum Operation {
6    Copy { src: PathBuf, dest: PathBuf },
7    Move { src: PathBuf, dest: PathBuf },
8    Rename { src: PathBuf, dest: PathBuf },
9    Delete { path: PathBuf },
10    CreateDir { path: PathBuf },
11}
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum OperationStatus {
15    Pending,
16    InProgress,
17    Done,
18    Failed(String),
19}
20
21pub trait Filesystem: Send + Sync {
22    fn copy(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError>;
23    fn rename(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError>;
24    fn delete(&self, path: &PathBuf) -> Result<(), OxiNavError>;
25    fn create_dir(&self, path: &PathBuf) -> Result<(), OxiNavError>;
26    fn exists(&self, path: &PathBuf) -> bool;
27}
28
29pub struct OperationEngine<F: Filesystem> {
30    fs: F,
31}
32
33impl<F: Filesystem> OperationEngine<F> {
34    pub fn new(fs: F) -> Self {
35        Self { fs }
36    }
37
38    pub fn execute(&self, op: &Operation) -> Result<(), OxiNavError> {
39        match op {
40            Operation::Copy { src, dest } => {
41                if !self.fs.exists(src) {
42                    return Err(OxiNavError::SourceNotFound(src.clone()));
43                }
44                if self.fs.exists(dest) {
45                    return Err(OxiNavError::DestinationExists(dest.clone()));
46                }
47                self.fs.copy(src, dest)
48            }
49            Operation::Move { src, dest } | Operation::Rename { src, dest } => {
50                if !self.fs.exists(src) {
51                    return Err(OxiNavError::SourceNotFound(src.clone()));
52                }
53                if self.fs.exists(dest) {
54                    return Err(OxiNavError::DestinationExists(dest.clone()));
55                }
56                self.fs.rename(src, dest)
57            }
58            Operation::Delete { path } => {
59                if !self.fs.exists(path) {
60                    return Err(OxiNavError::SourceNotFound(path.clone()));
61                }
62                self.fs.delete(path)
63            }
64            Operation::CreateDir { path } => {
65                if self.fs.exists(path) {
66                    return Err(OxiNavError::DestinationExists(path.clone()));
67                }
68                self.fs.create_dir(path)
69            }
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use std::collections::HashSet;
78    use std::sync::{Arc, Mutex};
79
80    #[derive(Clone)]
81    struct MockFs {
82        files: Arc<Mutex<HashSet<PathBuf>>>,
83    }
84
85    impl MockFs {
86        fn with(paths: &[&str]) -> Self {
87            Self {
88                files: Arc::new(Mutex::new(paths.iter().map(PathBuf::from).collect())),
89            }
90        }
91    }
92
93    impl Filesystem for MockFs {
94        fn copy(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError> {
95            self.files.lock().unwrap().insert(dest.clone());
96            let _ = src;
97            Ok(())
98        }
99        fn rename(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError> {
100            let mut files = self.files.lock().unwrap();
101            files.remove(src);
102            files.insert(dest.clone());
103            Ok(())
104        }
105        fn delete(&self, path: &PathBuf) -> Result<(), OxiNavError> {
106            self.files.lock().unwrap().remove(path);
107            Ok(())
108        }
109        fn create_dir(&self, path: &PathBuf) -> Result<(), OxiNavError> {
110            self.files.lock().unwrap().insert(path.clone());
111            Ok(())
112        }
113        fn exists(&self, path: &PathBuf) -> bool {
114            self.files.lock().unwrap().contains(path)
115        }
116    }
117
118    #[test]
119    fn copy_missing_source_errors() {
120        let engine = OperationEngine::new(MockFs::with(&[]));
121        let result = engine.execute(&Operation::Copy {
122            src: "/a".into(),
123            dest: "/b".into(),
124        });
125        assert!(matches!(result, Err(OxiNavError::SourceNotFound(_))));
126    }
127
128    #[test]
129    fn copy_existing_dest_errors() {
130        let engine = OperationEngine::new(MockFs::with(&["/a", "/b"]));
131        let result = engine.execute(&Operation::Copy {
132            src: "/a".into(),
133            dest: "/b".into(),
134        });
135        assert!(matches!(result, Err(OxiNavError::DestinationExists(_))));
136    }
137
138    #[test]
139    fn delete_missing_source_errors() {
140        let engine = OperationEngine::new(MockFs::with(&[]));
141        let result = engine.execute(&Operation::Delete { path: "/a".into() });
142        assert!(matches!(result, Err(OxiNavError::SourceNotFound(_))));
143    }
144
145    #[test]
146    fn create_dir_existing_errors() {
147        let engine = OperationEngine::new(MockFs::with(&["/dir"]));
148        let result = engine.execute(&Operation::CreateDir {
149            path: "/dir".into(),
150        });
151        assert!(matches!(result, Err(OxiNavError::DestinationExists(_))));
152    }
153}