summaryrefslogtreecommitdiffstats
path: root/src/utils/path.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2025-02-08 08:46:34 +0800
committerGitHub <noreply@github.com>2025-02-08 08:46:34 +0800
commiteb527ea758b49adbc1e311638cc6999190ff1124 (patch)
treee2396eb8c92adb7cd236bbe1b6c0fa4ec26f6d05 /src/utils/path.rs
parenta491820ab5ff9bb58e62fd25d88f31ab53a81db9 (diff)
downloadaichat-eb527ea758b49adbc1e311638cc6999190ff1124.tar.gz
feat: enhance `.file` for loading resources from diverse sources (#1155)
Diffstat (limited to 'src/utils/path.rs')
-rw-r--r--src/utils/path.rs15
1 files changed, 15 insertions, 0 deletions
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<T1: AsRef<Path>, T2: AsRef<Path>>(
base_path: T1,
@@ -75,6 +76,20 @@ pub fn get_patch_extension(path: &str) -> Option<String> {
.map(|v| v.to_string_lossy().to_lowercase())
}
+pub fn to_absolute_path(path: &str) -> Result<String> {
+ 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<String>)> {
if let Some(start) = path_str.find("/**/*.").or_else(|| path_str.find(r"\**\*.")) {
let base_path = path_str[..start].to_string();