|
@@ -16,39 +16,33 @@ pub struct DiskEntry {
|
|
|
}
|
|
|
|
|
|
fn is_dir(file_name: String) -> crate::Result<bool> {
|
|
|
- match metadata(file_name.to_string()) {
|
|
|
- Ok(md) => return Result::Ok(md.is_dir()),
|
|
|
- Err(err) => return Result::Err(err.to_string().into()),
|
|
|
- };
|
|
|
+ match metadata(file_name) {
|
|
|
+ Ok(md) => Result::Ok(md.is_dir()),
|
|
|
+ Err(err) => Result::Err(err.to_string().into()),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
pub fn walk_dir(path_copy: String) -> crate::Result<Vec<DiskEntry>> {
|
|
|
println!("Trying to walk: {}", path_copy.as_str());
|
|
|
let mut files_and_dirs: Vec<DiskEntry> = vec![];
|
|
|
for result in Walk::new(path_copy) {
|
|
|
- match result {
|
|
|
- Ok(entry) => {
|
|
|
- let display_value = entry.path().display();
|
|
|
- let _dir_name = display_value.to_string();
|
|
|
-
|
|
|
- match is_dir(display_value.to_string()) {
|
|
|
- Ok(flag) => {
|
|
|
- files_and_dirs.push(DiskEntry {
|
|
|
- path: display_value.to_string(),
|
|
|
- is_dir: flag,
|
|
|
- name: display_value.to_string(),
|
|
|
- });
|
|
|
- }
|
|
|
- Err(_) => {}
|
|
|
- }
|
|
|
+ if let Ok(entry) = result {
|
|
|
+ let display_value = entry.path().display();
|
|
|
+ let _dir_name = display_value.to_string();
|
|
|
+
|
|
|
+ if let Ok(flag) = is_dir(display_value.to_string()) {
|
|
|
+ files_and_dirs.push(DiskEntry {
|
|
|
+ path: display_value.to_string(),
|
|
|
+ is_dir: flag,
|
|
|
+ name: display_value.to_string(),
|
|
|
+ });
|
|
|
}
|
|
|
- Err(_) => {}
|
|
|
}
|
|
|
}
|
|
|
- return Result::Ok(files_and_dirs);
|
|
|
+ Result::Ok(files_and_dirs)
|
|
|
}
|
|
|
|
|
|
-pub fn list_dir_contents(dir_path: &String) -> crate::Result<Vec<DiskEntry>> {
|
|
|
+pub fn list_dir_contents(dir_path: String) -> crate::Result<Vec<DiskEntry>> {
|
|
|
fs::read_dir(dir_path)
|
|
|
.map_err(|err| crate::Error::with_chain(err, "read string failed"))
|
|
|
.and_then(|paths| {
|
|
@@ -83,11 +77,10 @@ mod test {
|
|
|
#[quickcheck]
|
|
|
fn qc_is_dir(f: String) -> bool {
|
|
|
// is the string runs through is_dir and comes out as an OK result then it must be a DIR.
|
|
|
- match is_dir(f.clone()) {
|
|
|
- // check to see that the path exists.
|
|
|
- Ok(_) => std::path::PathBuf::from(f).exists(),
|
|
|
- // if is Err then string isn't a path nor a dir and function passes.
|
|
|
- Err(_) => true,
|
|
|
+ if let Ok(_) = is_dir(f.clone()) {
|
|
|
+ std::path::PathBuf::from(f).exists()
|
|
|
+ } else {
|
|
|
+ true
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -154,7 +147,7 @@ mod test {
|
|
|
let dir = String::from("test/");
|
|
|
|
|
|
// call list_dir_contents on the dir string
|
|
|
- let res = list_dir_contents(&dir);
|
|
|
+ let res = list_dir_contents(dir);
|
|
|
|
|
|
// assert that the result is Ok()
|
|
|
assert_ok!(&res);
|