From eb527ea758b49adbc1e311638cc6999190ff1124 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 8 Feb 2025 08:46:34 +0800 Subject: feat: enhance `.file` for loading resources from diverse sources (#1155) --- src/utils/path.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/utils/path.rs') diff --git a/src/utils/path.rs b/src/utils/path.rs index bf3d936..20a365e 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -2,6 +2,7 @@ use std::path::{Component, Path, PathBuf}; use anyhow::{bail, Result}; use indexmap::IndexSet; +use path_absolutize::Absolutize; pub fn safe_join_path, T2: AsRef>( base_path: T1, @@ -75,6 +76,20 @@ pub fn get_patch_extension(path: &str) -> Option { .map(|v| v.to_string_lossy().to_lowercase()) } +pub fn to_absolute_path(path: &str) -> Result { + Ok(Path::new(&path).absolutize()?.display().to_string()) +} + +pub fn resolve_home_dir(path: &str) -> String { + let mut path = path.to_string(); + if path.starts_with("~/") || path.starts_with("~\\") { + if let Some(home_dir) = dirs::home_dir() { + path.replace_range(..1, &home_dir.display().to_string()); + } + } + path +} + fn parse_glob(path_str: &str) -> Result<(String, Vec)> { if let Some(start) = path_str.find("/**/*.").or_else(|| path_str.find(r"\**\*.")) { let base_path = path_str[..start].to_string(); -- cgit v1.2.3