mod utils;
use ignore::Walk;
use serde::Serialize;
use tempfile::{self, tempdir};
use utils::get_dir_name_from_path;
use std::fs::{self, metadata};
#[derive(Debug, Serialize)]
pub struct DiskEntry {
pub path: String,
pub is_dir: bool,
pub name: String,
}
fn is_dir(file_name: String) -> crate::Result<bool> {
match metadata(file_name) {
Ok(md) => Result::Ok(md.is_dir()),
Err(err) => Result::Err(err.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) {
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(),
});
}
}
}
Result::Ok(files_and_dirs)
}
pub fn list_dir_contents(dir_path: String) -> crate::Result<Vec<DiskEntry>> {
let paths = fs::read_dir(dir_path)?;
let mut dirs: Vec<DiskEntry> = vec![];
for path in paths {
let dir_path = path.expect("dirpath error").path();
let _dir_name = dir_path.display();
dirs.push(DiskEntry {
path: format!("{}", _dir_name),
is_dir: true,
name: get_dir_name_from_path(_dir_name.to_string()),
});
}
Ok(dirs)
}
pub fn with_temp_dir<F: FnOnce(&tempfile::TempDir) -> ()>(callback: F) -> crate::Result<()> {
let dir = tempdir()?;
callback(&dir);
dir.close()?;
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use quickcheck_macros::quickcheck;
use totems::assert_ok;
#[quickcheck]
fn qc_is_dir(f: String) -> bool {
if let Ok(_) = is_dir(f.clone()) {
std::path::PathBuf::from(f).exists()
} else {
true
}
}
#[test]
fn check_walk_dir() {
let dir = String::from("test/");
let file_one = format!("{}test.txt", &dir).to_string();
let file_two = format!("{}test_binary", &dir).to_string();
let res = walk_dir(dir.clone());
assert_ok!(&res);
if let Ok(vec) = res {
assert_eq!(vec.len(), 3);
let first = &vec[0];
let second = &vec[1];
let third = &vec[2];
assert_eq!(first.path, dir);
assert_eq!(first.is_dir, true);
assert_eq!(first.name, dir);
if second.path.contains(".txt") {
assert_eq!(second.path, file_one);
assert_eq!(second.is_dir, false);
assert_eq!(second.name, file_one);
assert_eq!(third.path, file_two);
assert_eq!(third.is_dir, false);
assert_eq!(third.name, file_two);
} else {
assert_eq!(second.path, file_two);
assert_eq!(second.is_dir, false);
assert_eq!(second.name, file_two);
assert_eq!(third.path, file_one);
assert_eq!(third.is_dir, false);
assert_eq!(third.name, file_one);
}
}
}
#[test]
fn check_list_dir_contents() {
let dir = String::from("test/");
let res = list_dir_contents(dir);
assert_ok!(&res);
if let Ok(vec) = res {
assert_eq!(vec.len(), 2);
let first = &vec[0];
let second = &vec[1];
if first.path.contains(".txt") {
assert_eq!(first.path, "test/test.txt".to_string());
assert_eq!(first.is_dir, true);
assert_eq!(first.name, "test.txt".to_string());
assert_eq!(second.path, "test/test_binary".to_string());
assert_eq!(second.is_dir, true);
assert_eq!(second.name, "test_binary".to_string());
} else {
assert_eq!(second.path, "test/test.txt".to_string());
assert_eq!(second.is_dir, true);
assert_eq!(second.name, "test.txt".to_string());
assert_eq!(first.path, "test/test_binary".to_string());
assert_eq!(first.is_dir, true);
assert_eq!(first.name, "test_binary".to_string());
}
}
}
#[test]
fn check_test_dir() {
let callback = |td: &tempfile::TempDir| {
println!("{:?}", td);
};
let res = with_temp_dir(callback);
assert_ok!(res);
}
}