use std::process::Command;
fn wsl_paths(
path: &str,
distro: Option<String>,
to_linux_path: bool,
) -> Result<String, Box<dyn std::error::Error>> {
let distro_args: Vec<String> = match distro {
Some(distro_name) => vec!["-d".to_string(), distro_name],
None => vec![],
};
let path_arg = {
if to_linux_path {
"-a".to_string()
} else {
"-m".to_string()
}
};
let stdout = Command::new("wsl.exe")
.args(distro_args)
.arg("-e")
.arg("wslpath")
.arg(path_arg)
.arg(path.replace("\\", "\\\\"))
.output()?
.stdout;
let wsl_path = std::str::from_utf8(&stdout)?.trim().to_string();
Ok(wsl_path)
}
pub fn wsl_to_windows_with_distro(
path: &str,
distro: String,
) -> Result<String, Box<dyn std::error::Error>> {
wsl_paths(path, Some(distro), false)
}
pub fn wsl_to_windows(path: &str) -> Result<String, Box<dyn std::error::Error>> {
wsl_paths(path, None, false)
}
pub fn windows_to_wsl_with_distro(
path: &str,
distro: String,
) -> Result<String, Box<dyn std::error::Error>> {
wsl_paths(path, Some(distro), true)
}
pub fn windows_to_wsl(path: &str) -> Result<String, Box<dyn std::error::Error>> {
wsl_paths(path, None, true)
}
#[cfg(test)]
mod tests {
use crate::{windows_to_wsl, wsl_to_windows};
#[test]
fn test_wsl_to_windows() {
assert_eq!(wsl_to_windows("/mnt/c").unwrap_or_default(), "C:/");
}
#[test]
fn test_windows_to_wsl() {
assert_eq!(windows_to_wsl("C:/").unwrap_or_default(), "/mnt/c/");
}
}