Skip to main content

ai_agent/utils/
path.rs

1// Source: /data/home/swei/claudecode/openclaudecode/src/utils/path.ts
2#![allow(dead_code)]
3
4use std::path::Path;
5
6pub fn expand_path(path: &str) -> String {
7    if path.starts_with('~') {
8        if let Some(home) = dirs::home_dir() {
9            return path.replace('~', &home.to_string_lossy());
10        }
11    }
12    path.to_string()
13}
14
15pub fn normalize_path(path: &str) -> String {
16    Path::new(path)
17        .canonicalize()
18        .map(|p| p.to_string_lossy().to_string())
19        .unwrap_or_else(|_| path.to_string())
20}
21
22pub fn is_absolute(path: &str) -> bool {
23    Path::new(path).is_absolute()
24}
25
26pub fn join_path(a: &str, b: &str) -> String {
27    Path::new(a).join(b).to_string_lossy().to_string()
28}
29
30pub fn parent_path(path: &str) -> Option<String> {
31    Path::new(path)
32        .parent()
33        .map(|p| p.to_string_lossy().to_string())
34}
35
36pub fn file_name(path: &str) -> Option<String> {
37    Path::new(path)
38        .file_name()
39        .map(|n| n.to_string_lossy().to_string())
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_is_absolute() {
48        #[cfg(unix)]
49        assert!(is_absolute("/usr/bin"));
50        #[cfg(windows)]
51        assert!(is_absolute("C:\\Windows"));
52    }
53}