diff --git a/Cargo.lock b/Cargo.lock index c42d7386..98135396 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,7 +263,6 @@ dependencies = [ "serde", "serde_json", "tempfile", - "thiserror", ] [[package]] diff --git a/libbpf-cargo/CHANGELOG.md b/libbpf-cargo/CHANGELOG.md index 9098e111..afcf160e 100644 --- a/libbpf-cargo/CHANGELOG.md +++ b/libbpf-cargo/CHANGELOG.md @@ -1,5 +1,6 @@ Unreleased ---------- +- Removed `Error` enum in favor of `anyhow::Error` - Bumped minimum Rust version to `1.65` diff --git a/libbpf-cargo/Cargo.toml b/libbpf-cargo/Cargo.toml index 1af3504d..1df799ef 100644 --- a/libbpf-cargo/Cargo.toml +++ b/libbpf-cargo/Cargo.toml @@ -43,7 +43,6 @@ semver = "1.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tempfile = "3.3" -thiserror = "1.0" clap = { version = "4.0.32", default-features = false, features = ["std", "derive", "help", "usage"] } [dev-dependencies] diff --git a/libbpf-cargo/src/lib.rs b/libbpf-cargo/src/lib.rs index 6cbdd012..48aea59a 100644 --- a/libbpf-cargo/src/lib.rs +++ b/libbpf-cargo/src/lib.rs @@ -65,12 +65,13 @@ use std::path::Path; use std::path::PathBuf; -use std::result; use anyhow::anyhow; +use anyhow::Context as _; +use anyhow::Result; + use tempfile::tempdir; use tempfile::TempDir; -use thiserror::Error; // libbpf-cargo binary is the primary consumer of the following modules. As such, // we do not use all the symbols. Silence any unused code warnings. @@ -86,17 +87,6 @@ mod metadata; #[cfg(test)] mod test; -/// Canonical error type for this crate. -#[derive(Error, Debug)] -pub enum Error { - #[error("Error building BPF object file")] - Build(#[source] anyhow::Error), - #[error("Error generating skeleton")] - Generate(#[source] anyhow::Error), -} - -pub type Result = result::Result; - /// `SkeletonBuilder` builds and generates a single skeleton. /// /// This interface is meant to be used in build scripts. @@ -225,24 +215,24 @@ impl SkeletonBuilder { let source = self .source .as_ref() - .ok_or_else(|| Error::Build(anyhow!("No source file")))?; + .ok_or_else(|| anyhow!("No source file provided"))?; let filename = source .file_name() - .ok_or_else(|| Error::Build(anyhow!("Missing file name")))? + .ok_or_else(|| anyhow!("Missing file name"))? .to_str() - .ok_or_else(|| Error::Build(anyhow!("Invalid unicode in file name")))?; + .ok_or_else(|| anyhow!("Invalid unicode in file name"))?; if !filename.ends_with(".bpf.c") { - return Err(Error::Build(anyhow!( - "Source file={} does not have .bpf.c suffix", + return Err(anyhow!( + "Source `{}` does not have .bpf.c suffix", source.display() - ))); + )); } if self.obj.is_none() { let name = filename.split('.').next().unwrap(); - let dir = tempdir().map_err(|e| Error::Build(e.into()))?; + let dir = tempdir().context("failed to create temporary directory")?; let objfile = dir.path().join(format!("{name}.o")); self.obj = Some(objfile); // Hold onto tempdir so that it doesn't get deleted early @@ -258,7 +248,7 @@ impl SkeletonBuilder { self.skip_clang_version_check, &self.clang_args, ) - .map_err(Error::Build)?; + .with_context(|| format!("failed to build `{}`", source.display()))?; Ok(()) } @@ -267,10 +257,7 @@ impl SkeletonBuilder { // // [`SkeletonBuilder::obj`] must be set for this to succeed. pub fn generate>(&mut self, output: P) -> Result<()> { - let objfile = self - .obj - .as_ref() - .ok_or_else(|| Error::Generate(anyhow!("No object file")))?; + let objfile = self.obj.as_ref().ok_or_else(|| anyhow!("No object file"))?; gen::gen_single( self.debug, @@ -278,7 +265,7 @@ impl SkeletonBuilder { gen::OutputDest::File(output.as_ref()), Some(&self.rustfmt), ) - .map_err(Error::Generate)?; + .with_context(|| format!("failed to generate `{}`", objfile.display()))?; Ok(()) }