oxinav-core 0.1.0

Core logic for OxiNav file navigator
Documentation
use crate::error::OxiNavError;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub enum Operation {
    Copy { src: PathBuf, dest: PathBuf },
    Move { src: PathBuf, dest: PathBuf },
    Rename { src: PathBuf, dest: PathBuf },
    Delete { path: PathBuf },
    CreateDir { path: PathBuf },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OperationStatus {
    Pending,
    InProgress,
    Done,
    Failed(String),
}

pub trait Filesystem: Send + Sync {
    fn copy(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError>;
    fn rename(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError>;
    fn delete(&self, path: &PathBuf) -> Result<(), OxiNavError>;
    fn create_dir(&self, path: &PathBuf) -> Result<(), OxiNavError>;
    fn exists(&self, path: &PathBuf) -> bool;
}

pub struct OperationEngine<F: Filesystem> {
    fs: F,
}

impl<F: Filesystem> OperationEngine<F> {
    pub fn new(fs: F) -> Self {
        Self { fs }
    }

    pub fn execute(&self, op: &Operation) -> Result<(), OxiNavError> {
        match op {
            Operation::Copy { src, dest } => {
                if !self.fs.exists(src) {
                    return Err(OxiNavError::SourceNotFound(src.clone()));
                }
                if self.fs.exists(dest) {
                    return Err(OxiNavError::DestinationExists(dest.clone()));
                }
                self.fs.copy(src, dest)
            }
            Operation::Move { src, dest } | Operation::Rename { src, dest } => {
                if !self.fs.exists(src) {
                    return Err(OxiNavError::SourceNotFound(src.clone()));
                }
                if self.fs.exists(dest) {
                    return Err(OxiNavError::DestinationExists(dest.clone()));
                }
                self.fs.rename(src, dest)
            }
            Operation::Delete { path } => {
                if !self.fs.exists(path) {
                    return Err(OxiNavError::SourceNotFound(path.clone()));
                }
                self.fs.delete(path)
            }
            Operation::CreateDir { path } => {
                if self.fs.exists(path) {
                    return Err(OxiNavError::DestinationExists(path.clone()));
                }
                self.fs.create_dir(path)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;
    use std::sync::{Arc, Mutex};

    #[derive(Clone)]
    struct MockFs {
        files: Arc<Mutex<HashSet<PathBuf>>>,
    }

    impl MockFs {
        fn with(paths: &[&str]) -> Self {
            Self {
                files: Arc::new(Mutex::new(paths.iter().map(PathBuf::from).collect())),
            }
        }
    }

    impl Filesystem for MockFs {
        fn copy(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError> {
            self.files.lock().unwrap().insert(dest.clone());
            let _ = src;
            Ok(())
        }
        fn rename(&self, src: &PathBuf, dest: &PathBuf) -> Result<(), OxiNavError> {
            let mut files = self.files.lock().unwrap();
            files.remove(src);
            files.insert(dest.clone());
            Ok(())
        }
        fn delete(&self, path: &PathBuf) -> Result<(), OxiNavError> {
            self.files.lock().unwrap().remove(path);
            Ok(())
        }
        fn create_dir(&self, path: &PathBuf) -> Result<(), OxiNavError> {
            self.files.lock().unwrap().insert(path.clone());
            Ok(())
        }
        fn exists(&self, path: &PathBuf) -> bool {
            self.files.lock().unwrap().contains(path)
        }
    }

    #[test]
    fn copy_missing_source_errors() {
        let engine = OperationEngine::new(MockFs::with(&[]));
        let result = engine.execute(&Operation::Copy {
            src: "/a".into(),
            dest: "/b".into(),
        });
        assert!(matches!(result, Err(OxiNavError::SourceNotFound(_))));
    }

    #[test]
    fn copy_existing_dest_errors() {
        let engine = OperationEngine::new(MockFs::with(&["/a", "/b"]));
        let result = engine.execute(&Operation::Copy {
            src: "/a".into(),
            dest: "/b".into(),
        });
        assert!(matches!(result, Err(OxiNavError::DestinationExists(_))));
    }

    #[test]
    fn delete_missing_source_errors() {
        let engine = OperationEngine::new(MockFs::with(&[]));
        let result = engine.execute(&Operation::Delete { path: "/a".into() });
        assert!(matches!(result, Err(OxiNavError::SourceNotFound(_))));
    }

    #[test]
    fn create_dir_existing_errors() {
        let engine = OperationEngine::new(MockFs::with(&["/dir"]));
        let result = engine.execute(&Operation::CreateDir {
            path: "/dir".into(),
        });
        assert!(matches!(result, Err(OxiNavError::DestinationExists(_))));
    }
}