1600 lines · 54.2 KB · Rust
RawDownload
use globset::{Glob, GlobSet, GlobSetBuilder};use regex::Regex;use serde::{Deserialize, Serialize};use std::cmp::Ordering;use std::fmt::{Display, Formatter};use std::mem;use std::path::StripPrefixError;use std::sync::{Arc, OnceLock};use std::{    ffi::OsStr,    path::{Path, PathBuf},    sync::LazyLock,}; /// Returns the path to the user's home directory.pub fn home_dir() -> &'static PathBuf {    static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();    HOME_DIR.get_or_init(|| dirs::home_dir().expect("failed to determine home directory"))} pub trait PathExt {    fn compact(&self) -> PathBuf;    fn extension_or_hidden_file_name(&self) -> Option<&str>;    fn to_sanitized_string(&self) -> String;    fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>    where        Self: From<&'a Path>,    {        #[cfg(unix)]        {            use std::os::unix::prelude::OsStrExt;            Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))        }        #[cfg(windows)]        {            use anyhow::Context as _;            use tendril::fmt::{Format, WTF8};            WTF8::validate(bytes)                .then(|| {                    // Safety: bytes are valid WTF-8 sequence.                    Self::from(Path::new(unsafe {                        OsStr::from_encoded_bytes_unchecked(bytes)                    }))                })                .with_context(|| format!("Invalid WTF-8 sequence: {bytes:?}"))        }    }} impl<T: AsRef<Path>> PathExt for T {    /// Compacts a given file path by replacing the user's home directory    /// prefix with a tilde (`~`).    ///    /// # Returns    ///    /// * A `PathBuf` containing the compacted file path. If the input path    ///   does not have the user's home directory prefix, or if we are not on    ///   Linux or macOS, the original path is returned unchanged.    fn compact(&self) -> PathBuf {        if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {            match self.as_ref().strip_prefix(home_dir().as_path()) {                Ok(relative_path) => {                    let mut shortened_path = PathBuf::new();                    shortened_path.push("~");                    shortened_path.push(relative_path);                    shortened_path                }                Err(_) => self.as_ref().to_path_buf(),            }        } else {            self.as_ref().to_path_buf()        }    }     /// Returns a file's extension or, if the file is hidden, its name without the leading dot    fn extension_or_hidden_file_name(&self) -> Option<&str> {        let path = self.as_ref();        let file_name = path.file_name()?.to_str()?;        if file_name.starts_with('.') {            return file_name.strip_prefix('.');        }         path.extension()            .and_then(|e| e.to_str())            .or_else(|| path.file_stem()?.to_str())    }     /// Returns a sanitized string representation of the path.    /// Note, on Windows, this assumes that the path is a valid UTF-8 string and    /// is not a UNC path.    fn to_sanitized_string(&self) -> String {        #[cfg(target_os = "windows")]        {            self.as_ref().to_string_lossy().replace("/", "\\")        }        #[cfg(not(target_os = "windows"))]        {            self.as_ref().to_string_lossy().to_string()        }    }} /// In memory, this is identical to `Path`. On non-Windows conversions to this type are no-ops. On/// windows, these conversions sanitize UNC paths by removing the `\\\\?\\` prefix.#[derive(Eq, PartialEq, Hash, Ord, PartialOrd)]#[repr(transparent)]pub struct SanitizedPath(Path); impl SanitizedPath {    pub fn new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {        #[cfg(not(target_os = "windows"))]        return Self::unchecked_new(path.as_ref());         #[cfg(target_os = "windows")]        return Self::unchecked_new(dunce::simplified(path.as_ref()));    }     pub fn unchecked_new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl        unsafe { mem::transmute::<&Path, &Self>(path.as_ref()) }    }     pub fn from_arc(path: Arc<Path>) -> Arc<Self> {        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl        #[cfg(not(target_os = "windows"))]        return unsafe { mem::transmute::<Arc<Path>, Arc<Self>>(path) };         // TODO: could avoid allocating here if dunce::simplified results in the same path        #[cfg(target_os = "windows")]        return Self::new(&path).into();    }     pub fn new_arc<T: AsRef<Path> + ?Sized>(path: &T) -> Arc<Self> {        Self::new(path).into()    }     pub fn cast_arc(path: Arc<Self>) -> Arc<Path> {        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl        unsafe { mem::transmute::<Arc<Self>, Arc<Path>>(path) }    }     pub fn cast_arc_ref(path: &Arc<Self>) -> &Arc<Path> {        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl        unsafe { mem::transmute::<&Arc<Self>, &Arc<Path>>(path) }    }     pub fn starts_with(&self, prefix: &Self) -> bool {        self.0.starts_with(&prefix.0)    }     pub fn as_path(&self) -> &Path {        &self.0    }     pub fn file_name(&self) -> Option<&std::ffi::OsStr> {        self.0.file_name()    }     pub fn extension(&self) -> Option<&std::ffi::OsStr> {        self.0.extension()    }     pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {        self.0.join(path)    }     pub fn parent(&self) -> Option<&Self> {        self.0.parent().map(Self::unchecked_new)    }     pub fn strip_prefix(&self, base: &Self) -> Result<&Path, StripPrefixError> {        self.0.strip_prefix(base.as_path())    }     pub fn to_str(&self) -> Option<&str> {        self.0.to_str()    }     pub fn to_path_buf(&self) -> PathBuf {        self.0.to_path_buf()    }     pub fn to_glob_string(&self) -> String {        #[cfg(target_os = "windows")]        {            self.0.to_string_lossy().replace("/", "\\")        }        #[cfg(not(target_os = "windows"))]        {            self.0.to_string_lossy().to_string()        }    }} impl std::fmt::Debug for SanitizedPath {    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {        std::fmt::Debug::fmt(&self.0, formatter)    }} impl Display for SanitizedPath {    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {        write!(f, "{}", self.0.display())    }} impl From<&SanitizedPath> for Arc<SanitizedPath> {    fn from(sanitized_path: &SanitizedPath) -> Self {        let path: Arc<Path> = sanitized_path.0.into();        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl        unsafe { mem::transmute(path) }    }} impl From<&SanitizedPath> for PathBuf {    fn from(sanitized_path: &SanitizedPath) -> Self {        sanitized_path.as_path().into()    }} impl AsRef<Path> for SanitizedPath {    fn as_ref(&self) -> &Path {        &self.0    }} #[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum PathStyle {    Posix,    Windows,} impl PathStyle {    #[cfg(target_os = "windows")]    pub const fn current() -> Self {        PathStyle::Windows    }     #[cfg(not(target_os = "windows"))]    pub const fn current() -> Self {        PathStyle::Posix    }     #[inline]    pub fn separator(&self) -> &str {        match self {            PathStyle::Posix => "/",            PathStyle::Windows => "\\",        }    }} #[derive(Debug, Clone)]pub struct RemotePathBuf {    inner: PathBuf,    style: PathStyle,    string: String, // Cached string representation} impl RemotePathBuf {    pub fn new(path: PathBuf, style: PathStyle) -> Self {        #[cfg(target_os = "windows")]        let string = match style {            PathStyle::Posix => path.to_string_lossy().replace('\\', "/"),            PathStyle::Windows => path.to_string_lossy().into(),        };        #[cfg(not(target_os = "windows"))]        let string = match style {            PathStyle::Posix => path.to_string_lossy().to_string(),            PathStyle::Windows => path.to_string_lossy().replace('/', "\\"),        };        Self {            inner: path,            style,            string,        }    }     pub fn from_str(path: &str, style: PathStyle) -> Self {        let path_buf = PathBuf::from(path);        Self::new(path_buf, style)    }     #[cfg(target_os = "windows")]    pub fn to_proto(&self) -> String {        match self.path_style() {            PathStyle::Posix => self.to_string(),            PathStyle::Windows => self.inner.to_string_lossy().replace('\\', "/"),        }    }     #[cfg(not(target_os = "windows"))]    pub fn to_proto(&self) -> String {        match self.path_style() {            PathStyle::Posix => self.inner.to_string_lossy().to_string(),            PathStyle::Windows => self.to_string(),        }    }     pub fn as_path(&self) -> &Path {        &self.inner    }     pub fn path_style(&self) -> PathStyle {        self.style    }     pub fn parent(&self) -> Option<RemotePathBuf> {        self.inner            .parent()            .map(|p| RemotePathBuf::new(p.to_path_buf(), self.style))    }} impl Display for RemotePathBuf {    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {        write!(f, "{}", self.string)    }} /// A delimiter to use in `path_query:row_number:column_number` strings parsing.pub const FILE_ROW_COLUMN_DELIMITER: char = ':'; const ROW_COL_CAPTURE_REGEX: &str = r"(?xs)    ([^\(]+)\:(?:        \((\d+)[,:](\d+)\) # filename:(row,column), filename:(row:column)        |        \((\d+)\)()     # filename:(row)    )    |    ([^\(]+)(?:        \((\d+)[,:](\d+)\) # filename(row,column), filename(row:column)        |        \((\d+)\)()     # filename(row)    )    |    (.+?)(?:        \:+(\d+)\:(\d+)\:*$  # filename:row:column        |        \:+(\d+)\:*()$       # filename:row    )"; /// A representation of a path-like string with optional row and column numbers./// Matching values example: `te`, `test.rs:22`, `te:22:5`, `test.c(22)`, `test.c(22,5)`etc.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]pub struct PathWithPosition {    pub path: PathBuf,    pub row: Option<u32>,    // Absent if row is absent.    pub column: Option<u32>,} impl PathWithPosition {    /// Returns a PathWithPosition from a path.    pub fn from_path(path: PathBuf) -> Self {        Self {            path,            row: None,            column: None,        }    }     /// Parses a string that possibly has `:row:column` or `(row, column)` suffix.    /// Parenthesis format is used by [MSBuild](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-diagnostic-format-for-tasks) compatible tools    /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.    /// If the suffix parsing fails, the whole string is parsed as a path.    ///    /// Be mindful that `test_file:10:1:` is a valid posix filename.    /// `PathWithPosition` class assumes that the ending position-like suffix is **not** part of the filename.    ///    /// # Examples    ///    /// ```    /// # use util::paths::PathWithPosition;    /// # use std::path::PathBuf;    /// assert_eq!(PathWithPosition::parse_str("test_file"), PathWithPosition {    ///     path: PathBuf::from("test_file"),    ///     row: None,    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file:10"), PathWithPosition {    ///     path: PathBuf::from("test_file"),    ///     row: Some(10),    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs"),    ///     row: None,    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs"),    ///     row: Some(1),    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs"),    ///     row: Some(1),    ///     column: Some(2),    /// });    /// ```    ///    /// # Expected parsing results when encounter ill-formatted inputs.    /// ```    /// # use util::paths::PathWithPosition;    /// # use std::path::PathBuf;    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs:a"),    ///     row: None,    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a:b"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs:a:b"),    ///     row: None,    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs::"),    ///     row: None,    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs"),    ///     row: Some(1),    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs"),    ///     row: Some(1),    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1:2"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs"),    ///     row: Some(1),    ///     column: Some(2),    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::2"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs:1"),    ///     row: Some(2),    ///     column: None,    /// });    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2:3"), PathWithPosition {    ///     path: PathBuf::from("test_file.rs:1"),    ///     row: Some(2),    ///     column: Some(3),    /// });    /// ```    pub fn parse_str(s: &str) -> Self {        let trimmed = s.trim();        let path = Path::new(trimmed);        let maybe_file_name_with_row_col = path.file_name().unwrap_or_default().to_string_lossy();        if maybe_file_name_with_row_col.is_empty() {            return Self {                path: Path::new(s).to_path_buf(),                row: None,                column: None,            };        }         // Let's avoid repeated init cost on this. It is subject to thread contention, but        // so far this code isn't called from multiple hot paths. Getting contention here        // in the future seems unlikely.        static SUFFIX_RE: LazyLock<Regex> =            LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());        match SUFFIX_RE            .captures(&maybe_file_name_with_row_col)            .map(|caps| caps.extract())        {            Some((_, [file_name, maybe_row, maybe_column])) => {                let row = maybe_row.parse::<u32>().ok();                let column = maybe_column.parse::<u32>().ok();                 let suffix_length = maybe_file_name_with_row_col.len() - file_name.len();                let path_without_suffix = &trimmed[..trimmed.len() - suffix_length];                 Self {                    path: Path::new(path_without_suffix).to_path_buf(),                    row,                    column,                }            }            None => {                // The `ROW_COL_CAPTURE_REGEX` deals with separated digits only,                // but in reality there could be `foo/bar.py:22:in` inputs which we want to match too.                // The regex mentioned is not very extendable with "digit or random string" checks, so do this here instead.                let delimiter = ':';                let mut path_parts = s                    .rsplitn(3, delimiter)                    .collect::<Vec<_>>()                    .into_iter()                    .rev()                    .fuse();                let mut path_string = path_parts.next().expect("rsplitn should have the rest of the string as its last parameter that we reversed").to_owned();                let mut row = None;                let mut column = None;                if let Some(maybe_row) = path_parts.next() {                    if let Ok(parsed_row) = maybe_row.parse::<u32>() {                        row = Some(parsed_row);                        if let Some(parsed_column) = path_parts                            .next()

Lines 1–500 of 1600.