Skip to content

Commit e0c0daa

Browse files
committed
One-liner version of error message
1 parent 1a86c38 commit e0c0daa

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,49 @@ impl fmt::Display for Error {
427427
}
428428
}
429429

430+
impl Error {
431+
/// Returns a concise version of the error message
432+
pub fn error_message(&self) -> String {
433+
434+
// Extracts just the command name from WrappedCommand's output.
435+
// The command name alone is not available in the Error enum.
436+
fn trimmed_command(command: &str) -> &str {
437+
let env_prefix = command.split_inclusive(' ')
438+
.take_while(|arg| arg.starts_with("PKG_CONFIG") && arg.contains('='))
439+
.map(|arg| arg.len())
440+
.sum::<usize>();
441+
command[env_prefix..].split(" --").next().unwrap()
442+
}
443+
444+
match self {
445+
Error::Command { command, cause } => match cause.kind() {
446+
io::ErrorKind::NotFound => {
447+
format!("`{}` command not found. Is pkg-config installed?", trimmed_command(command))
448+
}
449+
_ => format!("Could not run `{}` command, because {}", trimmed_command(command), cause),
450+
},
451+
Error::Failure { command, output } => {
452+
format!("Command `{}` failed with {}", trimmed_command(command), output.status)
453+
}
454+
Error::ProbeFailure {
455+
name,
456+
command,
457+
output,
458+
} => format!(
459+
"The system library `{}` was not found, because command `{}` failed with {}",
460+
name, trimmed_command(command), output.status
461+
),
462+
Error::EnvNoPkgConfig(var) => {
463+
format!("pkg-config has been disabled via {} env var", var)
464+
}
465+
Error::CrossCompilation => {
466+
format!("pkg-config has not been set up to support cross-compilation")
467+
}
468+
_ => self.to_string(),
469+
}
470+
}
471+
}
472+
430473
fn format_output(output: &Output, f: &mut fmt::Formatter<'_>) -> fmt::Result {
431474
let stdout = String::from_utf8_lossy(&output.stdout);
432475
if !stdout.is_empty() {

0 commit comments

Comments
 (0)