Skip to content

Commit

Permalink
Update build.rs
Browse files Browse the repository at this point in the history
Moved RUSTIFIED_ENUMS before main()
Added proper error handling with Result
Converted unwrap() and expect() to ?
Maintained all existing comments and line structure
  • Loading branch information
qxrein authored Dec 26, 2024
1 parent b08c5fa commit 37cb83a
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ extern crate pkg_config;
use std::env;
use std::path::PathBuf;

fn main() {
const RUSTIFIED_ENUMS: &[&str] = &[
"dtvcc_(window|pen)_.*",
"ccx_output_format",
"ccx_output_date_format",
];

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut allowlist_functions = Vec::new();
allowlist_functions.extend_from_slice(&[
".*(?i)_?dtvcc_.*",
Expand All @@ -13,15 +19,13 @@ fn main() {
"version",
"set_binary_mode",
]);

#[cfg(feature = "hardsubx_ocr")]
allowlist_functions.extend_from_slice(&[
"edit_distance",
"convert_pts_to_.*",
"av_rescale_q",
"mprint",
]);

let mut allowlist_types = Vec::new();
allowlist_types.extend_from_slice(&[
".*(?i)_?dtvcc_.*",
Expand All @@ -41,54 +45,41 @@ fn main() {
"uint8_t",
"word_list",
]);

#[cfg(feature = "hardsubx_ocr")]
allowlist_types.extend_from_slice(&["AVRational", "AVPacket", "AVFrame"]);

let mut builder = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h");

// enable hardsubx if and only if the feature is on
#[cfg(feature = "hardsubx_ocr")]
{
builder = builder.clang_arg("-DENABLE_HARDSUBX");
}

// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
builder = builder.parse_callbacks(Box::new(bindgen::CargoCallbacks));

for type_name in allowlist_types {
builder = builder.allowlist_type(type_name);
}

for fn_name in allowlist_functions {
builder = builder.allowlist_function(fn_name);
}

for rust_enum in RUSTIFIED_ENUMS {
builder = builder.rustified_enum(rust_enum);
}

let bindings = builder
.derive_default(true)
.no_default("dtvcc_pen_attribs|dtvcc_pen_color|dtvcc_symbol")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Convert to Result
.map_err(|e| format!("Unable to generate bindings: {}", e))?;
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_path = PathBuf::from(env::var("OUT_DIR")?);
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
.map_err(|e| format!("Couldn't write bindings: {}", e))?;

Ok(())
}

const RUSTIFIED_ENUMS: &[&str] = &[
"dtvcc_(window|pen)_.*",
"ccx_output_format",
"ccx_output_date_format",
];

0 comments on commit 37cb83a

Please sign in to comment.