Files
MIT/src/utils/path_ext.rs
2023-12-30 16:51:22 +08:00

57 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use crate::utils::util;
use std::path::{Path, PathBuf};
/**
Path的扩展 基于util 为了解耦不要再util中使用PathExt
*/
pub trait PathExt {
fn to_absolute(&self) -> PathBuf;
fn to_absolute_workdir(&self) -> PathBuf;
fn to_relative(&self) -> PathBuf;
fn to_relative_workdir(&self) -> PathBuf;
fn is_sub_to(&self, parent: &PathBuf) -> bool;
fn include_in<T, U>(&self, paths: U) -> bool
where
T: AsRef<Path>,
U: IntoIterator<Item = T>;
}
/*
在 Rust 中当你调用一个方法时Rust 会尝试自动解引用和自动引用auto-deref and auto-ref来匹配方法签名。
如果有一个为 Path 实现的方法,你可以在 PathBuf、&PathBuf、&&PathBuf 等上调用这个方法Rust 会自动进行必要的解引用。
*/
impl PathExt for Path {
/// 转换为绝对路径
fn to_absolute(&self) -> PathBuf {
util::get_absolute_path(&self)
}
/// 转换为绝对路径from workdir相对路径
fn to_absolute_workdir(&self) -> PathBuf {
util::to_workdir_absolute_path(&self)
}
/// 转换为相对路径to cur_dir
fn to_relative(&self) -> PathBuf {
util::get_relative_path(&self)
}
/// 转换为相对路径to workdir
fn to_relative_workdir(&self) -> PathBuf {
util::to_workdir_relative_path(&self)
}
/// 从字符串角度判断path是否是parent的子路径不检测存在性)
fn is_sub_to(&self, parent: &PathBuf) -> bool {
util::is_sub_path(&self, parent)
}
/// 判断是否在paths中包括子目录不检查存在
fn include_in<T, U>(&self, paths: U) -> bool
where
T: AsRef<Path>,
U: IntoIterator<Item = T>,
{
util::include_in_paths(&self, paths)
}
}