From f47caa666e0a2f7dd9ee9ac2535a2d26b301d4fb Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 13 May 2023 11:51:28 +0200 Subject: [PATCH] Add AbsPath::absolutize --- crates/paths/src/lib.rs | 5 +++++ crates/project-model/src/project_json.rs | 15 ++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/paths/src/lib.rs b/crates/paths/src/lib.rs index 0cfbf1d9a1..e0c20a4143 100644 --- a/crates/paths/src/lib.rs +++ b/crates/paths/src/lib.rs @@ -140,6 +140,11 @@ impl AbsPath { self.0.parent().map(AbsPath::assert) } + /// Equivalent of [`Path::join`] for `AbsPath` with an additional normalize step afterwards. + pub fn absolutize(&self, path: impl AsRef) -> AbsPathBuf { + self.join(path).normalize() + } + /// Equivalent of [`Path::join`] for `AbsPath`. pub fn join(&self, path: impl AsRef) -> AbsPathBuf { self.as_ref().join(path).try_into().unwrap() diff --git a/crates/project-model/src/project_json.rs b/crates/project-model/src/project_json.rs index ede2dc769e..80897f7478 100644 --- a/crates/project-model/src/project_json.rs +++ b/crates/project-model/src/project_json.rs @@ -98,24 +98,23 @@ impl ProjectJson { /// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via /// configuration. pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson { - let absolutize = - |p| AbsPathBuf::try_from(p).unwrap_or_else(|path| base.join(&path)).normalize(); + let absolutize_on_base = |p| base.absolutize(p); ProjectJson { - sysroot: data.sysroot.map(absolutize), - sysroot_src: data.sysroot_src.map(absolutize), + sysroot: data.sysroot.map(absolutize_on_base), + sysroot_src: data.sysroot_src.map(absolutize_on_base), project_root: base.to_path_buf(), crates: data .crates .into_iter() .map(|crate_data| { - let root_module = absolutize(crate_data.root_module); + let root_module = absolutize_on_base(crate_data.root_module); let is_workspace_member = crate_data .is_workspace_member .unwrap_or_else(|| root_module.starts_with(base)); let (include, exclude) = match crate_data.source { Some(src) => { let absolutize = |dirs: Vec| { - dirs.into_iter().map(absolutize).collect::>() + dirs.into_iter().map(absolutize_on_base).collect::>() }; (absolutize(src.include_dirs), absolutize(src.exclude_dirs)) } @@ -142,7 +141,9 @@ impl ProjectJson { cfg: crate_data.cfg, target: crate_data.target, env: crate_data.env, - proc_macro_dylib_path: crate_data.proc_macro_dylib_path.map(absolutize), + proc_macro_dylib_path: crate_data + .proc_macro_dylib_path + .map(absolutize_on_base), is_workspace_member, include, exclude,