Skip to content

Commit d039d32

Browse files
authored
Merge pull request #144 from lu-zero/simplify-run
Simplify running the command
2 parents 947aea5 + 393a252 commit d039d32

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/lib.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub fn target_supported() -> bool {
270270
pub fn get_variable(package: &str, variable: &str) -> Result<String, Error> {
271271
let arg = format!("--variable={}", variable);
272272
let cfg = Config::new();
273-
let out = run(cfg.command(package, &[&arg]))?;
273+
let out = cfg.run(package, &[&arg])?;
274274
Ok(str::from_utf8(&out).unwrap().trim_end().to_owned())
275275
}
276276

@@ -392,17 +392,19 @@ impl Config {
392392

393393
let mut library = Library::new();
394394

395-
let output = run(self.command(name, &["--libs", "--cflags"])).map_err(|e| match e {
396-
Error::Failure { command, output } => Error::ProbeFailure {
397-
name: name.to_owned(),
398-
command,
399-
output,
400-
},
401-
other => other,
402-
})?;
395+
let output = self
396+
.run(name, &["--libs", "--cflags"])
397+
.map_err(|e| match e {
398+
Error::Failure { command, output } => Error::ProbeFailure {
399+
name: name.to_owned(),
400+
command,
401+
output,
402+
},
403+
other => other,
404+
})?;
403405
library.parse_libs_cflags(name, &output, self);
404406

405-
let output = run(self.command(name, &["--modversion"]))?;
407+
let output = self.run(name, &["--modversion"])?;
406408
library.parse_modversion(str::from_utf8(&output).unwrap());
407409

408410
Ok(library)
@@ -474,10 +476,31 @@ impl Config {
474476
self.statik.unwrap_or_else(|| self.infer_static(name))
475477
}
476478

477-
fn command(&self, name: &str, args: &[&str]) -> Command {
479+
fn run(&self, name: &str, args: &[&str]) -> Result<Vec<u8>, Error> {
478480
let exe = self
479481
.targetted_env_var("PKG_CONFIG")
480482
.unwrap_or_else(|| OsString::from("pkg-config"));
483+
484+
let mut cmd = self.command(exe, name, args);
485+
match cmd.output() {
486+
Ok(output) => {
487+
if output.status.success() {
488+
Ok(output.stdout)
489+
} else {
490+
Err(Error::Failure {
491+
command: format!("{:?}", cmd),
492+
output,
493+
})
494+
}
495+
}
496+
Err(cause) => Err(Error::Command {
497+
command: format!("{:?}", cmd),
498+
cause,
499+
}),
500+
}
501+
}
502+
503+
fn command(&self, exe: OsString, name: &str, args: &[&str]) -> Command {
481504
let mut cmd = Command::new(exe);
482505
if self.is_static(name) {
483506
cmd.arg("--static");
@@ -815,25 +838,6 @@ fn is_static_available(name: &str, system_roots: &[PathBuf], dirs: &[PathBuf]) -
815838
})
816839
}
817840

818-
fn run(mut cmd: Command) -> Result<Vec<u8>, Error> {
819-
match cmd.output() {
820-
Ok(output) => {
821-
if output.status.success() {
822-
Ok(output.stdout)
823-
} else {
824-
Err(Error::Failure {
825-
command: format!("{:?}", cmd),
826-
output,
827-
})
828-
}
829-
}
830-
Err(cause) => Err(Error::Command {
831-
command: format!("{:?}", cmd),
832-
cause,
833-
}),
834-
}
835-
}
836-
837841
/// Split output produced by pkg-config --cflags and / or --libs into separate flags.
838842
///
839843
/// Backslash in output is used to preserve literal meaning of following byte. Different words are

0 commit comments

Comments
 (0)