Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Error enumeration from libbpf-cargo #609

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading