ai_agent/utils/
ide_path_conversion.rs1use std::process::Command;
6
7pub trait IdePathConverter {
9 fn to_local_path(&self, ide_path: &str) -> String;
11 fn to_ide_path(&self, local_path: &str) -> String;
13}
14
15pub struct WindowsToWSLConverter {
17 wsl_distro_name: Option<String>,
18}
19
20impl WindowsToWSLConverter {
21 pub fn new(wsl_distro_name: Option<String>) -> Self {
23 Self { wsl_distro_name }
24 }
25}
26
27impl IdePathConverter for WindowsToWSLConverter {
28 fn to_local_path(&self, windows_path: &str) -> String {
29 if windows_path.is_empty() {
30 return windows_path.to_string();
31 }
32
33 if let Some(ref distro) = self.wsl_distro_name {
35 if let Some(caps) = regex::Regex::new(r"^\\\\wsl(?:\.localhost|\$)\\([^\\]+)(.*)$")
36 .ok()
37 .and_then(|r| r.captures(windows_path))
38 {
39 if caps.get(1).map(|m| m.as_str()) != Some(distro.as_str()) {
40 return windows_path.to_string();
42 }
43 }
44 }
45
46 if let Ok(result) = Command::new("wslpath").args(["-u", windows_path]).output() {
48 if result.status.success() {
49 return String::from_utf8_lossy(&result.stdout).trim().to_string();
50 }
51 }
52
53 let result = windows_path.replace('\\', "/");
56
57 if result.len() >= 2 && result.chars().nth(1) == Some(':') {
59 let letter = result.chars().next().unwrap().to_ascii_lowercase();
60 return format!("/mnt/{}{}", letter, &result[2..]);
61 }
62
63 result
64 }
65
66 fn to_ide_path(&self, wsl_path: &str) -> String {
67 if wsl_path.is_empty() {
68 return wsl_path.to_string();
69 }
70
71 if let Ok(result) = Command::new("wslpath").args(["-w", wsl_path]).output() {
73 if result.status.success() {
74 return String::from_utf8_lossy(&result.stdout).trim().to_string();
75 }
76 }
77
78 wsl_path.to_string()
80 }
81}
82
83pub fn check_wsl_distro_match(windows_path: &str, wsl_distro_name: &str) -> bool {
92 if let Some(caps) = regex::Regex::new(r"^\\\\wsl(?:\.localhost|\$)\\([^\\]+)(.*)$")
93 .ok()
94 .and_then(|r| r.captures(windows_path))
95 {
96 caps.get(1).map(|m| m.as_str()) == Some(wsl_distro_name)
97 } else {
98 true }
100}