fast_walker/
utils.rs

1use std::{
2    fmt::{Formatter, Write},
3    fs::DirEntry,
4    path::{Path, PathBuf},
5};
6
7/// Convert a path to unix style path.
8///
9/// # Examples
10///
11/// ```
12/// # use fast_walker::utils::to_unix_path;
13/// let path = std::path::Path::new("C:\\P4Root\\test.txt");
14/// ```
15pub fn to_unix_path(path: &Path) -> String {
16    #[cfg(target_os = "windows")]
17    {
18        path.to_string_lossy().trim_start_matches("\\\\?\\").replace("\\", "/")
19    }
20    #[cfg(not(target_os = "windows"))]
21    {
22        path.to_string_lossy().to_string()
23    }
24}
25
26/// Get path and file name from a DirEntry.
27pub fn path_info(entry: std::io::Result<DirEntry>) -> std::io::Result<(PathBuf, String)> {
28    let entry = entry?;
29    let name = match entry.file_name().to_str() {
30        Some(s) => s.to_string(),
31        None => Err(std::io::Error::new(std::io::ErrorKind::Other, "file name is not utf-8"))?,
32    };
33    Ok((entry.path().canonicalize()?, name))
34}
35
36#[allow(dead_code)]
37pub(crate) fn write_unix_path(f: &mut Formatter<'_>, path: &Path) -> std::fmt::Result {
38    for c in path.to_string_lossy().trim_start_matches("\\\\?\\").chars() {
39        if c == '\\' { f.write_char('/')? } else { f.write_char(c)? }
40    }
41    Ok(())
42}