supa_mdx_lint/utils/
path.rs

1use std::{
2    ops::Deref,
3    path::{Component, Path, PathBuf},
4};
5
6pub(crate) struct IsGlob(pub bool);
7
8impl Deref for IsGlob {
9    type Target = bool;
10
11    fn deref(&self) -> &Self::Target {
12        &self.0
13    }
14}
15
16fn normalize_glob_path(path: &Path) -> PathBuf {
17    let components = path.components();
18    let mut result = PathBuf::new();
19
20    for component in components {
21        match component {
22            Component::ParentDir => {
23                result.pop();
24            }
25            Component::CurDir => {}
26            _ => {
27                result.push(component);
28            }
29        }
30    }
31
32    result
33}
34
35/// Normalize a path for pattern comparison by canonicalizing it, and replacing
36/// Windows-specific features:
37/// - Replace ackslashes with slashes
38/// - Remove extended-length prefix
39pub(crate) fn normalize_path(path: &Path, is_glob: IsGlob) -> String {
40    let path = if *is_glob {
41        normalize_glob_path(path)
42    } else {
43        path.canonicalize().unwrap_or(path.into())
44    };
45    let mut path_str = path.to_string_lossy().replace("\\", "/");
46    if path_str.starts_with("//?/") {
47        path_str = path_str[4..].to_string();
48    }
49    path_str
50}