Renamed crates and modules
This commit is contained in:
199
src/fs/dir.rs
Normal file
199
src/fs/dir.rs
Normal file
@@ -0,0 +1,199 @@
|
||||
use std::fs::{self, DirEntry};
|
||||
use std::path::PathBuf;
|
||||
use std::fmt;
|
||||
use std::error::Error;
|
||||
|
||||
use crate::fs::file::{self, File};
|
||||
|
||||
|
||||
|
||||
|
||||
pub struct Directory {
|
||||
files: Vec<File>,
|
||||
directories: Vec<Directory>,
|
||||
pub path: PathBuf,
|
||||
pub errors: Vec<DirError>,
|
||||
}
|
||||
|
||||
impl Directory {
|
||||
pub fn new(path: &PathBuf) -> Result<Directory, DirError> {
|
||||
|
||||
if !path.exists() {
|
||||
fs::create_dir_all(path)?;
|
||||
}
|
||||
|
||||
let entries: Vec<_> = fs::read_dir(path)?.collect();
|
||||
|
||||
let entries: Vec<_> = entries
|
||||
.into_iter()
|
||||
.map(|entry| match entry {
|
||||
Ok(entry) => match entry.metadata() {
|
||||
Ok(_) => Ok(entry),
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
).collect();
|
||||
|
||||
let (valid_entries, io_errors): (Vec<_>, Vec<_>) = entries.into_iter().partition(|entry| entry.is_ok());
|
||||
|
||||
let valid_entries: Vec<_> = valid_entries.into_iter().map(|entry| entry.unwrap()).collect();
|
||||
let io_errors = io_errors.into_iter().map(|err| DirError::from(err.err().unwrap()));
|
||||
|
||||
|
||||
let dirs = Directory::get_dirs(&valid_entries);
|
||||
let (valid_dirs, dir_errors): (Vec<_>, Vec<_>) = dirs.into_iter().partition(|dir| dir.is_ok());
|
||||
|
||||
let directories: Vec<Directory> = valid_dirs.into_iter().map(|dir| dir.unwrap()).collect();
|
||||
let dir_errors = dir_errors.into_iter().map(|err| DirError::from(err.err().unwrap()));
|
||||
|
||||
|
||||
let files = Directory::get_files(&valid_entries);
|
||||
let (valid_files, file_errors): (Vec<_>, Vec<_>) = files.into_iter().partition(|file| file.is_ok());
|
||||
|
||||
let files: Vec<File> = valid_files.into_iter().map(|file| file.unwrap()).collect();
|
||||
let file_errors = file_errors.into_iter().map(|err| DirError::from(err.err().unwrap()));
|
||||
|
||||
|
||||
let errors: Vec<DirError> = io_errors.chain(dir_errors).chain(file_errors).collect();
|
||||
|
||||
Ok(Directory{ files, directories, path: path.to_path_buf(), errors })
|
||||
}
|
||||
|
||||
|
||||
fn get_files(entries: &Vec<DirEntry>) -> Vec<Result<File, DirError>> {
|
||||
|
||||
let files: Vec<_> = entries.into_iter().filter_map(|entry| match entry.metadata() {
|
||||
Ok(data) if data.is_file() => {
|
||||
match File::new(&entry.path()) {
|
||||
Ok(file) => Some(Ok(file)),
|
||||
Err(e) => Some(Err(DirError::from(e))),
|
||||
}
|
||||
},
|
||||
Ok(_) => None,
|
||||
Err(e) => Some(Err(DirError::from(e))),
|
||||
}).collect();
|
||||
|
||||
files
|
||||
}
|
||||
|
||||
|
||||
fn get_dirs(entries: &Vec<DirEntry>) -> Vec<Result<Directory, DirError>> {
|
||||
|
||||
let directories: Vec<_> = entries.into_iter().filter_map(|entry| match entry.metadata() {
|
||||
Ok(data) if data.is_dir() => {
|
||||
match Directory::new(&entry.path()) {
|
||||
Ok(dir) => Some(Ok(dir)),
|
||||
Err(e) => Some(Err(DirError::from(e))),
|
||||
}
|
||||
},
|
||||
Ok(_) => None,
|
||||
Err(e) => Some(Err(DirError::from(e))),
|
||||
}).collect();
|
||||
|
||||
directories
|
||||
}
|
||||
|
||||
|
||||
pub fn copy(&self, dest_path: &PathBuf) -> Result<Vec<DirError>, DirError> {
|
||||
|
||||
let file_copy_results: Vec<_> = self.files
|
||||
.iter()
|
||||
.map(|file| {
|
||||
file.copy( &dest_path.join( PathBuf::from(&file.filename) ) )
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
let dir_copy_results = {
|
||||
let dirs = self.directories.iter();
|
||||
|
||||
let result = dirs.map(|dir| {
|
||||
let dir_name = match dir.path.file_name() {
|
||||
Some(filename) => filename,
|
||||
None => return Err(DirError::NoDirNameError),
|
||||
};
|
||||
|
||||
let new_dest_path = dest_path.join(PathBuf::from(dir_name));
|
||||
|
||||
if !new_dest_path.exists() {
|
||||
fs::create_dir(&new_dest_path)?;
|
||||
}
|
||||
|
||||
dir.copy(&new_dest_path)
|
||||
}).collect::<Vec<_>>();
|
||||
|
||||
result
|
||||
};
|
||||
|
||||
let mut copy_errors = Vec::new();
|
||||
|
||||
file_copy_results.into_iter().for_each(|result| if result.is_err() {
|
||||
copy_errors.push(DirError::from(result.err().unwrap()))
|
||||
});
|
||||
|
||||
dir_copy_results.into_iter().for_each(|result| match result {
|
||||
Err(e) => {
|
||||
copy_errors.push(DirError::from(e));
|
||||
},
|
||||
Ok(copy_results) => {
|
||||
copy_results.into_iter().for_each(|error| copy_errors.push(error));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Ok(copy_errors)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DirError {
|
||||
DirCopyMetadataError(std::env::VarError),
|
||||
DirIOError(std::io::Error),
|
||||
DirFileCopyError(file::FileError),
|
||||
NoDirNameError,
|
||||
}
|
||||
|
||||
impl Error for DirError {}
|
||||
|
||||
impl fmt::Display for DirError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
DirError::DirCopyMetadataError(var_error) => {
|
||||
write!(f, "{}", var_error)
|
||||
},
|
||||
DirError::DirIOError(io_error) => {
|
||||
write!(f, "{}", io_error)
|
||||
},
|
||||
DirError::DirFileCopyError(copy_error) => {
|
||||
write!(f, "{}", copy_error)
|
||||
},
|
||||
DirError::NoDirNameError => {
|
||||
write!(f, "Directory does not have a valid name")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::env::VarError> for DirError {
|
||||
fn from(error: std::env::VarError) -> DirError {
|
||||
DirError::DirCopyMetadataError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for DirError {
|
||||
fn from(error: std::io::Error) -> DirError {
|
||||
DirError::DirIOError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<file::FileError> for DirError {
|
||||
fn from(error: file::FileError) -> DirError {
|
||||
DirError::DirFileCopyError(error)
|
||||
}
|
||||
}
|
||||
71
src/fs/file.rs
Normal file
71
src/fs/file.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use std::path::PathBuf;
|
||||
use std::fs;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
|
||||
|
||||
|
||||
|
||||
pub struct File {
|
||||
pub path: PathBuf,
|
||||
pub filename: String,
|
||||
}
|
||||
|
||||
impl File {
|
||||
pub fn new(path: &PathBuf) -> Result<File, FileError> {
|
||||
|
||||
let filename = match path.file_name() {
|
||||
Some(filename) => match filename.to_str() {
|
||||
Some(filename) => String::from(filename),
|
||||
None => return Err(FileError::FilenameInvalidUTFError),
|
||||
},
|
||||
None => return Err(FileError::NoFileNameError),
|
||||
};
|
||||
|
||||
Ok(File{ path: path.to_path_buf(), filename })
|
||||
}
|
||||
|
||||
|
||||
pub fn copy(&self, dest_path: &PathBuf) -> Result<(), FileError> {
|
||||
|
||||
fs::copy(&self.path, dest_path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FileError {
|
||||
CopyError(std::io::Error),
|
||||
NoFileNameError,
|
||||
FilenameInvalidUTFError,
|
||||
}
|
||||
|
||||
impl Error for FileError {}
|
||||
|
||||
impl fmt::Display for FileError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
FileError::CopyError(copy_error) => {
|
||||
write!(f, "{}", copy_error)
|
||||
},
|
||||
FileError::FilenameInvalidUTFError => {
|
||||
write!(f, "Invalild UTF in filename")
|
||||
},
|
||||
FileError::NoFileNameError => {
|
||||
write!(f, "File does not have a valid filename")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for FileError {
|
||||
fn from(error: std::io::Error) -> FileError {
|
||||
FileError::CopyError(error)
|
||||
}
|
||||
}
|
||||
2
src/fs/mod.rs
Normal file
2
src/fs/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod dir;
|
||||
pub mod file;
|
||||
Reference in New Issue
Block a user