use std::path::{Path, PathBuf};
use anyhow::{anyhow, Result};
pub(crate) fn get_absolute_filepath<P>(filepath: P) -> Result<PathBuf>
where
P: AsRef<Path>,
{
match filepath.as_ref().canonicalize() {
Ok(pb) => Ok(pb),
Err(error) => Err(anyhow!(error)),
}
}
pub(crate) fn path_has_extension<P>(filepath: P, extension: &str) -> bool
where
P: AsRef<Path>,
{
match filepath.as_ref().extension() {
Some(ext) => {
if extension.starts_with(".") {
return ext.to_str().unwrap() == &extension[1..];
}
return ext.to_str().unwrap() == extension;
}
None => return false,
}
}
pub(crate) fn path_is_hidden<P>(filepath: P) -> bool
where
P: AsRef<Path>,
{
match get_absolute_filepath(filepath) {
Ok(pb) => {
for path in pb.iter() {
if path.to_str().unwrap().starts_with(".") {
return true;
}
}
false
}
Err(_) => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_absolute_filepath_good_path() {
let testpath = get_absolute_filepath("./Cargo.toml");
assert!(testpath.is_ok());
assert!(testpath.unwrap().ends_with("recurse/Cargo.toml"));
}
#[test]
fn test_get_absolute_filepath_bad_path() {
let testpath = get_absolute_filepath("./bogus.bad");
assert!(testpath.is_err());
}
#[test]
fn test_path_has_extension_with_correct_extension() {
let testpath = Path::new("./tests/testfiles/path/test.txt");
assert!(path_has_extension(testpath, ".txt"));
assert!(path_has_extension(testpath, "txt"));
}
#[test]
fn test_path_has_extension_with_incorrect_extension() {
let testpath = Path::new("./tests/testfiles/path/test.txt");
assert_eq!(path_has_extension(testpath, ".yaml"), false);
assert_eq!(path_has_extension(testpath, "yaml"), false);
}
#[test]
fn test_path_has_extension_with_no_extension() {
let testpath = Path::new("./tests/testfiles/path/testfile");
assert_eq!(path_has_extension(testpath, ".txt"), false);
assert_eq!(path_has_extension(testpath, "txt"), false);
}
#[test]
fn test_path_is_hidden_with_dotfile() {
let testpath = Path::new("./tests/testfiles/path/.testfile");
assert!(path_is_hidden(testpath));
assert!(path_is_hidden(testpath)); }
#[test]
fn test_path_is_hidden_with_dotfile_pathbuf() {
let testpath = PathBuf::from("./tests/testfiles/path/.testfile");
assert!(path_is_hidden(&testpath)); assert!(path_is_hidden(testpath)); }
#[test]
fn test_path_is_hidden_with_dotdir_in_path() {
let testpath = Path::new("./tests/testfiles/.dotdir/testfile");
assert!(path_is_hidden(testpath));
}
#[test]
fn test_path_is_hidden_with_dotdir_only() {
let testpath = Path::new("./tests/testfiles/.dotdir");
assert!(path_is_hidden(testpath));
}
#[test]
fn test_path_is_not_hidden_without_dotfile_or_dotdir() {
let testpath = Path::new("./tests/testfiles/path/testfile");
assert_eq!(path_is_hidden(testpath), false);
}
}