1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::path::{Component, Path, PathBuf};

use crate::{RResult, ResolveResult, Resolver, ResolverResult};

impl Resolver {
    #[cfg(not(target_os = "windows"))]
    fn adjust(p: PathBuf) -> String {
        p.display().to_string()
    }

    /// Eliminate `\\?\` prefix in windows.
    /// reference: https://stackoverflow.com/questions/41233684/why-does-my-canonicalized-path-get-prefixed-with
    #[cfg(target_os = "windows")]
    fn adjust(p: PathBuf) -> String {
        const VERBATIM_PREFIX: &str = r#"\\?\"#;
        let p = p.display().to_string();
        if p.starts_with(VERBATIM_PREFIX) {
            p[VERBATIM_PREFIX.len()..].to_string()
        } else {
            p
        }
    }

    pub fn normalize_path(&self, path: &Path, query: &str, fragment: &str) -> RResult<PathBuf> {
        if self.options.symlinks {
            Path::canonicalize(path)
                .map_err(|_| "Path normalized failed".to_string())
                .map(|result| {
                    PathBuf::from(format!("{}{}{}", Self::adjust(result), query, fragment))
                })
        } else {
            let result = path
                .components()
                .fold(PathBuf::new(), |mut acc, path_component| {
                    match path_component {
                        Component::Prefix(prefix) => acc.push(prefix.as_os_str()),
                        Component::Normal(name) => acc.push(name),
                        Component::RootDir => acc.push("/"),
                        Component::CurDir => {}
                        Component::ParentDir => {
                            acc.pop();
                        }
                    }
                    acc
                });
            Ok(PathBuf::from(format!(
                "{}{}{}",
                result.to_str().unwrap(),
                query,
                fragment
            )))
        }
    }

    pub fn normalize_result(
        &self,
        result: ResolveResult,
        query: &str,
        fragment: &str,
    ) -> ResolverResult {
        match result {
            ResolveResult::Path(path) => self
                .normalize_path(&path, query, fragment)
                .map(ResolveResult::Path),
            ResolveResult::Ignored => Ok(ResolveResult::Ignored),
        }
    }
}