From fe4de05986532faa9398a9a597009b56c2f35d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Mon, 20 Nov 2023 11:25:50 -0800 Subject: [PATCH] Remove Error enumeration from libbpf-cargo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Error enumeration in libbpf-cargo doesn't really serve any meaningful purposes at this point: sure, it helps users differentiate between build and generation errors, but the difference arguably has no semantic significance that is worth preserving. On top of that, users can always invoke build() and generate() instead of build_and_generate() to have literally the same information at their finger tips. Remove the Error type in favor of just exposing a anyhow::Error, which is probably the most flexible. Signed-off-by: Daniel Müller --- Cargo.lock | 1 - libbpf-cargo/CHANGELOG.md | 1 + libbpf-cargo/Cargo.toml | 1 - libbpf-cargo/src/lib.rs | 39 +++++++++++++-------------------------- 4 files changed, 14 insertions(+), 28 deletions(-) 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(()) }