fitsio-sys 0.5.7

FFI wrapper around cfitsio
Documentation
//! This package was automatically generated with [`rust-bindgen`][1] and as such was not
//! user-generated.
//!
//! The functions contained are expected to be used with [`fitsio`][2], a high level API wrapper
//! around the low level direct C-bindings, though the bindings are complete enough to be usable.
//!
//! This code will not be directly documented, and so users should refer to the [`fitsio` C
//! documentation][3] for usage.
//!
//! ## Note about function names
//!
//! Unfortunately we must use fits short names throughout. The C-api exposes long names for
//! functions which are more descriptive, for example `fits_open_file` instead of `ffopen`, but the
//! symbols available in the library have only short names, and the long names are merely
//! preprocessor definitions.
//!
//! ## Examples
//!
//! ```rust
//! use std::ptr;
//! use std::ffi;
//! # use fitsio_sys::{ffinit, ffphps, ffclos};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let filename = ffi::CString::new("!/tmp/test.fits").unwrap();
//! let mut fptr = ptr::null_mut();
//! let mut status = 0;
//!
//! unsafe {
//!     // Create a new file, clobbering any pre-existing file
//!     ffinit(&mut fptr as *mut *mut _,
//!         filename.as_ptr(),
//!         &mut status);
//!
//!     // Add an empty primary HDU
//!     ffphps(fptr, 8, 0, ptr::null_mut(), &mut status);
//!
//!     // Finally close the file
//!     ffclos(fptr, &mut status);
//! }
//!
//! assert_eq!(status, 0);
//! # Ok(())
//! # }
//! ```
//!
//! [1]: https://github.com/crabtw/rust-bindgen
//! [2]: https://crates.io/crates/fitsio
//! [3]: http://heasarc.gsfc.nasa.gov/docs/software/fitsio/c/c_user/cfitsio.html

#![allow(improper_ctypes)]

mod aliases;
pub use aliases::*;

#[cfg(not(feature = "bindgen"))]
#[allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
#[allow(
    clippy::unreadable_literal,
    clippy::transmute_ptr_to_ptr,
    clippy::redundant_static_lifetimes,
    clippy::missing_safety_doc,
    clippy::should_implement_trait,
    clippy::upper_case_acronyms
)]
mod sys {

    // automatically generated by rust-bindgen
    // Prevent clippy from throwing errors in generated code

    #[cfg(target_pointer_width = "64")]
    include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bindings_64.rs"));
    #[cfg(target_pointer_width = "32")]
    include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bindings_32.rs"));
}

#[cfg(feature = "bindgen")]
#[allow(
    non_upper_case_globals,
    non_camel_case_types,
    non_snake_case,
    improper_ctypes
)]
// Prevent clippy from throwing errors in generated code
#[allow(
    clippy::unreadable_literal,
    clippy::transmute_ptr_to_ptr,
    clippy::redundant_static_lifetimes,
    clippy::missing_safety_doc,
    clippy::useless_transmute,
    clippy::trivially_copy_pass_by_ref,
    clippy::too_many_arguments,
    clippy::should_implement_trait,
    clippy::upper_case_acronyms
)]
mod sys {
    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

pub use sys::*;

// global functions

/// Representation of the version of cfitsio used within bindings
pub struct CfitsioVersion {
    /// Minor version
    pub minor: u32,
    /// Major version
    pub major: u32,
}

impl std::fmt::Display for CfitsioVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}", self.major, self.minor)
    }
}

pub fn cfitsio_version() -> CfitsioVersion {
    CfitsioVersion {
        // TODO: we need to detect the version of cfitsio we are binding to. Version >=4 supports
        // this field, but earlier versions don't.
        // patch: CFITSIO_MICRO,
        minor: CFITSIO_MINOR,
        major: CFITSIO_MAJOR,
    }
}

#[cfg(test)]
mod tests {
    use std::{ffi::CString, ptr};

    use crate::{
        aliases::{fits_get_num_rows, fits_movabs_hdu, fits_open_file},
        ffgnrw, ffmahd, ffopen,
    };

    #[test]
    fn fitsio_sys_works() {
        let mut fptr = ptr::null_mut();
        let filename = CString::new("../testdata/full_example.fits").expect("valid C string");
        let iomode = 0; // read only
        let mut num_table_rows = 0;
        let mut status = 0;

        unsafe {
            ffopen(&mut fptr, filename.as_ptr(), iomode, &mut status);
            assert_eq!(status, 0);

            ffmahd(fptr, 2, &mut 0, &mut status);
            assert_eq!(status, 0);

            ffgnrw(fptr, &mut num_table_rows, &mut status);
            assert_eq!(status, 0);
            assert_eq!(num_table_rows, 50);
        }
    }

    #[test]
    fn fitsio_sys_aliases_work() {
        let mut fptr = ptr::null_mut();
        let filename = CString::new("../testdata/full_example.fits").expect("valid C string");
        let iomode = 0; // read only
        let mut num_table_rows = 0;
        let mut status = 0;

        unsafe {
            fits_open_file(&mut fptr, filename.as_ptr(), iomode, &mut status);
            assert_eq!(status, 0);

            fits_movabs_hdu(fptr, 2, &mut 0, &mut status);
            assert_eq!(status, 0);

            fits_get_num_rows(fptr, &mut num_table_rows, &mut status);
            assert_eq!(status, 0);
            assert_eq!(num_table_rows, 50);
        }
    }
}