Add AbsPath::absolutize

This commit is contained in:
Lukas Wirth 2023-05-13 11:51:28 +02:00
parent 939ebb4454
commit f47caa666e
2 changed files with 13 additions and 7 deletions

View file

@ -140,6 +140,11 @@ impl AbsPath {
self.0.parent().map(AbsPath::assert) 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<Path>) -> AbsPathBuf {
self.join(path).normalize()
}
/// Equivalent of [`Path::join`] for `AbsPath`. /// Equivalent of [`Path::join`] for `AbsPath`.
pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf { pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf {
self.as_ref().join(path).try_into().unwrap() self.as_ref().join(path).try_into().unwrap()

View file

@ -98,24 +98,23 @@ impl ProjectJson {
/// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via /// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via
/// configuration. /// configuration.
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson { pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
let absolutize = let absolutize_on_base = |p| base.absolutize(p);
|p| AbsPathBuf::try_from(p).unwrap_or_else(|path| base.join(&path)).normalize();
ProjectJson { ProjectJson {
sysroot: data.sysroot.map(absolutize), sysroot: data.sysroot.map(absolutize_on_base),
sysroot_src: data.sysroot_src.map(absolutize), sysroot_src: data.sysroot_src.map(absolutize_on_base),
project_root: base.to_path_buf(), project_root: base.to_path_buf(),
crates: data crates: data
.crates .crates
.into_iter() .into_iter()
.map(|crate_data| { .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 let is_workspace_member = crate_data
.is_workspace_member .is_workspace_member
.unwrap_or_else(|| root_module.starts_with(base)); .unwrap_or_else(|| root_module.starts_with(base));
let (include, exclude) = match crate_data.source { let (include, exclude) = match crate_data.source {
Some(src) => { Some(src) => {
let absolutize = |dirs: Vec<PathBuf>| { let absolutize = |dirs: Vec<PathBuf>| {
dirs.into_iter().map(absolutize).collect::<Vec<_>>() dirs.into_iter().map(absolutize_on_base).collect::<Vec<_>>()
}; };
(absolutize(src.include_dirs), absolutize(src.exclude_dirs)) (absolutize(src.include_dirs), absolutize(src.exclude_dirs))
} }
@ -142,7 +141,9 @@ impl ProjectJson {
cfg: crate_data.cfg, cfg: crate_data.cfg,
target: crate_data.target, target: crate_data.target,
env: crate_data.env, 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, is_workspace_member,
include, include,
exclude, exclude,