aya-tool: simplify and improve error handling

reviewable/pr1363/r1
Tamir Duberstein 1 week ago
parent 98e8c78376
commit 9bc185a61e
No known key found for this signature in database

@ -2,7 +2,7 @@ use std::{
fs::{self, File}, fs::{self, File},
io::{self, Write as _}, io::{self, Write as _},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command, process::{Command, Output},
str, str,
}; };
@ -14,14 +14,14 @@ pub enum Error {
#[error("error executing bpftool")] #[error("error executing bpftool")]
BpfTool(#[source] io::Error), BpfTool(#[source] io::Error),
#[error("{stderr}\nbpftool failed with exit code {code}")] #[error("bpftool failed: {0:?}")]
BpfToolExit { code: i32, stderr: String }, BpfToolExit(Output),
#[error("bindgen failed")] #[error("bindgen failed")]
Bindgen(#[source] io::Error), Bindgen(#[source] io::Error),
#[error("{stderr}\nbindgen failed with exit code {code}")] #[error("bindgen failed: {0:?}")]
BindgenExit { code: i32, stderr: String }, BindgenExit(Output),
#[error("error reading header file")] #[error("error reading header file")]
ReadHeaderFile(#[source] io::Error), ReadHeaderFile(#[source] io::Error),
@ -74,14 +74,13 @@ pub fn generate<T: AsRef<str>>(
.output() .output()
.map_err(Error::Bindgen)?; .map_err(Error::Bindgen)?;
if !output.status.success() { let Output { status, .. } = &output;
return Err(Error::BindgenExit { if !status.success() {
code: output.status.code().unwrap(), return Err(Error::BindgenExit(output));
stderr: str::from_utf8(&output.stderr).unwrap().to_owned(),
});
} }
let Output { stdout, .. } = output;
Ok(str::from_utf8(&output.stdout).unwrap().to_owned()) Ok(String::from_utf8(stdout).unwrap())
} }
fn c_header_from_btf(path: &Path) -> Result<String, Error> { fn c_header_from_btf(path: &Path) -> Result<String, Error> {
@ -92,14 +91,13 @@ fn c_header_from_btf(path: &Path) -> Result<String, Error> {
.output() .output()
.map_err(Error::BpfTool)?; .map_err(Error::BpfTool)?;
if !output.status.success() { let Output { status, .. } = &output;
return Err(Error::BpfToolExit { if !status.success() {
code: output.status.code().unwrap(), return Err(Error::BpfToolExit(output));
stderr: str::from_utf8(&output.stderr).unwrap().to_owned(),
});
} }
let Output { stdout, .. } = output;
Ok(str::from_utf8(&output.stdout).unwrap().to_owned()) Ok(String::from_utf8(stdout).unwrap())
} }
fn extract_ctypes_prefix(s: &[String]) -> (Vec<String>, Option<String>) { fn extract_ctypes_prefix(s: &[String]) -> (Vec<String>, Option<String>) {

Loading…
Cancel
Save