fix: status命令对当前目录进行过滤 & 改进util泛型模板

This commit is contained in:
mrbeanc
2023-12-24 11:46:07 +08:00
parent 3272b793da
commit 07dfd24d32
4 changed files with 87 additions and 20 deletions

View File

@@ -174,17 +174,38 @@ where
}
/// 过滤列表中的元素,对.iter().filter().cloned().collect()的简化
pub fn filter<T, F>(items: &Vec<T>, pred: F) -> Vec<T>
pub fn filter<'a, I, O, T, F>(items: I, pred: F) -> O
where
T: Clone,
T: Clone + 'a,
I: IntoIterator<Item = &'a T>,
O: FromIterator<T>,
F: Fn(&T) -> bool,
{
items.iter().filter(|item| pred(item)).cloned().collect()
//items可以是一个引用
items.into_iter().filter(|item| pred(item)).cloned().collect::<O>()
}
/// 对列表中的元素应用func对.iter().map().collect()的简化
pub fn map<'a, I, O, T, F>(items: I, func: F) -> O
where
T: Clone + 'a,
I: IntoIterator<Item = &'a T>,
O: FromIterator<T>,
F: Fn(&T) -> T,
{
//items可以是一个引用
items.into_iter().map(|item| func(item)).collect::<O>()
}
/// 过滤列表中的元素使其在paths中包括子目录不检查存在性
pub fn filter_to_fit_paths(items: &Vec<PathBuf>, paths: &Vec<PathBuf>) -> HashSet<PathBuf> {
filter(items, |item| include_in_paths(item, paths)).into_iter().collect()
pub fn filter_to_fit_paths<T, O>(items: &Vec<T>, paths: &Vec<T>) -> O
where
T: AsRef<Path> + Clone,
O: FromIterator<T> + IntoIterator<Item = T>,
{
filter::<_, O, _, _>(items, |item| include_in_paths(item.as_ref(), paths))
.into_iter()
.collect::<O>()
}
/// 检查文件是否在工作区内, 若不存在则false
@@ -295,8 +316,11 @@ pub fn is_empty_dir(dir: &Path) -> bool {
}
pub fn is_cur_dir(dir: &Path) -> bool {
let cur_dir = std::env::current_dir().unwrap(); //应该是绝对路径吧
get_absolute_path(dir) == cur_dir
get_absolute_path(dir) == cur_dir()
}
pub fn cur_dir() -> PathBuf {
std::env::current_dir().unwrap() //应该是绝对路径吧
}
/// 列出工作区所有文件(包括子文件夹)