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

Add BindgenError and Change generate to return the error #2125

Merged
merged 10 commits into from
Dec 29, 2021
35 changes: 18 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,20 +2147,28 @@ fn ensure_libclang_is_loaded() {
fn ensure_libclang_is_loaded() {}

/// Error type for rust-bindgen.
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BindgenError {
MikuroXina marked this conversation as resolved.
Show resolved Hide resolved
/// Any provided header was invalid.
InvalidHeader(String),
/// The header was a folder.
FolderAsHeader(String),
/// Permissions to read the header is insufficient.
InsufficientPermissions(String),
/// The header does not exist.
NotExist(String),
MikuroXina marked this conversation as resolved.
Show resolved Hide resolved
/// Clang diagnosed an error.
ClangDiagnostic(String),
}

impl std::fmt::Display for BindgenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BindgenError::InvalidHeader(message) => {
write!(f, "invalid header: {}", message)
BindgenError::FolderAsHeader(h) => write!(f, "'{}' is a folder", h),
BindgenError::InsufficientPermissions(h) => {
write!(f, "insufficient permissions to read '{}'", h)
}
BindgenError::NotExist(h) => {
write!(f, "header '{}' does not exist.", h)
}
BindgenError::ClangDiagnostic(message) => {
write!(f, "clang diagnosed error: {}", message)
Expand Down Expand Up @@ -2343,23 +2351,16 @@ impl Bindings {
if let Some(h) = options.input_header.as_ref() {
if let Ok(md) = std::fs::metadata(h) {
if md.is_dir() {
return Err(BindgenError::InvalidHeader(format!(
"'{}' is a folder",
h
)));
return Err(BindgenError::FolderAsHeader(h.into()));
}
if !can_read(&md.permissions()) {
return Err(BindgenError::InvalidHeader(format!(
"insufficient permissions to read '{}'",
h
)));
return Err(BindgenError::InsufficientPermissions(
h.into(),
));
}
options.clang_args.push(h.clone())
} else {
return Err(BindgenError::InvalidHeader(format!(
"header '{}' does not exist.",
h
)));
return Err(BindgenError::NotExist(h.into()));
}
}

Expand Down