summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcc <cc@localhost>2025-08-20 17:11:53 -0700
committercc <cc@localhost>2025-08-20 17:11:53 -0700
commit950a720a5c0b43199877b3727f0d06412f8a12fb (patch)
tree3223020184feb9c69a4b9d8534f3914be0519e8b
parentd0deb0161c48e10505c668a57e0ed8a56c36c25a (diff)
Modularization
-rw-r--r--src/lib.rs63
-rw-r--r--src/tiff.rs52
2 files changed, 61 insertions, 54 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e3395e9..4e630d5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,50 +1,4 @@
-use std::os::raw::{c_char, c_void};
-use std::ffi::CString;
-
-/// # TIFF Internal Structure
-/// An opaque type
-#[repr(C)]
-pub struct TIFF {
- _unused: [u8; 0],
-}
-
-/// # TIFF Warning Handler Definition
-pub type TIFFWarningHandler = Option<unsafe extern "C" fn(module: *const c_char, fmt: *const c_char, ap: *mut c_void)>;
-
-/// # TIFF Ignore Warnings Handle
-unsafe extern "C" fn tiff_ignore_warning_handle(_module: *const c_char, _fmt: *const c_char, _ap: *mut std::ffi::c_void) {
- // Do nothing
-}
-
-#[link(name = "tiff")]
-unsafe extern "C" {
- pub fn TIFFSetWarningHandler(handler: TIFFWarningHandler) -> TIFFWarningHandler;
- pub fn TIFFOpen(filename: *const c_char, mode: *const c_char) -> *mut TIFF;
- pub fn TIFFClose(tiff_ptr: *mut TIFF);
-}
-
-/// # Ignore TIFF Warnings
-pub fn tiff_ignore_warnings() {
- unsafe {
- let _ = TIFFSetWarningHandler(Some(tiff_ignore_warning_handle));
- }
-}
-
-/// # Open TIFF File
-pub fn tiff_open(filename: &str) -> *mut TIFF {
- let c_filename = CString::new(filename).expect("Cast error");
- let c_mode = CString::new("r").expect("Cast error");
- unsafe {
- TIFFOpen(c_filename.as_ptr(), c_mode.as_ptr())
- }
-}
-
-/// # Close TIFF File
-pub fn tiff_close(tiff_ptr: *mut TIFF) {
- unsafe {
- TIFFClose(tiff_ptr);
- }
-}
+mod tiff;
#[cfg(test)]
mod tests {
@@ -57,16 +11,17 @@ mod tests {
#[test]
fn ignore_warning_test() {
- tiff_ignore_warnings();
- assert_eq!(1, 1);
+ tiff::ignore_warnings();
+ assert_eq!(0, 0);
}
#[test]
fn tiff_open_test() {
- tiff_ignore_warnings();
- let result = tiff_open("../test.tif");
- assert!(!result.is_null());
- tiff_close(result);
- assert_eq!(1, 1);
+ tiff::ignore_warnings();
+ if let Some(result) = tiff::open("../test.tif") {
+ tiff::close(result);
+ } else {
+ assert!(false);
+ }
}
}
diff --git a/src/tiff.rs b/src/tiff.rs
new file mode 100644
index 0000000..c2f1a17
--- /dev/null
+++ b/src/tiff.rs
@@ -0,0 +1,52 @@
+use std::os::raw::{c_char, c_void};
+use std::ffi::CString;
+
+/// # TIFF Internal Structure
+/// An opaque type
+#[repr(C)]
+pub struct TIFF {
+ _unused: [u8; 0],
+}
+
+/// # TIFF Warning Handler Definition
+type TIFFWarningHandler = Option<unsafe extern "C" fn(module: *const c_char, fmt: *const c_char, ap: *mut c_void)>;
+
+/// # TIFF Ignore Warnings Handle
+unsafe extern "C" fn tiff_ignore_warning_handle(_module: *const c_char, _fmt: *const c_char, _ap: *mut std::ffi::c_void) {
+ // Do nothing
+}
+
+#[link(name = "tiff")]
+unsafe extern "C" {
+ fn TIFFSetWarningHandler(handler: TIFFWarningHandler) -> TIFFWarningHandler;
+ fn TIFFOpen(filename: *const c_char, mode: *const c_char) -> *mut TIFF;
+ fn TIFFClose(tiff_ptr: *mut TIFF);
+}
+
+/// # Ignore TIFF Warnings
+pub fn ignore_warnings() {
+ unsafe {
+ let _ = TIFFSetWarningHandler(Some(tiff_ignore_warning_handle));
+ }
+}
+
+/// # Open TIFF File
+pub fn open(filename: &str) -> Option<*mut TIFF> {
+ let c_filename = CString::new(filename).expect("Cast error");
+ let c_mode = CString::new("r").expect("Cast error");
+ unsafe {
+ let result = TIFFOpen(c_filename.as_ptr(), c_mode.as_ptr());
+ if result.is_null() {
+ return None;
+ } else {
+ return Some(result);
+ }
+ }
+}
+
+/// # Close TIFF File
+pub fn close(tiff_ptr: *mut TIFF) {
+ unsafe {
+ TIFFClose(tiff_ptr);
+ }
+}