diff --git a/src/host/mod.rs b/src/host/mod.rs index 1265bb3..85fcd81 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -48,26 +48,24 @@ pub enum HostErrorType { } /// ### HostError -/// +/// /// HostError is a wrapper for the error type and the exact io error pub struct HostError { pub error: HostErrorType, - pub ioerr: Option + pub ioerr: Option, } impl HostError { - /// ### new - /// + /// /// Instantiates a new HostError pub(crate) fn new(error: HostErrorType, errno: Option) -> HostError { HostError { error: error, - ioerr: errno + ioerr: errno, } } - } impl std::fmt::Display for HostError { @@ -83,7 +81,7 @@ impl std::fmt::Display for HostError { }; match &self.ioerr { Some(err) => write!(f, "{}: {}", code_str, err), - None => write!(f, "{}", code_str) + None => write!(f, "{}", code_str), } } } @@ -157,14 +155,14 @@ impl Localhost { } /// ### mkdir - /// + /// /// Make a directory at path and update the file list (only if relative) pub fn mkdir(&mut self, dir_name: &Path) -> Result<(), HostError> { self.mkdir_ex(dir_name, false) } /// ### mkdir_ex - /// + /// /// Extended option version of makedir. /// ignex: don't report error if directory already exists pub fn mkdir_ex(&mut self, dir_name: &Path, ignex: bool) -> Result<(), HostError> { @@ -180,7 +178,7 @@ impl Localhost { if dir_path.exists() { match ignex { true => return Ok(()), - false => return Err(HostError::new(HostErrorType::FileAlreadyExists, None)) + false => return Err(HostError::new(HostErrorType::FileAlreadyExists, None)), } } match std::fs::create_dir(dir_path) { @@ -189,24 +187,24 @@ impl Localhost { if dir_name.is_relative() { self.files = match self.scan_dir(self.wrkdir.as_path()) { Ok(f) => f, - Err(err) => return Err(err) + Err(err) => return Err(err), }; } Ok(()) - }, - Err(err) => Err(HostError::new(HostErrorType::CouldNotCreateFile, Some(err))) + } + Err(err) => Err(HostError::new(HostErrorType::CouldNotCreateFile, Some(err))), } } /// ### remove - /// + /// /// Remove file entry pub fn remove(&mut self, entry: &FsEntry) -> Result<(), HostError> { match entry { FsEntry::Directory(dir) => { // If file doesn't exist; return error - if ! dir.abs_path.as_path().exists() { - return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None)) + if !dir.abs_path.as_path().exists() { + return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None)); } // Remove match std::fs::remove_dir_all(dir.abs_path.as_path()) { @@ -214,17 +212,17 @@ impl Localhost { // Update dir self.files = match self.scan_dir(self.wrkdir.as_path()) { Ok(f) => f, - Err(err) => return Err(err) + Err(err) => return Err(err), }; Ok(()) - }, - Err(err) => Err(HostError::new(HostErrorType::DeleteFailed, Some(err))) + } + Err(err) => Err(HostError::new(HostErrorType::DeleteFailed, Some(err))), } - }, + } FsEntry::File(file) => { // If file doesn't exist; return error - if ! file.abs_path.as_path().exists() { - return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None)) + if !file.abs_path.as_path().exists() { + return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None)); } // Remove match std::fs::remove_file(file.abs_path.as_path()) { @@ -232,45 +230,45 @@ impl Localhost { // Update dir self.files = match self.scan_dir(self.wrkdir.as_path()) { Ok(f) => f, - Err(err) => return Err(err) + Err(err) => return Err(err), }; Ok(()) - }, - Err(err) => Err(HostError::new(HostErrorType::DeleteFailed, Some(err))) + } + Err(err) => Err(HostError::new(HostErrorType::DeleteFailed, Some(err))), } } } } /// ### rename - /// + /// /// Rename file or directory to new name pub fn rename(&mut self, entry: &FsEntry, dst_path: &Path) -> Result<(), HostError> { let abs_path: PathBuf = match entry { FsEntry::Directory(dir) => dir.abs_path.clone(), - FsEntry::File(f) => f.abs_path.clone() + FsEntry::File(f) => f.abs_path.clone(), }; match std::fs::rename(abs_path.as_path(), dst_path) { Ok(_) => { // Scan dir self.files = match self.scan_dir(self.wrkdir.as_path()) { Ok(f) => f, - Err(err) => return Err(err) + Err(err) => return Err(err), }; Ok(()) - }, - Err(err) => Err(HostError::new(HostErrorType::CouldNotCreateFile, Some(err))) + } + Err(err) => Err(HostError::new(HostErrorType::CouldNotCreateFile, Some(err))), } } /// ### stat - /// + /// /// Stat file and create a FsEntry #[cfg(any(unix, macos, linux))] pub fn stat(&self, path: &Path) -> Result { let attr: Metadata = match fs::metadata(path.clone()) { Ok(metadata) => metadata, - Err(err) => return Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))) + Err(err) => return Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))), }; let file_name: String = String::from(path.file_name().unwrap().to_str().unwrap_or("")); // Match dir / file @@ -318,14 +316,14 @@ impl Localhost { } /// ### stat - /// + /// /// Stat file and create a FsEntry #[cfg(target_os = "windows")] #[cfg(not(tarpaulin_include))] pub fn stat(&self, path: &Path) -> Result { let attr: Metadata = match fs::metadata(path.clone()) { Ok(metadata) => metadata, - Err(err) => return Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))) + Err(err) => return Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))), }; let file_name: String = String::from(path.file_name().unwrap().to_str().unwrap_or("")); // Match dir / file @@ -377,9 +375,14 @@ impl Localhost { /// Open file for read pub fn open_file_read(&self, file: &Path) -> Result { if !self.file_exists(file) { - return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None)) + return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None)); } - match OpenOptions::new().create(false).read(true).write(false).open(file) { + match OpenOptions::new() + .create(false) + .read(true) + .write(false) + .open(file) + { Ok(f) => Ok(f), Err(err) => Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))), } @@ -389,7 +392,12 @@ impl Localhost { /// /// Open file for write pub fn open_file_write(&self, file: &Path) -> Result { - match OpenOptions::new().create(true).write(true).truncate(true).open(file) { + match OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .open(file) + { Ok(f) => Ok(f), Err(err) => match self.file_exists(file) { true => Err(HostError::new(HostErrorType::ReadonlyFile, Some(err))), @@ -442,12 +450,12 @@ mod tests { use super::*; #[cfg(any(unix, macos, linux))] - use std::io::Write; - #[cfg(any(unix, macos, linux))] use std::fs::File; + #[cfg(any(unix, macos, linux))] + use std::io::Write; #[cfg(any(unix, macos, linux))] - use std::os::unix::fs::{PermissionsExt, symlink}; + use std::os::unix::fs::{symlink, PermissionsExt}; #[test] fn test_host_error_new() { @@ -586,7 +594,6 @@ mod tests { //fs::set_permissions(file.path(), perms)?; assert!(host.open_file_write(file.path()).is_err()); } - #[cfg(any(unix, macos, linux))] #[test] fn test_host_localhost_symlinks() { @@ -594,7 +601,11 @@ mod tests { // Create sample file assert!(File::create(format!("{}/foo.txt", tmpdir.path().display()).as_str()).is_ok()); // Create symlink - assert!(symlink(format!("{}/foo.txt", tmpdir.path().display()), format!("{}/bar.txt", tmpdir.path().display())).is_ok()); + assert!(symlink( + format!("{}/foo.txt", tmpdir.path().display()), + format!("{}/bar.txt", tmpdir.path().display()) + ) + .is_ok()); // Get dir let host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap(); let files: Vec = host.list_dir(); @@ -606,22 +617,28 @@ mod tests { if file_0.name == String::from("foo.txt") { assert!(file_0.symlink.is_none()); } else { - assert_eq!(*file_0.symlink.as_ref().unwrap(), PathBuf::from(format!("{}/foo.txt", tmpdir.path().display()))); + assert_eq!( + *file_0.symlink.as_ref().unwrap(), + PathBuf::from(format!("{}/foo.txt", tmpdir.path().display())) + ); } - }, - _ => panic!("expected entry 0 to be file: {:?}", file_0) + } + _ => panic!("expected entry 0 to be file: {:?}", file_0), }; // Verify simlink let file_1: &FsEntry = files.get(1).unwrap(); match file_1 { FsEntry::File(file_1) => { if file_1.name == String::from("bar.txt") { - assert_eq!(*file_1.symlink.as_ref().unwrap(), PathBuf::from(format!("{}/foo.txt", tmpdir.path().display()))); + assert_eq!( + *file_1.symlink.as_ref().unwrap(), + PathBuf::from(format!("{}/foo.txt", tmpdir.path().display())) + ); } else { assert!(file_1.symlink.is_none()); } - }, - _ => panic!("expected entry 0 to be file: {:?}", file_1) + } + _ => panic!("expected entry 0 to be file: {:?}", file_1), }; } @@ -635,10 +652,12 @@ mod tests { assert!(host.mkdir(PathBuf::from("test_dir").as_path()).is_ok()); let files: Vec = host.list_dir(); assert_eq!(files.len(), 1); // There should be 1 file now - // Try to re-create directory + // Try to re-create directory assert!(host.mkdir(PathBuf::from("test_dir").as_path()).is_err()); // Try abs path - assert!(host.mkdir_ex(PathBuf::from("/tmp/test_dir_123456789").as_path(), true).is_ok()); + assert!(host + .mkdir_ex(PathBuf::from("/tmp/test_dir_123456789").as_path(), true) + .is_ok()); } #[test] @@ -650,12 +669,12 @@ mod tests { let mut host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap(); let files: Vec = host.list_dir(); assert_eq!(files.len(), 1); // There should be 1 file now - // Remove file + // Remove file assert!(host.remove(files.get(0).unwrap()).is_ok()); // There should be 0 files now let files: Vec = host.list_dir(); assert_eq!(files.len(), 0); // There should be 0 files now - // Create directory + // Create directory assert!(host.mkdir(PathBuf::from("test_dir").as_path()).is_ok()); // Delete directory let files: Vec = host.list_dir(); @@ -668,22 +687,28 @@ mod tests { fn test_host_localhost_rename() { let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap(); // Create sample file - let src_path: PathBuf = PathBuf::from(format!("{}/foo.txt", tmpdir.path().display()).as_str()); + let src_path: PathBuf = + PathBuf::from(format!("{}/foo.txt", tmpdir.path().display()).as_str()); assert!(File::create(src_path.as_path()).is_ok()); let mut host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap(); let files: Vec = host.list_dir(); assert_eq!(files.len(), 1); // There should be 1 file now assert_eq!(get_filename(files.get(0).unwrap()), String::from("foo.txt")); // Rename file - let dst_path: PathBuf = PathBuf::from(format!("{}/bar.txt", tmpdir.path().display()).as_str()); - assert!(host.rename(files.get(0).unwrap(), dst_path.as_path()).is_ok()); + let dst_path: PathBuf = + PathBuf::from(format!("{}/bar.txt", tmpdir.path().display()).as_str()); + assert!(host + .rename(files.get(0).unwrap(), dst_path.as_path()) + .is_ok()); // There should be still 1 file now, but named bar.txt let files: Vec = host.list_dir(); assert_eq!(files.len(), 1); // There should be 0 files now assert_eq!(get_filename(files.get(0).unwrap()), String::from("bar.txt")); // Fail let bad_path: PathBuf = PathBuf::from("/asdailsjoidoewojdijow/ashdiuahu"); - assert!(host.rename(files.get(0).unwrap(), bad_path.as_path()).is_err()); + assert!(host + .rename(files.get(0).unwrap(), bad_path.as_path()) + .is_err()); } /// ### create_sample_file @@ -705,7 +730,7 @@ mod tests { fn get_filename(entry: &FsEntry) -> String { match entry { FsEntry::Directory(d) => d.name.clone(), - FsEntry::File(f) => f.name.clone() + FsEntry::File(f) => f.name.clone(), } } }