gltfpack_sys/lib.rs
1//! Rust bindings for meshoptimizer/gltfpack
2//!
3//! This crate bundles meshoptimizer v0.25 by Arseny Kapoulkine, licensed under MIT.
4//! See LICENSE for full license text.
5
6#![allow(non_upper_case_globals)]
7#![allow(non_camel_case_types)]
8#![allow(non_snake_case)]
9
10use std::ffi::CString;
11use std::os::raw::{c_char, c_int};
12use std::path::Path;
13
14extern "C" {
15 #[link_name = "compress"]
16 fn compress_ffi(input: *const c_char, output: *const c_char) -> c_int;
17}
18
19/// Compress a glTF/GLB file with maximum compression (equivalent to gltfpack -cc)
20///
21/// # Arguments
22/// * `input` - Path to input file (.obj/.gltf/.glb)
23/// * `output` - Path to output file (.gltf/.glb)
24///
25/// # Returns
26/// * `Ok(())` on success
27/// * `Err(i32)` with error code on failure
28///
29/// # Example
30/// ```no_run
31/// use gltfpack_sys::compress;
32///
33/// compress("model.glb", "model_compressed.glb").unwrap();
34/// ```
35pub fn compress(input: impl AsRef<Path>, output: impl AsRef<Path>) -> Result<(), i32> {
36 let input_c = CString::new(input.as_ref().to_str().ok_or(-1)?).map_err(|_| -1)?;
37 let output_c = CString::new(output.as_ref().to_str().ok_or(-1)?).map_err(|_| -1)?;
38
39 let result = unsafe { compress_ffi(input_c.as_ptr(), output_c.as_ptr()) };
40
41 if result == 0 {
42 Ok(())
43 } else {
44 Err(result)
45 }
46}