Skip to content

Commit

Permalink
Remove Error enumeration from libbpf-cargo
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Nov 21, 2023
1 parent ef3e33b commit fe4de05
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libbpf-cargo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
----------
- Removed `Error` enum in favor of `anyhow::Error`
- Bumped minimum Rust version to `1.65`


Expand Down
1 change: 0 additions & 1 deletion libbpf-cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
39 changes: 13 additions & 26 deletions libbpf-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<T> = result::Result<T, Error>;

/// `SkeletonBuilder` builds and generates a single skeleton.
///
/// This interface is meant to be used in build scripts.
Expand Down Expand Up @@ -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
Expand All @@ -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(())
}
Expand All @@ -267,18 +257,15 @@ impl SkeletonBuilder {
//
// [`SkeletonBuilder::obj`] must be set for this to succeed.
pub fn generate<P: AsRef<Path>>(&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,
objfile,
gen::OutputDest::File(output.as_ref()),
Some(&self.rustfmt),
)
.map_err(Error::Generate)?;
.with_context(|| format!("failed to generate `{}`", objfile.display()))?;

Ok(())
}
Expand Down

0 comments on commit fe4de05

Please sign in to comment.