Cleanup path_utils.rs (#74) (#103)

This commit is contained in:
kastenbutt 2019-11-29 11:06:40 +01:00 committed by nothingismagick
parent 4510f638e8
commit 897f64e715

View File

@ -28,8 +28,8 @@ pub struct DirInfo {
pub directories: Vec<String>, pub directories: Vec<String>,
} }
impl Options { impl Default for Options {
pub fn new() -> Options { fn default() -> Options {
Options { Options {
overwrite: false, overwrite: false,
skip: false, skip: false,
@ -41,14 +41,14 @@ impl Options {
} }
} }
impl DirOpts { impl Default for DirOpts {
pub fn new() -> DirOpts { fn default() -> DirOpts {
DirOpts { depth: 0 } DirOpts { depth: 0 }
} }
} }
impl FileOpts { impl Default for FileOpts {
pub fn new() -> FileOpts { fn default() -> FileOpts {
FileOpts { FileOpts {
overwrite: false, overwrite: false,
skip: false, skip: false,
@ -120,6 +120,7 @@ where
Ok(std::fs::copy(from, to)?) Ok(std::fs::copy(from, to)?)
} }
#[allow(dead_code)]
pub fn copy<P, Q>(from: P, to: Q, options: &Options) -> crate::Result<u64> pub fn copy<P, Q>(from: P, to: Q, options: &Options) -> crate::Result<u64>
where where
P: AsRef<Path>, P: AsRef<Path>,
@ -147,11 +148,11 @@ where
return Err("Invalid folder from".into()); return Err("Invalid folder from".into());
} }
let mut to: PathBuf = to.as_ref().to_path_buf(); let mut to: PathBuf = to.as_ref().to_path_buf();
if !options.content_only && ((options.copy_files && to.exists()) || !options.copy_files) { if !options.content_only && (!options.copy_files || to.exists()) {
to.push(dir_name); to.push(dir_name);
} }
let mut read_options = DirOpts::new(); let mut read_options = DirOpts::default();
if options.depth > 0 { if options.depth > 0 {
read_options.depth = options.depth; read_options.depth = options.depth;
} }
@ -203,10 +204,9 @@ pub fn get_dir_info<P>(path: P, options: &DirOpts) -> crate::Result<DirInfo>
where where
P: AsRef<Path>, P: AsRef<Path>,
{ {
let mut depth = 0;
if options.depth != 0 { let depth = if options.depth==0 { 0 } else { options.depth + 1 };
depth = options.depth + 1;
}
_get_dir_info(path, depth) _get_dir_info(path, depth)
} }
@ -218,7 +218,7 @@ where
let mut files = Vec::new(); let mut files = Vec::new();
let mut size = 0; let mut size = 0;
let item = path.as_ref().to_str(); let item = path.as_ref().to_str();
if !item.is_some() { if item.is_none() {
return Err("Invalid path".into()); return Err("Invalid path".into());
} }
let item = item.unwrap().to_string(); let item = item.unwrap().to_string();
@ -235,10 +235,10 @@ where
match _get_dir_info(_path, depth) { match _get_dir_info(_path, depth) {
Ok(items) => { Ok(items) => {
let mut _files = items.files; let mut _files = items.files;
let mut _dirrectories = items.directories; let mut _directories = items.directories;
size += items.size; size += items.size;
files.append(&mut _files); files.append(&mut _files);
directories.append(&mut _dirrectories); directories.append(&mut _directories);
} }
Err(err) => return Err(err), Err(err) => return Err(err),
} }
@ -249,8 +249,8 @@ where
files.push(item); files.push(item);
} }
Ok(DirInfo { Ok(DirInfo {
size: size, size,
files: files, files,
directories: directories, directories,
}) })
} }