mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-21 18:10:42 -04:00
Better more reliable path normalization
This commit is contained in:
parent
04a97a7002
commit
5a20012fcb
@ -692,31 +692,24 @@ impl Rewriter {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_path(p: PathBuf) -> PathBuf {
|
||||
let mut normalized = PathBuf::new();
|
||||
for segment in p.components() {
|
||||
normalized.push(segment);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
impl walk::Walker for Rewriter {
|
||||
fn visit_expression(&mut self, expr: &mut Expression) {
|
||||
// Rewrite all paths except for stdlib paths to absolute.
|
||||
let main_separator = format!("{}", std::path::MAIN_SEPARATOR);
|
||||
if let Expression::Include(ref mut def) = expr {
|
||||
let path = PathBuf::from(&def.path.fragment);
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
if path.is_relative() {
|
||||
def.path.fragment = self
|
||||
.base
|
||||
.join(path)
|
||||
.canonicalize()
|
||||
.unwrap()
|
||||
def.path.fragment = normalize_path(self.base.join(path))
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if path.is_relative() {
|
||||
def.path.fragment = self.base.join(path).to_string_lossy().to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Expression::Import(ref mut def) = expr {
|
||||
let path = PathBuf::from(
|
||||
&def.path
|
||||
@ -728,26 +721,11 @@ impl walk::Walker for Rewriter {
|
||||
if path.starts_with(format!("std{}", main_separator)) {
|
||||
return;
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
if path.is_relative() {
|
||||
def.path.fragment = self
|
||||
.base
|
||||
.join(path)
|
||||
.canonicalize()
|
||||
.unwrap()
|
||||
def.path.fragment = normalize_path(self.base.join(path))
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if path.is_relative() {
|
||||
def.path.fragment = self.base.join(path).to_string_lossy().to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// RangeDef defines a range with optional step.
|
||||
|
Loading…
x
Reference in New Issue
Block a user