Skip to main content

flowey_lib_common/_util/
wslpath.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use flowey::node::prelude::Context;
5use flowey::node::prelude::RustRuntimeServices;
6use flowey::node::prelude::anyhow;
7use std::path::Path;
8use std::path::PathBuf;
9
10fn convert(
11    rt: &RustRuntimeServices<'_>,
12    path: &Path,
13    absolute_windows_path: bool,
14) -> anyhow::Result<PathBuf> {
15    let converted = if absolute_windows_path {
16        flowey::shell_cmd!(rt, "wslpath -aw {path}")
17            .quiet()
18            .read()?
19    } else {
20        flowey::shell_cmd!(rt, "wslpath {path}").quiet().read()?
21    };
22
23    anyhow::ensure!(
24        !converted.trim().is_empty(),
25        "wslpath returned an empty path for {}",
26        path.display()
27    );
28
29    Ok(converted.trim().into())
30}
31
32pub fn win_to_linux(
33    rt: &RustRuntimeServices<'_>,
34    path: impl AsRef<Path>,
35) -> anyhow::Result<PathBuf> {
36    let path = path.as_ref();
37    convert(rt, path, false)
38        .with_context(|| format!("failed to convert Windows path {}", path.display()))
39}
40
41pub fn linux_to_win(
42    rt: &RustRuntimeServices<'_>,
43    path: impl AsRef<Path>,
44) -> anyhow::Result<PathBuf> {
45    let path = path.as_ref();
46    convert(rt, path, true)
47        .with_context(|| format!("failed to convert Linux path {}", path.display()))
48}