Skip to content

Commit a6717ef

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

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,62 @@ 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+
// Extracts just the command name from WrappedCommand's output.
434+
// The command name alone is not available in the Error enum.
435+
fn trimmed_command(command: &str) -> &str {
436+
let env_prefix = command
437+
.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!(
448+
"`{}` command not found. Is pkg-config installed?",
449+
trimmed_command(command)
450+
)
451+
}
452+
_ => format!(
453+
"Could not run `{}` command, because {}",
454+
trimmed_command(command),
455+
cause
456+
),
457+
},
458+
Error::Failure { command, output } => {
459+
format!(
460+
"Command `{}` failed with {}",
461+
trimmed_command(command),
462+
output.status
463+
)
464+
}
465+
Error::ProbeFailure {
466+
name,
467+
command,
468+
output,
469+
} => format!(
470+
"The system library `{}` was not found, because command `{}` failed with {}",
471+
name,
472+
trimmed_command(command),
473+
output.status
474+
),
475+
Error::EnvNoPkgConfig(var) => {
476+
format!("pkg-config has been disabled via {} env var", var)
477+
}
478+
Error::CrossCompilation => {
479+
format!("pkg-config has not been set up to support cross-compilation")
480+
}
481+
_ => self.to_string(),
482+
}
483+
}
484+
}
485+
430486
fn format_output(output: &Output, f: &mut fmt::Formatter<'_>) -> fmt::Result {
431487
let stdout = String::from_utf8_lossy(&output.stdout);
432488
if !stdout.is_empty() {

0 commit comments

Comments
 (0)