fix: Path::canonicalize -> normalize_path
* Nix doesn't require path to exist
This commit is contained in:
@@ -2,11 +2,28 @@
|
|||||||
#![allow(clippy::unwrap_used)]
|
#![allow(clippy::unwrap_used)]
|
||||||
|
|
||||||
use rnix::ast::{self, Expr, HasEntry};
|
use rnix::ast::{self, Expr, HasEntry};
|
||||||
|
use std::path::{Component, Path as StdPath, PathBuf};
|
||||||
|
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
fn normalize_path(path: &StdPath) -> String {
|
||||||
|
let mut normalized = PathBuf::new();
|
||||||
|
for component in path.components() {
|
||||||
|
match component {
|
||||||
|
Component::Prefix(p) => normalized.push(p.as_os_str()),
|
||||||
|
Component::RootDir => normalized.push("/"),
|
||||||
|
Component::CurDir => {}
|
||||||
|
Component::ParentDir => {
|
||||||
|
normalized.pop();
|
||||||
|
}
|
||||||
|
Component::Normal(c) => normalized.push(c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
normalized.to_string_lossy().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Downgrade<Ctx: DowngradeContext> {
|
pub trait Downgrade<Ctx: DowngradeContext> {
|
||||||
fn downgrade(self, ctx: &mut Ctx) -> Result<ExprId>;
|
fn downgrade(self, ctx: &mut Ctx) -> Result<ExprId>;
|
||||||
}
|
}
|
||||||
@@ -74,21 +91,10 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Path {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let resolved_path = if path_str.starts_with('/') {
|
let resolved_path = if path_str.starts_with('/') {
|
||||||
path_str
|
normalize_path(&std::path::PathBuf::from(&path_str))
|
||||||
} else {
|
} else {
|
||||||
let current_dir = ctx.get_current_dir();
|
let current_dir = ctx.get_current_dir();
|
||||||
|
normalize_path(¤t_dir.join(&path_str))
|
||||||
current_dir
|
|
||||||
.join(&path_str)
|
|
||||||
.canonicalize()
|
|
||||||
.map_err(|e| {
|
|
||||||
crate::error::Error::downgrade_error(format!(
|
|
||||||
"Failed to resolve path {}: {}",
|
|
||||||
path_str, e
|
|
||||||
))
|
|
||||||
})?
|
|
||||||
.to_string_lossy()
|
|
||||||
.to_string()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
vec![ctx.new_expr(Str { val: resolved_path }.to_ir())]
|
vec![ctx.new_expr(Str { val: resolved_path }.to_ir())]
|
||||||
|
|||||||
Reference in New Issue
Block a user