More error handling
This commit is contained in:
@@ -22,39 +22,25 @@ impl Directory {
|
||||
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 entries = fs::read_dir(path)?;
|
||||
let (valid_entries, io_errors): (Vec<_>, Vec<_>) = entries.partition(|entry| entry.is_ok());
|
||||
let valid_entries: Vec<DirEntry> = valid_entries.into_iter().map(|entry| entry.unwrap()).collect();
|
||||
|
||||
|
||||
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 dir_errors = dir_errors.into_iter().map(|err| DirError::from(err.err().unwrap()));
|
||||
let io_errors = io_errors.into_iter().map(|err| DirError::from(err.err().unwrap()));
|
||||
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 })
|
||||
|
||||
@@ -14,6 +14,15 @@ pub struct File {
|
||||
impl File {
|
||||
pub fn new(path: &PathBuf) -> Result<File, FileError> {
|
||||
|
||||
let parent_dir = match path.parent() {
|
||||
Some(parent) => parent,
|
||||
None => return Err(FileError::NoParentDirError),
|
||||
};
|
||||
|
||||
if !parent_dir.exists() {
|
||||
fs::create_dir_all(parent_dir)?;
|
||||
}
|
||||
|
||||
let filename = match path.file_name() {
|
||||
Some(filename) => match filename.to_str() {
|
||||
Some(filename) => String::from(filename),
|
||||
@@ -43,6 +52,7 @@ impl File {
|
||||
pub enum FileError {
|
||||
CopyError(std::io::Error),
|
||||
NoFileNameError,
|
||||
NoParentDirError,
|
||||
FilenameInvalidUTFError,
|
||||
}
|
||||
|
||||
@@ -57,9 +67,12 @@ impl fmt::Display for FileError {
|
||||
FileError::FilenameInvalidUTFError => {
|
||||
write!(f, "Invalild UTF in filename")
|
||||
},
|
||||
FileError::NoParentDirError => {
|
||||
write!(f, "File does not have a parent directory")
|
||||
}
|
||||
FileError::NoFileNameError => {
|
||||
write!(f, "File does not have a valid filename")
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user