Skip to main content

snaphu_rs/
lib.rs

1use std::os::raw::c_int;
2
3pub fn run_cli<I, S>(args: I) -> std::io::Result<()>
4where
5    I: IntoIterator<Item = S>,
6    S: AsRef<str>,
7{
8    use std::ffi::CString;
9    use std::os::raw::c_char;
10
11    /* Convert Rust args -> CStrings */
12    let cstrings: Vec<CString> = args
13        .into_iter()
14        .map(|s| CString::new(s.as_ref()))
15        .collect::<Result<_, _>>()
16        .map_err(|_| {
17            std::io::Error::new(
18                std::io::ErrorKind::InvalidInput,
19                "argument contains interior NUL byte",
20            )
21        })?;
22
23    /* Build argv */
24    let mut argv: Vec<*mut c_char> = cstrings.iter().map(|s| s.as_ptr() as *mut c_char).collect();
25
26    let argc = argv.len() as c_int;
27
28    /* Call the raw function */
29    let exit_code: i32 = unsafe { snaphu_sys::run_main(argc, argv.as_mut_ptr()) };
30    if exit_code == 0 {
31        Ok(())
32    } else {
33        Err(std::io::Error::from_raw_os_error(exit_code))
34    }
35}
36
37extern crate libc;
38// Keep this nodule private for now. It is a c2rust translation of the original SNAPHU code.
39mod snaphu_full;