Skip to content

Commit b45fa91

Browse files
committed
Borrow build paths in objdiff job
1 parent 51c3af2 commit b45fa91

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

objdiff-core/src/jobs/objdiff.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{sync::mpsc::Receiver, task::Waker};
22

33
use anyhow::{Error, Result, bail};
44
use time::OffsetDateTime;
5-
use typed_path::Utf8PlatformPathBuf;
5+
use typed_path::{Utf8PlatformPath, Utf8PlatformPathBuf, Utf8UnixPathBuf};
66

77
use crate::{
88
build::{BuildConfig, BuildStatus, run_make},
@@ -29,36 +29,37 @@ pub struct ObjDiffResult {
2929
pub time: OffsetDateTime,
3030
}
3131

32+
fn build_relative_path(
33+
project_dir: Option<&Utf8PlatformPath>,
34+
path: Option<&Utf8PlatformPath>,
35+
should_build: bool,
36+
label: &str,
37+
) -> Result<Option<Utf8UnixPathBuf>> {
38+
if !should_build {
39+
return Ok(None);
40+
}
41+
let project_dir = project_dir.ok_or_else(|| Error::msg("Missing project dir"))?;
42+
let Some(path) = path else { return Ok(None) };
43+
match path.strip_prefix(project_dir) {
44+
Ok(p) => Ok(Some(p.with_unix_encoding())),
45+
Err(_) => bail!("{label} path '{path}' doesn't begin with '{project_dir}'"),
46+
}
47+
}
48+
3249
fn run_build(
3350
context: &JobContext,
3451
cancel: Receiver<()>,
3552
config: ObjDiffConfig,
3653
) -> Result<Box<ObjDiffResult>> {
37-
let mut target_path_rel = None;
38-
let mut base_path_rel = None;
39-
if config.build_target || config.build_base {
40-
let project_dir = config
41-
.build_config
42-
.project_dir
43-
.as_ref()
44-
.ok_or_else(|| Error::msg("Missing project dir"))?;
45-
if let Some(target_path) = &config.target_path {
46-
target_path_rel = match target_path.strip_prefix(project_dir) {
47-
Ok(p) => Some(p.with_unix_encoding()),
48-
Err(_) => {
49-
bail!("Target path '{}' doesn't begin with '{}'", target_path, project_dir);
50-
}
51-
};
52-
}
53-
if let Some(base_path) = &config.base_path {
54-
base_path_rel = match base_path.strip_prefix(project_dir) {
55-
Ok(p) => Some(p.with_unix_encoding()),
56-
Err(_) => {
57-
bail!("Base path '{}' doesn't begin with '{}'", base_path, project_dir);
58-
}
59-
};
60-
};
61-
}
54+
let project_dir = config.build_config.project_dir.as_deref();
55+
let target_path_rel = build_relative_path(
56+
project_dir,
57+
config.target_path.as_deref(),
58+
config.build_target,
59+
"Target",
60+
)?;
61+
let base_path_rel =
62+
build_relative_path(project_dir, config.base_path.as_deref(), config.build_base, "Base")?;
6263

6364
let mut total = 1;
6465
if config.build_target && target_path_rel.is_some() {

0 commit comments

Comments
 (0)