intermodal/bin/gen/src/command_ext.rs
Casey Rodarmor 342266853e
Improve bin/gen error messages
Create an error enum with actual error messages.

type: development
2020-04-29 00:36:25 -07:00

53 lines
1.1 KiB
Rust

use crate::common::*;
#[allow(redundant_semicolons)]
pub(crate) trait CommandExt {
#[throws]
fn out(&mut self) -> String;
#[throws]
fn status_into_result(&mut self);
}
impl CommandExt for Command {
#[throws]
fn out(&mut self) -> String {
info!("Running {:?}…", self);
let output = self
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.output()
.context(error::CommandInvoke {
command: format!("{:?}", self),
})?;
if !output.status.success() {
throw!(Error::CommandStatus {
command: format!("{:?}", self),
exit_status: output.status,
});
}
let text = String::from_utf8(output.stdout).context(error::CommandDecode {
command: format!("{:?}", self),
})?;
text
}
#[throws]
fn status_into_result(&mut self) {
let status = self.status().context(error::CommandInvoke {
command: format!("{:?}", self),
})?;
if !status.success() {
throw!(Error::CommandStatus {
command: format!("{:?}", self),
exit_status: status
});
}
}
}